From 3045f432c7d14841f69d5ad24c353e9a1a61eb3c Mon Sep 17 00:00:00 2001 From: HMPerson1 Date: Sun, 4 Mar 2018 22:56:03 -0500 Subject: [PATCH] Fix #2496 --- clippy_lints/src/loops.rs | 3 ++- clippy_lints/src/utils/paths.rs | 2 ++ tests/ui/for_loop.rs | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index ca1d987db..085cbaf9b 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -1624,7 +1624,8 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> { if_chain! { // a range index op if let ExprMethodCall(ref meth, _, ref args) = expr.node; - if meth.name == "index" || meth.name == "index_mut"; + if (meth.name == "index" && match_trait_method(self.cx, expr, &paths::INDEX)) + || (meth.name == "index_mut" && match_trait_method(self.cx, expr, &paths::INDEX_MUT)); if !self.check(&args[1], &args[0], expr); then { return } } diff --git a/clippy_lints/src/utils/paths.rs b/clippy_lints/src/utils/paths.rs index 20244a19f..81c402ab4 100644 --- a/clippy_lints/src/utils/paths.rs +++ b/clippy_lints/src/utils/paths.rs @@ -33,6 +33,8 @@ pub const HASH: [&str; 2] = ["hash", "Hash"]; pub const HASHMAP: [&str; 5] = ["std", "collections", "hash", "map", "HashMap"]; pub const HASHMAP_ENTRY: [&str; 5] = ["std", "collections", "hash", "map", "Entry"]; pub const HASHSET: [&str; 5] = ["std", "collections", "hash", "set", "HashSet"]; +pub const INDEX: [&str; 3] = ["core", "ops", "Index"]; +pub const INDEX_MUT: [&str; 3] = ["core", "ops", "IndexMut"]; pub const INIT: [&str; 4] = ["core", "intrinsics", "", "init"]; pub const INTO: [&str; 3] = ["core", "convert", "Into"]; pub const INTO_ITERATOR: [&str; 4] = ["core", "iter", "traits", "IntoIterator"]; diff --git a/tests/ui/for_loop.rs b/tests/ui/for_loop.rs index d606e7a15..92f95e09d 100644 --- a/tests/ui/for_loop.rs +++ b/tests/ui/for_loop.rs @@ -556,3 +556,18 @@ pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) { dst[d + i] = dst[s + i]; } } + +mod issue_2496 { + pub trait Handle { + fn new_for_index(index: usize) -> Self; + fn index(&self) -> usize; + } + + pub fn test() -> H { + for x in 0..5 { + let next_handle = H::new_for_index(x); + println!("{}", next_handle.index()); + } + unimplemented!() + } +}