Skip to content

Commit

Permalink
Rollup merge of rust-lang#4268 - mikerite:fix-4260, r=flip1995
Browse files Browse the repository at this point in the history
Fix bug in `implicit_hasher` causing crashes

Skip linting if the type is from an external macro. Closes rust-lang#4260.

changelog: Fix bug in `implicit_hasher` causing crashes
  • Loading branch information
flip1995 authored Jul 12, 2019
2 parents 989a807 + de77498 commit a21c00c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitHasher {
vis.visit_ty(ty);

for target in &vis.found {
if in_external_macro(cx.sess(), generics.span) {
continue;
}
let generics_suggestion_span = generics.span.substitute_dummy({
let pos = snippet_opt(cx, item.span.until(body.arguments[0].pat.span))
.and_then(|snip| {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/auxiliary/implicit_hasher_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[macro_export]
macro_rules! implicit_hasher_fn {
() => {
pub fn f(input: &HashMap<u32, u32>) {}
};
}
7 changes: 7 additions & 0 deletions tests/ui/implicit_hasher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// aux-build:implicit_hasher_macros.rs
#![allow(unused)]

#[macro_use]
extern crate implicit_hasher_macros;

use std::cmp::Eq;
use std::collections::{HashMap, HashSet};
use std::hash::{BuildHasher, Hash};
Expand Down Expand Up @@ -88,4 +92,7 @@ gen!(fn bar);
pub mod test_macro;
__implicit_hasher_test_macro!(impl<K, V> for HashMap<K, V> where V: test_macro::A);

// #4260
implicit_hasher_fn!();

fn main() {}
20 changes: 10 additions & 10 deletions tests/ui/implicit_hasher.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: impl for `HashMap` should be generalized over different hashers
--> $DIR/implicit_hasher.rs:11:35
--> $DIR/implicit_hasher.rs:15:35
|
LL | impl<K: Hash + Eq, V> Foo<i8> for HashMap<K, V> {
| ^^^^^^^^^^^^^
Expand All @@ -15,7 +15,7 @@ LL | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default:
| ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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

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

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

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

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

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

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

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

0 comments on commit a21c00c

Please sign in to comment.