diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index a872ecf3f..f10ad81d1 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -1,3 +1,5 @@ +#![feature(tool_lints)] +#![allow(clippy::default_hash_types)] extern crate regex; #[macro_use] extern crate lazy_static; diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index 8f1823be1..01063d41e 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -2,8 +2,9 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc::ty::Ty; use rustc::hir::*; -use std::collections::HashMap; +use rustc_data_structures::fx::FxHashMap; use std::collections::hash_map::Entry; +use std::hash::BuildHasherDefault; use syntax::symbol::LocalInternedString; use rustc_data_structures::small_vec::OneVector; use crate::utils::{SpanlessEq, SpanlessHash}; @@ -263,8 +264,8 @@ fn if_sequence(mut expr: &Expr) -> (OneVector<&Expr>, OneVector<&Block>) { } /// Return the list of bindings in a pattern. -fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> HashMap> { - fn bindings_impl<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat, map: &mut HashMap>) { +fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> FxHashMap> { + fn bindings_impl<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat, map: &mut FxHashMap>) { match pat.node { PatKind::Box(ref pat) | PatKind::Ref(ref pat, _) => bindings_impl(cx, pat, map), PatKind::TupleStruct(_, ref pats, _) => for pat in pats { @@ -299,7 +300,7 @@ fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> HashMap> = HashMap::with_capacity(exprs.len()); + let mut map: FxHashMap<_, Vec<&_>> = FxHashMap::with_capacity_and_hasher( + exprs.len(), + BuildHasherDefault::default() + ); for expr in exprs { match map.entry(hash(expr)) { diff --git a/clippy_lints/src/functions.rs b/clippy_lints/src/functions.rs index c170b0a1e..ec9aa7a4f 100644 --- a/clippy_lints/src/functions.rs +++ b/clippy_lints/src/functions.rs @@ -5,7 +5,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc::ty; use rustc::hir::def::Def; -use std::collections::HashSet; +use rustc_data_structures::fx::FxHashSet; use syntax::ast; use rustc_target::spec::abi::Abi; use syntax::source_map::Span; @@ -151,7 +151,7 @@ impl<'a, 'tcx> Functions { let raw_ptrs = iter_input_pats(decl, body) .zip(decl.inputs.iter()) .filter_map(|(arg, ty)| raw_ptr_arg(arg, ty)) - .collect::>(); + .collect::>(); if !raw_ptrs.is_empty() { let tables = cx.tcx.body_tables(body.id()); @@ -177,7 +177,7 @@ fn raw_ptr_arg(arg: &hir::Arg, ty: &hir::Ty) -> Option { struct DerefVisitor<'a, 'tcx: 'a> { cx: &'a LateContext<'a, 'tcx>, - ptrs: HashSet, + ptrs: FxHashSet, tables: &'a ty::TypeckTables<'tcx>, } diff --git a/clippy_lints/src/inherent_impl.rs b/clippy_lints/src/inherent_impl.rs index 4ca812e56..a78811c31 100644 --- a/clippy_lints/src/inherent_impl.rs +++ b/clippy_lints/src/inherent_impl.rs @@ -3,7 +3,7 @@ use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; -use std::collections::HashMap; +use rustc_data_structures::fx::FxHashMap; use std::default::Default; use syntax_pos::Span; use crate::utils::span_lint_and_then; @@ -41,12 +41,12 @@ declare_clippy_lint! { } pub struct Pass { - impls: HashMap, + impls: FxHashMap, } impl Default for Pass { fn default() -> Self { - Pass { impls: HashMap::new() } + Pass { impls: FxHashMap::default() } } } diff --git a/clippy_lints/src/len_zero.rs b/clippy_lints/src/len_zero.rs index 4c9c21982..a31add18a 100644 --- a/clippy_lints/src/len_zero.rs +++ b/clippy_lints/src/len_zero.rs @@ -3,7 +3,7 @@ use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc::ty; -use std::collections::HashSet; +use rustc_data_structures::fx::FxHashSet; use syntax::ast::{Lit, LitKind, Name}; use syntax::source_map::{Span, Spanned}; use crate::utils::{get_item_name, in_macro, snippet, span_lint, span_lint_and_sugg, walk_ptrs_ty}; @@ -125,7 +125,7 @@ fn check_trait_items(cx: &LateContext<'_, '_>, visited_trait: &Item, trait_items } // fill the set with current and super traits - fn fill_trait_set(traitt: DefId, set: &mut HashSet, cx: &LateContext<'_, '_>) { + fn fill_trait_set(traitt: DefId, set: &mut FxHashSet, cx: &LateContext<'_, '_>) { if set.insert(traitt) { for supertrait in ::rustc::traits::supertrait_def_ids(cx.tcx, traitt) { fill_trait_set(supertrait, set, cx); @@ -134,7 +134,7 @@ fn check_trait_items(cx: &LateContext<'_, '_>, visited_trait: &Item, trait_items } if cx.access_levels.is_exported(visited_trait.id) && trait_items.iter().any(|i| is_named_self(cx, i, "len")) { - let mut current_and_super_traits = HashSet::new(); + let mut current_and_super_traits = FxHashSet::default(); let visited_trait_def_id = cx.tcx.hir.local_def_id(visited_trait.id); fill_trait_set(visited_trait_def_id, &mut current_and_super_traits, cx); diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs index beec9bac9..5b04b8294 100644 --- a/clippy_lints/src/lifetimes.rs +++ b/clippy_lints/src/lifetimes.rs @@ -5,7 +5,7 @@ use rustc::{declare_tool_lint, lint_array}; use rustc::hir::def::Def; use rustc::hir::*; use rustc::hir::intravisit::*; -use std::collections::{HashMap, HashSet}; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use syntax::source_map::Span; use crate::utils::{last_path_segment, span_lint}; use syntax::symbol::keywords; @@ -237,8 +237,8 @@ fn could_use_elision<'a, 'tcx: 'a>( } } -fn allowed_lts_from(named_generics: &[GenericParam]) -> HashSet { - let mut allowed_lts = HashSet::new(); +fn allowed_lts_from(named_generics: &[GenericParam]) -> FxHashSet { + let mut allowed_lts = FxHashSet::default(); for par in named_generics.iter() { if let GenericParamKind::Lifetime { .. } = par.kind { if par.bounds.is_empty() { @@ -263,7 +263,7 @@ fn lts_from_bounds<'a, T: Iterator>(mut vec: Vec, bo /// Number of unique lifetimes in the given vector. fn unique_lifetimes(lts: &[RefLt]) -> usize { - lts.iter().collect::>().len() + lts.iter().collect::>().len() } /// A visitor usable for `rustc_front::visit::walk_ty()`. @@ -424,7 +424,7 @@ fn has_where_lifetimes<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, where_clause: & } struct LifetimeChecker { - map: HashMap, + map: FxHashMap, } impl<'tcx> Visitor<'tcx> for LifetimeChecker { diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 6d9519911..724081964 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -15,7 +15,7 @@ use rustc::middle::mem_categorization::cmt_; use rustc::ty::{self, Ty}; use rustc::ty::subst::Subst; use rustc_errors::Applicability; -use std::collections::{HashMap, HashSet}; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use std::iter::{once, Iterator}; use syntax::ast; use syntax::source_map::Span; @@ -1030,10 +1030,10 @@ fn check_for_loop_range<'a, 'tcx>( let mut visitor = VarVisitor { cx, var: canonical_id, - indexed_mut: HashSet::new(), - indexed_indirectly: HashMap::new(), - indexed_directly: HashMap::new(), - referenced: HashSet::new(), + indexed_mut: FxHashSet::default(), + indexed_indirectly: FxHashMap::default(), + indexed_directly: FxHashMap::default(), + referenced: FxHashSet::default(), nonindex: false, prefer_mutable: false, }; @@ -1343,7 +1343,7 @@ fn check_for_loop_explicit_counter<'a, 'tcx>( // Look for variables that are incremented once per loop iteration. let mut visitor = IncrementVisitor { cx, - states: HashMap::new(), + states: FxHashMap::default(), depth: 0, done: false, }; @@ -1618,15 +1618,15 @@ struct VarVisitor<'a, 'tcx: 'a> { /// var name to look for as index var: ast::NodeId, /// indexed variables that are used mutably - indexed_mut: HashSet, + indexed_mut: FxHashSet, /// indirectly indexed variables (`v[(i + 4) % N]`), the extend is `None` for global - indexed_indirectly: HashMap>, + indexed_indirectly: FxHashMap>, /// subset of `indexed` of vars that are indexed directly: `v[i]` /// this will not contain cases like `v[calc_index(i)]` or `v[(i + 4) % N]` - indexed_directly: HashMap>, + indexed_directly: FxHashMap>, /// Any names that are used outside an index operation. /// Used to detect things like `&mut vec` used together with `vec[i]` - referenced: HashSet, + referenced: FxHashSet, /// has the loop variable been used in expressions other than the index of /// an index op? nonindex: bool, @@ -1906,7 +1906,7 @@ enum VarState { /// Scan a for loop for variables that are incremented exactly once. struct IncrementVisitor<'a, 'tcx: 'a> { cx: &'a LateContext<'a, 'tcx>, // context reference - states: HashMap, // incremented variables + states: FxHashMap, // incremented variables depth: u32, // depth of conditional expressions done: bool, } @@ -2197,8 +2197,8 @@ fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, e let mut var_visitor = VarCollectorVisitor { cx, - ids: HashSet::new(), - def_ids: HashMap::new(), + ids: FxHashSet::default(), + def_ids: FxHashMap::default(), skip: false, }; var_visitor.visit_expr(cond); @@ -2228,8 +2228,8 @@ fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, e /// All variables definition IDs are collected struct VarCollectorVisitor<'a, 'tcx: 'a> { cx: &'a LateContext<'a, 'tcx>, - ids: HashSet, - def_ids: HashMap, + ids: FxHashSet, + def_ids: FxHashMap, skip: bool, } diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index c00b3d93c..35a232f7f 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -1,7 +1,7 @@ use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass, LintContext, in_external_macro}; use rustc::{declare_tool_lint, lint_array}; +use rustc_data_structures::fx::FxHashMap; use if_chain::if_chain; -use std::collections::HashMap; use std::char; use syntax::ast::*; use syntax::source_map::Span; @@ -267,7 +267,7 @@ impl EarlyLintPass for MiscEarly { } fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, decl: &FnDecl, _: Span, _: NodeId) { - let mut registered_names: HashMap = HashMap::new(); + let mut registered_names: FxHashMap = FxHashMap::default(); for arg in &decl.inputs { if let PatKind::Ident(_, ident, None) = arg.pat.node { diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 340fa4d0e..76eaf0dba 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -9,13 +9,13 @@ use rustc::traits; use rustc::middle::expr_use_visitor as euv; use rustc::middle::mem_categorization as mc; use rustc_target::spec::abi::Abi; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use syntax::ast::NodeId; use syntax_pos::Span; use syntax::errors::DiagnosticBuilder; use crate::utils::{get_trait_def_id, implements_trait, in_macro, is_copy, is_self, match_type, multispan_sugg, paths, snippet, snippet_opt, span_lint_and_then}; use crate::utils::ptr::get_spans; -use std::collections::{HashMap, HashSet}; use std::borrow::Cow; /// **What it does:** Checks for functions taking arguments by value, but not @@ -301,18 +301,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { struct MovedVariablesCtxt<'a, 'tcx: 'a> { cx: &'a LateContext<'a, 'tcx>, - moved_vars: HashSet, + moved_vars: FxHashSet, /// Spans which need to be prefixed with `*` for dereferencing the /// suggested additional reference. - spans_need_deref: HashMap>, + spans_need_deref: FxHashMap>, } impl<'a, 'tcx> MovedVariablesCtxt<'a, 'tcx> { fn new(cx: &'a LateContext<'a, 'tcx>) -> Self { Self { cx, - moved_vars: HashSet::new(), - spans_need_deref: HashMap::new(), + moved_vars: FxHashSet::default(), + spans_need_deref: FxHashMap::default(), } } @@ -344,7 +344,7 @@ impl<'a, 'tcx> MovedVariablesCtxt<'a, 'tcx> { if let ExprKind::Match(ref c, ..) = e.node { self.spans_need_deref .entry(vid) - .or_insert_with(HashSet::new) + .or_insert_with(FxHashSet::default) .insert(c.span); } }, @@ -357,7 +357,7 @@ impl<'a, 'tcx> MovedVariablesCtxt<'a, 'tcx> { then { self.spans_need_deref .entry(vid) - .or_insert_with(HashSet::new) + .or_insert_with(FxHashSet::default) .insert(local.init .as_ref() .map(|e| e.span) diff --git a/clippy_lints/src/regex.rs b/clippy_lints/src/regex.rs index 6ac40c5a1..b7409bfbc 100644 --- a/clippy_lints/src/regex.rs +++ b/clippy_lints/src/regex.rs @@ -2,8 +2,8 @@ use regex_syntax; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; +use rustc_data_structures::fx::FxHashSet; use if_chain::if_chain; -use std::collections::HashSet; use syntax::ast::{LitKind, NodeId, StrStyle}; use syntax::source_map::{BytePos, Span}; use crate::utils::{is_expn_of, match_def_path, match_type, opt_def_id, paths, span_help_and_lint, span_lint}; @@ -67,7 +67,7 @@ declare_clippy_lint! { #[derive(Clone, Default)] pub struct Pass { - spans: HashSet, + spans: FxHashSet, last: Option, } diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index cc04eb8ee..c0369df50 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -1,3 +1,5 @@ +#![allow(clippy::default_hash_types)] + use crate::reexport::*; use rustc::hir; use rustc::hir::*; diff --git a/clippy_lints/src/unused_label.rs b/clippy_lints/src/unused_label.rs index 71d9520c0..ababaad29 100644 --- a/clippy_lints/src/unused_label.rs +++ b/clippy_lints/src/unused_label.rs @@ -2,7 +2,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc::hir; use rustc::hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor}; -use std::collections::HashMap; +use rustc_data_structures::fx::FxHashMap; use syntax::ast; use syntax::source_map::Span; use syntax::symbol::LocalInternedString; @@ -31,7 +31,7 @@ declare_clippy_lint! { pub struct UnusedLabel; struct UnusedLabelVisitor<'a, 'tcx: 'a> { - labels: HashMap, + labels: FxHashMap, cx: &'a LateContext<'a, 'tcx>, } @@ -57,7 +57,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedLabel { let mut v = UnusedLabelVisitor { cx, - labels: HashMap::new(), + labels: FxHashMap::default(), }; walk_fn(&mut v, kind, decl, body.id(), span, fn_id); diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/author.rs index e2c809c0c..541d5353d 100644 --- a/clippy_lints/src/utils/author.rs +++ b/clippy_lints/src/utils/author.rs @@ -8,8 +8,8 @@ use rustc::{declare_tool_lint, lint_array}; use rustc::hir; use rustc::hir::{Expr, ExprKind, QPath, TyKind, Pat, PatKind, BindingAnnotation, StmtKind, DeclKind, Stmt}; use rustc::hir::intravisit::{NestedVisitorMap, Visitor}; +use rustc_data_structures::fx::FxHashMap; use syntax::ast::{Attribute, LitKind, DUMMY_NODE_ID}; -use std::collections::HashMap; use crate::utils::get_attr; /// **What it does:** Generates clippy code that detects the offending pattern @@ -154,7 +154,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { impl PrintVisitor { fn new(s: &'static str) -> Self { Self { - ids: HashMap::new(), + ids: FxHashMap::default(), current: s.to_owned(), } } @@ -186,7 +186,7 @@ impl PrintVisitor { struct PrintVisitor { /// Fields are the current index that needs to be appended to pattern /// binding names - ids: HashMap<&'static str, usize>, + ids: FxHashMap<&'static str, usize>, /// the name that needs to be destructured current: String, } diff --git a/clippy_lints/src/utils/internal_lints.rs b/clippy_lints/src/utils/internal_lints.rs index 7b2d2f159..f3b915c7c 100644 --- a/clippy_lints/src/utils/internal_lints.rs +++ b/clippy_lints/src/utils/internal_lints.rs @@ -3,12 +3,11 @@ use rustc::{declare_tool_lint, lint_array}; use rustc::hir::*; use rustc::hir; use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; -use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use crate::utils::{match_qpath, paths, span_lint, span_lint_and_sugg}; use syntax::symbol::LocalInternedString; use syntax::ast::{Crate as AstCrate, Ident, ItemKind, Name}; use syntax::source_map::Span; -use std::collections::{HashMap, HashSet}; /// **What it does:** Checks for various things we like to keep tidy in clippy. @@ -114,12 +113,10 @@ impl EarlyLintPass for Clippy { } } - - #[derive(Clone, Debug, Default)] pub struct LintWithoutLintPass { - declared_lints: HashMap, - registered_lints: HashSet, + declared_lints: FxHashMap, + registered_lints: FxHashSet, } @@ -129,7 +126,6 @@ impl LintPass for LintWithoutLintPass { } } - impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) { if let hir::ItemKind::Static(ref ty, MutImmutable, body_id) = item.node { @@ -202,7 +198,7 @@ fn is_lint_array_type(ty: &Ty) -> bool { } struct LintCollector<'a, 'tcx: 'a> { - output: &'a mut HashSet, + output: &'a mut FxHashSet, cx: &'a LateContext<'a, 'tcx>, } @@ -221,8 +217,6 @@ impl<'a, 'tcx: 'a> Visitor<'tcx> for LintCollector<'a, 'tcx> { } } - - pub struct DefaultHashTypes { map: FxHashMap, } diff --git a/clippy_lints/src/utils/usage.rs b/clippy_lints/src/utils/usage.rs index 2661bc945..ac18d04e4 100644 --- a/clippy_lints/src/utils/usage.rs +++ b/clippy_lints/src/utils/usage.rs @@ -6,14 +6,14 @@ use rustc::middle::expr_use_visitor::*; use rustc::middle::mem_categorization::cmt_; use rustc::middle::mem_categorization::Categorization; use rustc::ty; -use std::collections::HashSet; +use rustc_data_structures::fx::FxHashSet; use syntax::ast::NodeId; use syntax::source_map::Span; /// Returns a set of mutated local variable ids or None if mutations could not be determined. -pub fn mutated_variables<'a, 'tcx: 'a>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tcx>) -> Option> { +pub fn mutated_variables<'a, 'tcx: 'a>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tcx>) -> Option> { let mut delegate = MutVarsDelegate { - used_mutably: HashSet::new(), + used_mutably: FxHashSet::default(), skip: false, }; let def_id = def_id::DefId::local(expr.hir_id.owner); @@ -39,7 +39,7 @@ pub fn is_potentially_mutated<'a, 'tcx: 'a>( } struct MutVarsDelegate { - used_mutably: HashSet, + used_mutably: FxHashSet, skip: bool, }