Skip to content

Commit de77498

Browse files
author
Michael Wright
committedJul 11, 2019
Fix bug in implicit_hasher causing crashes
Skip linting if the type is from an external macro. Closes #4260.
1 parent 032ae96 commit de77498

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed
 

‎clippy_lints/src/types.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitHasher {
20382038
vis.visit_ty(ty);
20392039

20402040
for target in &vis.found {
2041+
if in_external_macro(cx.sess(), generics.span) {
2042+
continue;
2043+
}
20412044
let generics_suggestion_span = generics.span.substitute_dummy({
20422045
let pos = snippet_opt(cx, item.span.until(body.arguments[0].pat.span))
20432046
.and_then(|snip| {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[macro_export]
2+
macro_rules! implicit_hasher_fn {
3+
() => {
4+
pub fn f(input: &HashMap<u32, u32>) {}
5+
};
6+
}

‎tests/ui/implicit_hasher.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// aux-build:implicit_hasher_macros.rs
12
#![allow(unused)]
23

4+
#[macro_use]
5+
extern crate implicit_hasher_macros;
6+
37
use std::cmp::Eq;
48
use std::collections::{HashMap, HashSet};
59
use std::hash::{BuildHasher, Hash};
@@ -88,4 +92,7 @@ gen!(fn bar);
8892
pub mod test_macro;
8993
__implicit_hasher_test_macro!(impl<K, V> for HashMap<K, V> where V: test_macro::A);
9094

95+
// #4260
96+
implicit_hasher_fn!();
97+
9198
fn main() {}

‎tests/ui/implicit_hasher.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: impl for `HashMap` should be generalized over different hashers
2-
--> $DIR/implicit_hasher.rs:11:35
2+
--> $DIR/implicit_hasher.rs:15:35
33
|
44
LL | impl<K: Hash + Eq, V> Foo<i8> for HashMap<K, V> {
55
| ^^^^^^^^^^^^^
@@ -15,7 +15,7 @@ LL | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default:
1515
| ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616

1717
error: impl for `HashMap` should be generalized over different hashers
18-
--> $DIR/implicit_hasher.rs:20:36
18+
--> $DIR/implicit_hasher.rs:24:36
1919
|
2020
LL | impl<K: Hash + Eq, V> Foo<i8> for (HashMap<K, V>,) {
2121
| ^^^^^^^^^^^^^
@@ -29,7 +29,7 @@ LL | ((HashMap::default(),), (HashMap::with_capacity_and_hasher(10, Defa
2929
| ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3030

3131
error: impl for `HashMap` should be generalized over different hashers
32-
--> $DIR/implicit_hasher.rs:25:19
32+
--> $DIR/implicit_hasher.rs:29:19
3333
|
3434
LL | impl Foo<i16> for HashMap<String, String> {
3535
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -43,7 +43,7 @@ LL | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default:
4343
| ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

4545
error: impl for `HashSet` should be generalized over different hashers
46-
--> $DIR/implicit_hasher.rs:42:32
46+
--> $DIR/implicit_hasher.rs:46:32
4747
|
4848
LL | impl<T: Hash + Eq> Foo<i8> for HashSet<T> {
4949
| ^^^^^^^^^^
@@ -57,7 +57,7 @@ LL | (HashSet::default(), HashSet::with_capacity_and_hasher(10, Default:
5757
| ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5858

5959
error: impl for `HashSet` should be generalized over different hashers
60-
--> $DIR/implicit_hasher.rs:47:19
60+
--> $DIR/implicit_hasher.rs:51:19
6161
|
6262
LL | impl Foo<i16> for HashSet<String> {
6363
| ^^^^^^^^^^^^^^^
@@ -71,7 +71,7 @@ LL | (HashSet::default(), HashSet::with_capacity_and_hasher(10, Default:
7171
| ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7272

7373
error: parameter of type `HashMap` should be generalized over different hashers
74-
--> $DIR/implicit_hasher.rs:64:23
74+
--> $DIR/implicit_hasher.rs:68:23
7575
|
7676
LL | pub fn foo(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32>) {}
7777
| ^^^^^^^^^^^^^^^^^
@@ -81,7 +81,7 @@ LL | pub fn foo<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i32, S>, _s
8181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
8282

8383
error: parameter of type `HashSet` should be generalized over different hashers
84-
--> $DIR/implicit_hasher.rs:64:53
84+
--> $DIR/implicit_hasher.rs:68:53
8585
|
8686
LL | pub fn foo(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32>) {}
8787
| ^^^^^^^^^^^^
@@ -91,7 +91,7 @@ LL | pub fn foo<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i32>, _set:
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
9292

9393
error: impl for `HashMap` should be generalized over different hashers
94-
--> $DIR/implicit_hasher.rs:68:43
94+
--> $DIR/implicit_hasher.rs:72:43
9595
|
9696
LL | impl<K: Hash + Eq, V> Foo<u8> for HashMap<K, V> {
9797
| ^^^^^^^^^^^^^
@@ -108,7 +108,7 @@ LL | (HashMap::default(), HashMap::with_capacity_and_hasher(10,
108108
| ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
109109

110110
error: parameter of type `HashMap` should be generalized over different hashers
111-
--> $DIR/implicit_hasher.rs:76:33
111+
--> $DIR/implicit_hasher.rs:80:33
112112
|
113113
LL | pub fn $name(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32>) {}
114114
| ^^^^^^^^^^^^^^^^^
@@ -121,7 +121,7 @@ LL | pub fn $name<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i
121121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
122122

123123
error: parameter of type `HashSet` should be generalized over different hashers
124-
--> $DIR/implicit_hasher.rs:76:63
124+
--> $DIR/implicit_hasher.rs:80:63
125125
|
126126
LL | pub fn $name(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32>) {}
127127
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)
Please sign in to comment.