Skip to content

Commit dbb8a87

Browse files
committed
Fix ptr-arg false positive for trait impls
Fixes #425
1 parent 555328c commit dbb8a87

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/ptr_arg.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use rustc::lint::*;
66
use rustc_front::hir::*;
77
use rustc::middle::ty;
8+
use rustc::front::map::Node;
89

910
use utils::{span_lint, match_type};
1011
use utils::{STRING_PATH, VEC_PATH};
@@ -34,6 +35,11 @@ impl LateLintPass for PtrArg {
3435

3536
fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
3637
if let &MethodImplItem(ref sig, _) = &item.node {
38+
if let Some(Node::NodeItem(it)) = cx.tcx.map.find(cx.tcx.map.get_parent(item.id)) {
39+
if let ItemImpl(_, _, _, Some(_), _, _) = it.node {
40+
return; // ignore trait impls
41+
}
42+
}
3743
check_fn(cx, &sig.decl);
3844
}
3945
}
@@ -47,8 +53,8 @@ impl LateLintPass for PtrArg {
4753

4854
fn check_fn(cx: &LateContext, decl: &FnDecl) {
4955
for arg in &decl.inputs {
50-
if let Some(pat_ty) = cx.tcx.pat_ty_opt(&arg.pat) {
51-
if let ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = pat_ty.sty {
56+
if let Some(ty) = cx.tcx.ast_ty_to_ty_cache.borrow().get(&arg.ty.id) {
57+
if let ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = ty.sty {
5258
if match_type(cx, ty, &VEC_PATH) {
5359
span_lint(cx, PTR_ARG, arg.ty.span,
5460
"writing `&Vec<_>` instead of `&[_]` involves one more reference \

tests/compile-fail/ptr_arg.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ fn do_str_mut(x: &mut String) { // no error here
2121

2222
fn main() {
2323
}
24+
25+
trait Foo {
26+
type Item;
27+
fn do_vec(x: &Vec<i64>); //~ERROR writing `&Vec<_>`
28+
fn do_item(x: &Self::Item);
29+
}
30+
31+
struct Bar;
32+
33+
// no error, in trait impl (#425)
34+
impl Foo for Bar {
35+
type Item = Vec<u8>;
36+
fn do_vec(x: &Vec<i64>) {}
37+
fn do_item(x: &Vec<u8>) {}
38+
}

0 commit comments

Comments
 (0)