Skip to content

Commit eea3077

Browse files
committed
Type parameter change and type change are now in a multispan suggestion
1 parent a4f45e8 commit eea3077

File tree

2 files changed

+35
-72
lines changed

2 files changed

+35
-72
lines changed

clippy_lints/src/types.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,8 +1453,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidUpcastComparisons {
14531453
}
14541454
}
14551455

1456-
/// **What it does:** Checks for public `impl` or `fn` missing generalization over
1457-
/// different hashers and implicitly defaulting to the default hashing
1456+
/// **What it does:** Checks for public `impl` or `fn` missing generalization
1457+
/// over different hashers and implicitly defaulting to the default hashing
14581458
/// algorithm (SipHash).
14591459
///
14601460
/// **Why is this bad?** `HashMap` or `HashSet` with custom hashers cannot be
@@ -1505,26 +1505,29 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitHasher {
15051505
&generics_snip[1..generics_snip.len() - 1]
15061506
};
15071507

1508-
db.span_suggestion(
1509-
generics_suggestion_span,
1510-
"consider adding a type parameter",
1511-
format!(
1512-
"<{}{}S: ::std::hash::BuildHasher{}>",
1513-
generics_snip,
1514-
if generics_snip.is_empty() { "" } else { ", " },
1515-
if vis.suggestions.is_empty() {
1516-
""
1517-
} else {
1518-
// request users to add `Default` bound so that generic constructors can be used
1519-
" + Default"
1520-
},
1521-
),
1522-
);
1523-
1524-
db.span_suggestion(
1525-
target.span(),
1526-
"...and change the type to",
1527-
format!("{}<{}, S>", target.type_name(), target.type_arguments(),),
1508+
multispan_sugg(
1509+
db,
1510+
"consider adding a type parameter".to_string(),
1511+
vec![
1512+
(
1513+
generics_suggestion_span,
1514+
format!(
1515+
"<{}{}S: ::std::hash::BuildHasher{}>",
1516+
generics_snip,
1517+
if generics_snip.is_empty() { "" } else { ", " },
1518+
if vis.suggestions.is_empty() {
1519+
""
1520+
} else {
1521+
// request users to add `Default` bound so that generic constructors can be used
1522+
" + Default"
1523+
},
1524+
),
1525+
),
1526+
(
1527+
target.span(),
1528+
format!("{}<{}, S>", target.type_name(), target.type_arguments(),),
1529+
),
1530+
],
15281531
);
15291532

15301533
if !vis.suggestions.is_empty() {

tests/ui/implicit_hasher.stderr

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ error: impl for `HashMap` should be generarized over different hashers
77
= note: `-D implicit-hasher` implied by `-D warnings`
88
help: consider adding a type parameter
99
|
10-
11 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<i8> for HashMap<K, V> {
10+
11 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<i8> for HashMap<K, V, S> {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
help: ...and change the type to
13-
|
14-
11 | impl<K: Hash + Eq, V> Foo<i8> for HashMap<K, V, S> {
15-
| ^^^^^^^^^^^^^^^^
1612
help: ...and use generic constructor
1713
|
1814
17 | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default::default()))
@@ -26,12 +22,8 @@ error: impl for `HashMap` should be generarized over different hashers
2622
|
2723
help: consider adding a type parameter
2824
|
29-
20 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<i8> for (HashMap<K, V>,) {
25+
20 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<i8> for (HashMap<K, V, S>,) {
3026
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31-
help: ...and change the type to
32-
|
33-
20 | impl<K: Hash + Eq, V> Foo<i8> for (HashMap<K, V, S>,) {
34-
| ^^^^^^^^^^^^^^^^
3527
help: ...and use generic constructor
3628
|
3729
22 | ((HashMap::default(),), (HashMap::with_capacity_and_hasher(10, Default::default()),))
@@ -45,12 +37,8 @@ error: impl for `HashMap` should be generarized over different hashers
4537
|
4638
help: consider adding a type parameter
4739
|
48-
25 | impl<S: ::std::hash::BuildHasher + Default> Foo<i16> for HashMap<String, String> {
40+
25 | impl<S: ::std::hash::BuildHasher + Default> Foo<i16> for HashMap<String, String, S> {
4941
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50-
help: ...and change the type to
51-
|
52-
25 | impl Foo<i16> for HashMap<String, String, S> {
53-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5442
help: ...and use generic constructor
5543
|
5644
27 | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default::default()))
@@ -64,12 +52,8 @@ error: impl for `HashSet` should be generarized over different hashers
6452
|
6553
help: consider adding a type parameter
6654
|
67-
43 | impl<T: Hash + Eq, S: ::std::hash::BuildHasher + Default> Foo<i8> for HashSet<T> {
55+
43 | impl<T: Hash + Eq, S: ::std::hash::BuildHasher + Default> Foo<i8> for HashSet<T, S> {
6856
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
69-
help: ...and change the type to
70-
|
71-
43 | impl<T: Hash + Eq> Foo<i8> for HashSet<T, S> {
72-
| ^^^^^^^^^^^^^
7357
help: ...and use generic constructor
7458
|
7559
45 | (HashSet::default(), HashSet::with_capacity_and_hasher(10, Default::default()))
@@ -83,12 +67,8 @@ error: impl for `HashSet` should be generarized over different hashers
8367
|
8468
help: consider adding a type parameter
8569
|
86-
48 | impl<S: ::std::hash::BuildHasher + Default> Foo<i16> for HashSet<String> {
70+
48 | impl<S: ::std::hash::BuildHasher + Default> Foo<i16> for HashSet<String, S> {
8771
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88-
help: ...and change the type to
89-
|
90-
48 | impl Foo<i16> for HashSet<String, S> {
91-
| ^^^^^^^^^^^^^^^^^^
9272
help: ...and use generic constructor
9373
|
9474
50 | (HashSet::default(), HashSet::with_capacity_and_hasher(10, Default::default()))
@@ -102,12 +82,8 @@ error: parameter of type `HashMap` should be generarized over different hashers
10282
|
10383
help: consider adding a type parameter
10484
|
105-
65 | pub fn foo<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32>) {
85+
65 | pub fn foo<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i32, S>, _set: &mut HashSet<i32>) {
10686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
107-
help: ...and change the type to
108-
|
109-
65 | pub fn foo(_map: &mut HashMap<i32, i32, S>, _set: &mut HashSet<i32>) {
110-
| ^^^^^^^^^^^^^^^^^^^^
11187

11288
error: parameter of type `HashSet` should be generarized over different hashers
11389
--> $DIR/implicit_hasher.rs:65:53
@@ -117,12 +93,8 @@ error: parameter of type `HashSet` should be generarized over different hashers
11793
|
11894
help: consider adding a type parameter
11995
|
120-
65 | pub fn foo<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32>) {
96+
65 | pub fn foo<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32, S>) {
12197
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
122-
help: ...and change the type to
123-
|
124-
65 | pub fn foo(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32, S>) {
125-
| ^^^^^^^^^^^^^^^
12698

12799
error: impl for `HashMap` should be generarized over different hashers
128100
--> $DIR/implicit_hasher.rs:70:43
@@ -135,12 +107,8 @@ error: impl for `HashMap` should be generarized over different hashers
135107
|
136108
help: consider adding a type parameter
137109
|
138-
70 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<u8> for HashMap<K, V> {
110+
70 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<u8> for HashMap<K, V, S> {
139111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140-
help: ...and change the type to
141-
|
142-
70 | impl<K: Hash + Eq, V> Foo<u8> for HashMap<K, V, S> {
143-
| ^^^^^^^^^^^^^^^^
144112
help: ...and use generic constructor
145113
|
146114
72 | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default::default()))
@@ -157,12 +125,8 @@ error: parameter of type `HashMap` should be generarized over different hashers
157125
|
158126
help: consider adding a type parameter
159127
|
160-
78 | pub fn $name<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32>) {
128+
78 | pub fn $name<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i32, S>, _set: &mut HashSet<i32>) {
161129
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
162-
help: ...and change the type to
163-
|
164-
78 | pub fn $name(_map: &mut HashMap<i32, i32, S>, _set: &mut HashSet<i32>) {
165-
| ^^^^^^^^^^^^^^^^^^^^
166130

167131
error: parameter of type `HashSet` should be generarized over different hashers
168132
--> $DIR/implicit_hasher.rs:78:63
@@ -175,10 +139,6 @@ error: parameter of type `HashSet` should be generarized over different hashers
175139
|
176140
help: consider adding a type parameter
177141
|
178-
78 | pub fn $name<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32>) {
142+
78 | pub fn $name<S: ::std::hash::BuildHasher>(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32, S>) {
179143
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
180-
help: ...and change the type to
181-
|
182-
78 | pub fn $name(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32, S>) {
183-
| ^^^^^^^^^^^^^^^
184144

0 commit comments

Comments
 (0)