Fix ptr-arg false positive for trait impls

Fixes #425
This commit is contained in:
Manish Goregaokar 2015-10-31 05:18:05 +05:30
parent 555328cc7b
commit dbb8a872a3
2 changed files with 23 additions and 2 deletions

View File

@ -5,6 +5,7 @@
use rustc::lint::*;
use rustc_front::hir::*;
use rustc::middle::ty;
use rustc::front::map::Node;
use utils::{span_lint, match_type};
use utils::{STRING_PATH, VEC_PATH};
@ -34,6 +35,11 @@ impl LateLintPass for PtrArg {
fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
if let &MethodImplItem(ref sig, _) = &item.node {
if let Some(Node::NodeItem(it)) = cx.tcx.map.find(cx.tcx.map.get_parent(item.id)) {
if let ItemImpl(_, _, _, Some(_), _, _) = it.node {
return; // ignore trait impls
}
}
check_fn(cx, &sig.decl);
}
}
@ -47,8 +53,8 @@ impl LateLintPass for PtrArg {
fn check_fn(cx: &LateContext, decl: &FnDecl) {
for arg in &decl.inputs {
if let Some(pat_ty) = cx.tcx.pat_ty_opt(&arg.pat) {
if let ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = pat_ty.sty {
if let Some(ty) = cx.tcx.ast_ty_to_ty_cache.borrow().get(&arg.ty.id) {
if let ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = ty.sty {
if match_type(cx, ty, &VEC_PATH) {
span_lint(cx, PTR_ARG, arg.ty.span,
"writing `&Vec<_>` instead of `&[_]` involves one more reference \

View File

@ -21,3 +21,18 @@ fn do_str_mut(x: &mut String) { // no error here
fn main() {
}
trait Foo {
type Item;
fn do_vec(x: &Vec<i64>); //~ERROR writing `&Vec<_>`
fn do_item(x: &Self::Item);
}
struct Bar;
// no error, in trait impl (#425)
impl Foo for Bar {
type Item = Vec<u8>;
fn do_vec(x: &Vec<i64>) {}
fn do_item(x: &Vec<u8>) {}
}