Skip to content

Fix ptr-arg false positive for trait impls #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/ptr_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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 \
Expand Down
15 changes: 15 additions & 0 deletions tests/compile-fail/ptr_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {}
}