Skip to content

Commit 31f16b8

Browse files
committed
Use rustc_typeck::hir_ty_to_ty
1 parent 5a61d88 commit 31f16b8

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#[macro_use]
1515
extern crate rustc;
16+
extern crate rustc_typeck;
1617
extern crate syntax;
1718
extern crate syntax_pos;
1819

clippy_lints/src/types.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc::hir::intravisit::{walk_body, walk_expr, walk_ty, FnKind, NestedVisito
55
use rustc::lint::*;
66
use rustc::ty::{self, Ty, TyCtxt, TypeckTables};
77
use rustc::ty::subst::Substs;
8+
use rustc_typeck::hir_ty_to_ty;
89
use std::cmp::Ordering;
910
use std::collections::BTreeMap;
1011
use std::borrow::Cow;
@@ -13,8 +14,8 @@ use syntax::attr::IntType;
1314
use syntax::codemap::Span;
1415
use syntax::errors::DiagnosticBuilder;
1516
use utils::{comparisons, higher, in_external_macro, in_macro, last_path_segment, match_def_path, match_path,
16-
multispan_sugg, opt_def_id, snippet, snippet_opt, span_help_and_lint, span_lint,
17-
span_lint_and_sugg, span_lint_and_then, type_size, same_tys};
17+
multispan_sugg, opt_def_id, same_tys, snippet, snippet_opt, span_help_and_lint, span_lint,
18+
span_lint_and_sugg, span_lint_and_then, type_size};
1819
use utils::paths;
1920

2021
/// Handles all the linting of funky types
@@ -1459,8 +1460,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidUpcastComparisons {
14591460
/// **Why is this bad?** `HashMap` or `HashSet` with custom hashers cannot be
14601461
/// used with them.
14611462
///
1462-
/// **Known problems:** Suggestions for replacing constructors are not always
1463-
/// accurate.
1463+
/// **Known problems:** Suggestions for replacing constructors contains
1464+
/// false-positives. Also applying suggestion can require modification of other
1465+
/// pieces of code, possibly including external crates.
14641466
///
14651467
/// **Example:**
14661468
/// ```rust
@@ -1620,7 +1622,7 @@ impl<'tcx> ImplicitHasherType<'tcx> {
16201622
let params = &path.segments.last().as_ref()?.parameters.as_ref()?.types;
16211623
let params_len = params.len();
16221624

1623-
let ty = cx.tcx.type_of(opt_def_id(path.def)?);
1625+
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
16241626

16251627
if match_path(path, &paths::HASHMAP) && params_len == 2 {
16261628
Some(ImplicitHasherType::HashMap(

tests/ui/implicit_hasher.stderr

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ 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> Foo<i8> for HashMap<K, V> {
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
11 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<i8> for HashMap<K, V> {
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
help: ...and change the type to
1313
|
1414
11 | impl<K: Hash + Eq, V> Foo<i8> for HashMap<K, V, S> {
1515
| ^^^^^^^^^^^^^^^^
16+
help: ...and use generic constructor
17+
|
18+
17 | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default::default()))
19+
| ^^^^^^^^^^^^^^^^^^
1620

1721
error: impl for `HashMap` should be generarized over different hashers
1822
--> $DIR/implicit_hasher.rs:20:36
@@ -22,12 +26,16 @@ error: impl for `HashMap` should be generarized over different hashers
2226
|
2327
help: consider adding a type parameter
2428
|
25-
20 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher> Foo<i8> for (HashMap<K, V>,) {
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
20 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<i8> for (HashMap<K, V>,) {
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2731
help: ...and change the type to
2832
|
2933
20 | impl<K: Hash + Eq, V> Foo<i8> for (HashMap<K, V, S>,) {
3034
| ^^^^^^^^^^^^^^^^
35+
help: ...and use generic constructor
36+
|
37+
22 | ((HashMap::default(),), (HashMap::with_capacity_and_hasher(10, Default::default()),))
38+
| ^^^^^^^^^^^^^^^^^^
3139

3240
error: impl for `HashMap` should be generarized over different hashers
3341
--> $DIR/implicit_hasher.rs:25:19
@@ -37,12 +45,16 @@ error: impl for `HashMap` should be generarized over different hashers
3745
|
3846
help: consider adding a type parameter
3947
|
40-
25 | impl<S: ::std::hash::BuildHasher> Foo<i16> for HashMap<String, String> {
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
25 | impl<S: ::std::hash::BuildHasher + Default> Foo<i16> for HashMap<String, String> {
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4250
help: ...and change the type to
4351
|
4452
25 | impl Foo<i16> for HashMap<String, String, S> {
4553
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
54+
help: ...and use generic constructor
55+
|
56+
27 | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default::default()))
57+
| ^^^^^^^^^^^^^^^^^^
4658

4759
error: impl for `HashSet` should be generarized over different hashers
4860
--> $DIR/implicit_hasher.rs:43:32
@@ -52,12 +64,16 @@ error: impl for `HashSet` should be generarized over different hashers
5264
|
5365
help: consider adding a type parameter
5466
|
55-
43 | impl<T: Hash + Eq, S: ::std::hash::BuildHasher> Foo<i8> for HashSet<T> {
56-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
43 | impl<T: Hash + Eq, S: ::std::hash::BuildHasher + Default> Foo<i8> for HashSet<T> {
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5769
help: ...and change the type to
5870
|
5971
43 | impl<T: Hash + Eq> Foo<i8> for HashSet<T, S> {
6072
| ^^^^^^^^^^^^^
73+
help: ...and use generic constructor
74+
|
75+
45 | (HashSet::default(), HashSet::with_capacity_and_hasher(10, Default::default()))
76+
| ^^^^^^^^^^^^^^^^^^
6177

6278
error: impl for `HashSet` should be generarized over different hashers
6379
--> $DIR/implicit_hasher.rs:48:19
@@ -67,12 +83,16 @@ error: impl for `HashSet` should be generarized over different hashers
6783
|
6884
help: consider adding a type parameter
6985
|
70-
48 | impl<S: ::std::hash::BuildHasher> Foo<i16> for HashSet<String> {
71-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86+
48 | impl<S: ::std::hash::BuildHasher + Default> Foo<i16> for HashSet<String> {
87+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7288
help: ...and change the type to
7389
|
7490
48 | impl Foo<i16> for HashSet<String, S> {
7591
| ^^^^^^^^^^^^^^^^^^
92+
help: ...and use generic constructor
93+
|
94+
50 | (HashSet::default(), HashSet::with_capacity_and_hasher(10, Default::default()))
95+
| ^^^^^^^^^^^^^^^^^^
7696

7797
error: parameter of type `HashMap` should be generarized over different hashers
7898
--> $DIR/implicit_hasher.rs:65:23
@@ -115,12 +135,16 @@ error: impl for `HashMap` should be generarized over different hashers
115135
|
116136
help: consider adding a type parameter
117137
|
118-
70 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher> Foo<u8> for HashMap<K, V> {
119-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
138+
70 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<u8> for HashMap<K, V> {
139+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120140
help: ...and change the type to
121141
|
122142
70 | impl<K: Hash + Eq, V> Foo<u8> for HashMap<K, V, S> {
123143
| ^^^^^^^^^^^^^^^^
144+
help: ...and use generic constructor
145+
|
146+
72 | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default::default()))
147+
| ^^^^^^^^^^^^^^^^^^
124148

125149
error: parameter of type `HashMap` should be generarized over different hashers
126150
--> $DIR/implicit_hasher.rs:78:33

0 commit comments

Comments
 (0)