Use rustc_typeck::hir_ty_to_ty

This commit is contained in:
sinkuu 2017-10-11 23:08:36 +09:00
parent 5a61d88fa1
commit 31f16b87b7
3 changed files with 44 additions and 17 deletions
clippy_lints/src
tests/ui

@ -13,6 +13,7 @@
#[macro_use]
extern crate rustc;
extern crate rustc_typeck;
extern crate syntax;
extern crate syntax_pos;

@ -5,6 +5,7 @@ use rustc::hir::intravisit::{walk_body, walk_expr, walk_ty, FnKind, NestedVisito
use rustc::lint::*;
use rustc::ty::{self, Ty, TyCtxt, TypeckTables};
use rustc::ty::subst::Substs;
use rustc_typeck::hir_ty_to_ty;
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::borrow::Cow;
@ -13,8 +14,8 @@ use syntax::attr::IntType;
use syntax::codemap::Span;
use syntax::errors::DiagnosticBuilder;
use utils::{comparisons, higher, in_external_macro, in_macro, last_path_segment, match_def_path, match_path,
multispan_sugg, opt_def_id, snippet, snippet_opt, span_help_and_lint, span_lint,
span_lint_and_sugg, span_lint_and_then, type_size, same_tys};
multispan_sugg, opt_def_id, same_tys, snippet, snippet_opt, span_help_and_lint, span_lint,
span_lint_and_sugg, span_lint_and_then, type_size};
use utils::paths;
/// Handles all the linting of funky types
@ -1459,8 +1460,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidUpcastComparisons {
/// **Why is this bad?** `HashMap` or `HashSet` with custom hashers cannot be
/// used with them.
///
/// **Known problems:** Suggestions for replacing constructors are not always
/// accurate.
/// **Known problems:** Suggestions for replacing constructors contains
/// false-positives. Also applying suggestion can require modification of other
/// pieces of code, possibly including external crates.
///
/// **Example:**
/// ```rust
@ -1620,7 +1622,7 @@ impl<'tcx> ImplicitHasherType<'tcx> {
let params = &path.segments.last().as_ref()?.parameters.as_ref()?.types;
let params_len = params.len();
let ty = cx.tcx.type_of(opt_def_id(path.def)?);
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
if match_path(path, &paths::HASHMAP) && params_len == 2 {
Some(ImplicitHasherType::HashMap(

@ -7,12 +7,16 @@ error: impl for `HashMap` should be generarized over different hashers
= note: `-D implicit-hasher` implied by `-D warnings`
help: consider adding a type parameter
|
11 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher> Foo<i8> for HashMap<K, V> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<i8> for HashMap<K, V> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ...and change the type to
|
11 | impl<K: Hash + Eq, V> Foo<i8> for HashMap<K, V, S> {
| ^^^^^^^^^^^^^^^^
help: ...and use generic constructor
|
17 | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default::default()))
| ^^^^^^^^^^^^^^^^^^
error: impl for `HashMap` should be generarized over different hashers
--> $DIR/implicit_hasher.rs:20:36
@ -22,12 +26,16 @@ error: impl for `HashMap` should be generarized over different hashers
|
help: consider adding a type parameter
|
20 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher> Foo<i8> for (HashMap<K, V>,) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<i8> for (HashMap<K, V>,) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ...and change the type to
|
20 | impl<K: Hash + Eq, V> Foo<i8> for (HashMap<K, V, S>,) {
| ^^^^^^^^^^^^^^^^
help: ...and use generic constructor
|
22 | ((HashMap::default(),), (HashMap::with_capacity_and_hasher(10, Default::default()),))
| ^^^^^^^^^^^^^^^^^^
error: impl for `HashMap` should be generarized over different hashers
--> $DIR/implicit_hasher.rs:25:19
@ -37,12 +45,16 @@ error: impl for `HashMap` should be generarized over different hashers
|
help: consider adding a type parameter
|
25 | impl<S: ::std::hash::BuildHasher> Foo<i16> for HashMap<String, String> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25 | impl<S: ::std::hash::BuildHasher + Default> Foo<i16> for HashMap<String, String> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ...and change the type to
|
25 | impl Foo<i16> for HashMap<String, String, S> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ...and use generic constructor
|
27 | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default::default()))
| ^^^^^^^^^^^^^^^^^^
error: impl for `HashSet` should be generarized over different hashers
--> $DIR/implicit_hasher.rs:43:32
@ -52,12 +64,16 @@ error: impl for `HashSet` should be generarized over different hashers
|
help: consider adding a type parameter
|
43 | impl<T: Hash + Eq, S: ::std::hash::BuildHasher> Foo<i8> for HashSet<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43 | impl<T: Hash + Eq, S: ::std::hash::BuildHasher + Default> Foo<i8> for HashSet<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ...and change the type to
|
43 | impl<T: Hash + Eq> Foo<i8> for HashSet<T, S> {
| ^^^^^^^^^^^^^
help: ...and use generic constructor
|
45 | (HashSet::default(), HashSet::with_capacity_and_hasher(10, Default::default()))
| ^^^^^^^^^^^^^^^^^^
error: impl for `HashSet` should be generarized over different hashers
--> $DIR/implicit_hasher.rs:48:19
@ -67,12 +83,16 @@ error: impl for `HashSet` should be generarized over different hashers
|
help: consider adding a type parameter
|
48 | impl<S: ::std::hash::BuildHasher> Foo<i16> for HashSet<String> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48 | impl<S: ::std::hash::BuildHasher + Default> Foo<i16> for HashSet<String> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ...and change the type to
|
48 | impl Foo<i16> for HashSet<String, S> {
| ^^^^^^^^^^^^^^^^^^
help: ...and use generic constructor
|
50 | (HashSet::default(), HashSet::with_capacity_and_hasher(10, Default::default()))
| ^^^^^^^^^^^^^^^^^^
error: parameter of type `HashMap` should be generarized over different hashers
--> $DIR/implicit_hasher.rs:65:23
@ -115,12 +135,16 @@ error: impl for `HashMap` should be generarized over different hashers
|
help: consider adding a type parameter
|
70 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher> Foo<u8> for HashMap<K, V> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70 | impl<K: Hash + Eq, V, S: ::std::hash::BuildHasher + Default> Foo<u8> for HashMap<K, V> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ...and change the type to
|
70 | impl<K: Hash + Eq, V> Foo<u8> for HashMap<K, V, S> {
| ^^^^^^^^^^^^^^^^
help: ...and use generic constructor
|
72 | (HashMap::default(), HashMap::with_capacity_and_hasher(10, Default::default()))
| ^^^^^^^^^^^^^^^^^^
error: parameter of type `HashMap` should be generarized over different hashers
--> $DIR/implicit_hasher.rs:78:33