Change Hash{Map, Set} to FxHash{Map, Set}

This commit is contained in:
flip1995 2018-09-12 01:34:52 +02:00
parent cfa3c33b1d
commit e28440d2e0
No known key found for this signature in database
GPG Key ID: ECF9412261FAA470
15 changed files with 67 additions and 65 deletions

View File

@ -1,3 +1,5 @@
#![feature(tool_lints)]
#![allow(clippy::default_hash_types)]
extern crate regex;
#[macro_use]
extern crate lazy_static;

View File

@ -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<LocalInternedString, Ty<'tcx>> {
fn bindings_impl<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat, map: &mut HashMap<LocalInternedString, Ty<'tcx>>) {
fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> FxHashMap<LocalInternedString, Ty<'tcx>> {
fn bindings_impl<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat, map: &mut FxHashMap<LocalInternedString, Ty<'tcx>>) {
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<LocalInt
}
}
let mut result = HashMap::new();
let mut result = FxHashMap::default();
bindings_impl(cx, pat, &mut result);
result
}
@ -333,7 +334,10 @@ where
};
}
let mut map: HashMap<_, Vec<&_>> = 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)) {

View File

@ -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::<HashSet<_>>();
.collect::<FxHashSet<_>>();
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<ast::NodeId> {
struct DerefVisitor<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>,
ptrs: HashSet<ast::NodeId>,
ptrs: FxHashSet<ast::NodeId>,
tables: &'a ty::TypeckTables<'tcx>,
}

View File

@ -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<def_id::DefId, (Span, Generics)>,
impls: FxHashMap<def_id::DefId, (Span, Generics)>,
}
impl Default for Pass {
fn default() -> Self {
Pass { impls: HashMap::new() }
Pass { impls: FxHashMap::default() }
}
}

View File

@ -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<DefId>, cx: &LateContext<'_, '_>) {
fn fill_trait_set(traitt: DefId, set: &mut FxHashSet<DefId>, 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);

View File

@ -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<RefLt> {
let mut allowed_lts = HashSet::new();
fn allowed_lts_from(named_generics: &[GenericParam]) -> FxHashSet<RefLt> {
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<Item = &'a Lifetime>>(mut vec: Vec<RefLt>, bo
/// Number of unique lifetimes in the given vector.
fn unique_lifetimes(lts: &[RefLt]) -> usize {
lts.iter().collect::<HashSet<_>>().len()
lts.iter().collect::<FxHashSet<_>>().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<Name, Span>,
map: FxHashMap<Name, Span>,
}
impl<'tcx> Visitor<'tcx> for LifetimeChecker {

View File

@ -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<Name>,
indexed_mut: FxHashSet<Name>,
/// indirectly indexed variables (`v[(i + 4) % N]`), the extend is `None` for global
indexed_indirectly: HashMap<Name, Option<region::Scope>>,
indexed_indirectly: FxHashMap<Name, Option<region::Scope>>,
/// 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<Name, Option<region::Scope>>,
indexed_directly: FxHashMap<Name, Option<region::Scope>>,
/// Any names that are used outside an index operation.
/// Used to detect things like `&mut vec` used together with `vec[i]`
referenced: HashSet<Name>,
referenced: FxHashSet<Name>,
/// 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<NodeId, VarState>, // incremented variables
states: FxHashMap<NodeId, VarState>, // 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<NodeId>,
def_ids: HashMap<def_id::DefId, bool>,
ids: FxHashSet<NodeId>,
def_ids: FxHashMap<def_id::DefId, bool>,
skip: bool,
}

View File

@ -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<String, Span> = HashMap::new();
let mut registered_names: FxHashMap<String, Span> = FxHashMap::default();
for arg in &decl.inputs {
if let PatKind::Ident(_, ident, None) = arg.pat.node {

View File

@ -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<NodeId>,
moved_vars: FxHashSet<NodeId>,
/// Spans which need to be prefixed with `*` for dereferencing the
/// suggested additional reference.
spans_need_deref: HashMap<NodeId, HashSet<Span>>,
spans_need_deref: FxHashMap<NodeId, FxHashSet<Span>>,
}
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)

View File

@ -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<Span>,
spans: FxHashSet<Span>,
last: Option<NodeId>,
}

View File

@ -1,3 +1,5 @@
#![allow(clippy::default_hash_types)]
use crate::reexport::*;
use rustc::hir;
use rustc::hir::*;

View File

@ -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<LocalInternedString, Span>,
labels: FxHashMap<LocalInternedString, Span>,
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);

View File

@ -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,
}

View File

@ -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<Name, Span>,
registered_lints: HashSet<Name>,
declared_lints: FxHashMap<Name, Span>,
registered_lints: FxHashSet<Name>,
}
@ -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<Name>,
output: &'a mut FxHashSet<Name>,
cx: &'a LateContext<'a, 'tcx>,
}
@ -221,8 +217,6 @@ impl<'a, 'tcx: 'a> Visitor<'tcx> for LintCollector<'a, 'tcx> {
}
}
pub struct DefaultHashTypes {
map: FxHashMap<String, String>,
}

View File

@ -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<HashSet<NodeId>> {
pub fn mutated_variables<'a, 'tcx: 'a>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tcx>) -> Option<FxHashSet<NodeId>> {
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<NodeId>,
used_mutably: FxHashSet<NodeId>,
skip: bool,
}