ptr_arg improvements (fixes #214)

* do not trigger on mutable references
* use "real" type from ty, not AST type
This commit is contained in:
Georg Brandl 2015-08-21 18:28:17 +02:00
parent 6ffe1ed337
commit b2df15d65a
2 changed files with 37 additions and 27 deletions

View File

@ -4,10 +4,9 @@
use rustc::lint::*;
use syntax::ast::*;
use syntax::codemap::Span;
use rustc::middle::ty;
use types::match_ty_unwrap;
use utils::span_lint;
use utils::{span_lint, match_def_path};
declare_lint! {
pub PTR_ARG,
@ -43,24 +42,32 @@ impl LintPass for PtrArg {
}
}
#[allow(unused_imports)]
fn check_fn(cx: &Context, decl: &FnDecl) {
{
// In case stuff gets moved around
use collections::vec::Vec;
use collections::string::String;
}
for arg in &decl.inputs {
match &arg.ty.node {
&TyPtr(ref p) | &TyRptr(_, ref p) =>
check_ptr_subtype(cx, arg.ty.span, &p.ty),
_ => ()
if arg.ty.node == TyInfer { // "self" arguments
continue;
}
let ref sty = cx.tcx.pat_ty(&*arg.pat).sty;
if let &ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = sty {
if let ty::TyStruct(did, _) = ty.sty {
if match_def_path(cx, did.did, &["collections", "vec", "Vec"]) {
span_lint(cx, PTR_ARG, arg.ty.span,
"writing `&Vec<_>` instead of `&[_]` involves one more reference \
and cannot be used with non-Vec-based slices. Consider changing \
the type to `&[...]`");
}
else if match_def_path(cx, did.did, &["collections", "string", "String"]) {
span_lint(cx, PTR_ARG, arg.ty.span,
"writing `&String` instead of `&str` involves a new object \
where a slice will do. Consider changing the type to `&str`");
}
}
}
}
}
fn check_ptr_subtype(cx: &Context, span: Span, ty: &Ty) {
match_ty_unwrap(ty, &["Vec"]).map_or_else(|| match_ty_unwrap(ty,
&["String"]).map_or((), |_| {
span_lint(cx, PTR_ARG, span,
"writing `&String` instead of `&str` involves a new object \
where a slice will do. Consider changing the type to `&str`")
}), |_| span_lint(cx, PTR_ARG, span,
"writing `&Vec<_>` instead of \
`&[_]` involves one more reference and cannot be used with \
non-Vec-based slices. Consider changing the type to `&[...]`"))
}

View File

@ -1,20 +1,23 @@
#![feature(plugin)]
#![plugin(clippy)]
#![allow(unused)]
#![deny(ptr_arg)]
#[deny(ptr_arg)]
#[allow(unused)]
fn do_vec(x: &Vec<i64>) { //~ERROR writing `&Vec<_>` instead of `&[_]`
//Nothing here
}
#[deny(ptr_arg)]
#[allow(unused)]
fn do_vec_mut(x: &mut Vec<i64>) { // no error here
//Nothing here
}
fn do_str(x: &String) { //~ERROR writing `&String` instead of `&str`
//Nothing here either
}
fn main() {
let x = vec![1i64, 2, 3];
do_vec(&x);
do_str(&"hello".to_owned());
fn do_str_mut(x: &mut String) { // no error here
//Nothing here either
}
fn main() {
}