Rustup "Minimize uses of LocalInternedString"

This commit is contained in:
Lzu Tao 2019-09-05 13:56:10 +07:00
parent abbb7ee12f
commit c12b700e87
4 changed files with 20 additions and 28 deletions

View file

@ -319,7 +319,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
let name = meta_item.path.segments.last().unwrap().ident.name; let name = meta_item.path.segments.last().unwrap().ident.name;
if let CheckLintNameResult::Tool(Err((None, _))) = lint_store.check_lint_name( if let CheckLintNameResult::Tool(Err((None, _))) = lint_store.check_lint_name(
&name.as_str(), &name.as_str(),
Some(tool_name.as_str()), Some(tool_name.name),
); );
then { then {
span_lint_and_then( span_lint_and_then(
@ -332,7 +332,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
let name_lower = name.as_str().to_lowercase(); let name_lower = name.as_str().to_lowercase();
match lint_store.check_lint_name( match lint_store.check_lint_name(
&name_lower, &name_lower,
Some(tool_name.as_str()) Some(tool_name.name)
) { ) {
// FIXME: can we suggest similar lint names here? // FIXME: can we suggest similar lint names here?
// https://github.com/rust-lang/rust/pull/56992 // https://github.com/rust-lang/rust/pull/56992

View file

@ -8,7 +8,7 @@ use rustc_data_structures::fx::FxHashMap;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::hash::BuildHasherDefault; use std::hash::BuildHasherDefault;
use syntax::symbol::LocalInternedString; use syntax::symbol::Symbol;
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Checks for consecutive `if`s with the same condition. /// **What it does:** Checks for consecutive `if`s with the same condition.
@ -168,8 +168,8 @@ fn lint_same_cond(cx: &LateContext<'_, '_>, conds: &[&Expr]) {
fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr) { fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr) {
fn same_bindings<'tcx>( fn same_bindings<'tcx>(
cx: &LateContext<'_, 'tcx>, cx: &LateContext<'_, 'tcx>,
lhs: &FxHashMap<LocalInternedString, Ty<'tcx>>, lhs: &FxHashMap<Symbol, Ty<'tcx>>,
rhs: &FxHashMap<LocalInternedString, Ty<'tcx>>, rhs: &FxHashMap<Symbol, Ty<'tcx>>,
) -> bool { ) -> bool {
lhs.len() == rhs.len() lhs.len() == rhs.len()
&& lhs && lhs
@ -275,12 +275,8 @@ fn if_sequence(mut expr: &Expr) -> (SmallVec<[&Expr; 1]>, SmallVec<[&Block; 1]>)
} }
/// Returns the list of bindings in a pattern. /// Returns the list of bindings in a pattern.
fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> FxHashMap<LocalInternedString, Ty<'tcx>> { fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> FxHashMap<Symbol, Ty<'tcx>> {
fn bindings_impl<'a, 'tcx>( fn bindings_impl<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat, map: &mut FxHashMap<Symbol, Ty<'tcx>>) {
cx: &LateContext<'a, 'tcx>,
pat: &Pat,
map: &mut FxHashMap<LocalInternedString, Ty<'tcx>>,
) {
match pat.node { match pat.node {
PatKind::Box(ref pat) | PatKind::Ref(ref pat, _) => bindings_impl(cx, pat, map), PatKind::Box(ref pat) | PatKind::Ref(ref pat, _) => bindings_impl(cx, pat, map),
PatKind::TupleStruct(_, ref pats, _) => { PatKind::TupleStruct(_, ref pats, _) => {
@ -289,7 +285,7 @@ fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> FxHashMap<LocalI
} }
}, },
PatKind::Binding(.., ident, ref as_pat) => { PatKind::Binding(.., ident, ref as_pat) => {
if let Entry::Vacant(v) = map.entry(ident.as_str()) { if let Entry::Vacant(v) = map.entry(ident.name) {
v.insert(cx.tables.pat_ty(pat)); v.insert(cx.tables.pat_ty(pat));
} }
if let Some(ref as_pat) = *as_pat { if let Some(ref as_pat) = *as_pat {

View file

@ -6,7 +6,7 @@ use rustc::lint::{EarlyContext, EarlyLintPass, Lint, LintArray, LintPass};
use rustc::{declare_tool_lint, impl_lint_pass}; use rustc::{declare_tool_lint, impl_lint_pass};
use syntax::ast::*; use syntax::ast::*;
use syntax::source_map::Span; use syntax::source_map::Span;
use syntax::symbol::{InternedString, LocalInternedString}; use syntax::symbol::Symbol;
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Detects enumeration variants that are prefixed or suffixed /// **What it does:** Detects enumeration variants that are prefixed or suffixed
@ -102,7 +102,7 @@ declare_clippy_lint! {
} }
pub struct EnumVariantNames { pub struct EnumVariantNames {
modules: Vec<(InternedString, String)>, modules: Vec<(Symbol, String)>,
threshold: u64, threshold: u64,
} }
@ -122,10 +122,6 @@ impl_lint_pass!(EnumVariantNames => [
MODULE_INCEPTION MODULE_INCEPTION
]); ]);
fn var2str(var: &Variant) -> LocalInternedString {
var.ident.as_str()
}
/// Returns the number of chars that match from the start /// Returns the number of chars that match from the start
fn partial_match(pre: &str, name: &str) -> usize { fn partial_match(pre: &str, name: &str) -> usize {
let mut name_iter = name.chars(); let mut name_iter = name.chars();
@ -157,7 +153,7 @@ fn check_variant(
return; return;
} }
for var in &def.variants { for var in &def.variants {
let name = var2str(var); let name = var.ident.name.as_str();
if partial_match(item_name, &name) == item_name_chars if partial_match(item_name, &name) == item_name_chars
&& name.chars().nth(item_name_chars).map_or(false, |c| !c.is_lowercase()) && name.chars().nth(item_name_chars).map_or(false, |c| !c.is_lowercase())
&& name.chars().nth(item_name_chars + 1).map_or(false, |c| !c.is_numeric()) && name.chars().nth(item_name_chars + 1).map_or(false, |c| !c.is_numeric())
@ -168,11 +164,11 @@ fn check_variant(
span_lint(cx, lint, var.span, "Variant name ends with the enum's name"); span_lint(cx, lint, var.span, "Variant name ends with the enum's name");
} }
} }
let first = var2str(&def.variants[0]); let first = &def.variants[0].ident.name.as_str();
let mut pre = &first[..camel_case::until(&*first)]; let mut pre = &first[..camel_case::until(&*first)];
let mut post = &first[camel_case::from(&*first)..]; let mut post = &first[camel_case::from(&*first)..];
for var in &def.variants { for var in &def.variants {
let name = var2str(var); let name = var.ident.name.as_str();
let pre_match = partial_match(pre, &name); let pre_match = partial_match(pre, &name);
pre = &pre[..pre_match]; pre = &pre[..pre_match];
@ -245,14 +241,14 @@ impl EarlyLintPass for EnumVariantNames {
#[allow(clippy::similar_names)] #[allow(clippy::similar_names)]
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
let item_name = item.ident.as_str(); let item_name = item.ident.name.as_str();
let item_name_chars = item_name.chars().count(); let item_name_chars = item_name.chars().count();
let item_camel = to_camel_case(&item_name); let item_camel = to_camel_case(&item_name);
if !item.span.from_expansion() && is_present_in_source(cx, item.span) { if !item.span.from_expansion() && is_present_in_source(cx, item.span) {
if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() { if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() {
// constants don't have surrounding modules // constants don't have surrounding modules
if !mod_camel.is_empty() { if !mod_camel.is_empty() {
if mod_name.as_symbol() == item.ident.name { if mod_name == &item.ident.name {
if let ItemKind::Mod(..) = item.node { if let ItemKind::Mod(..) = item.node {
span_lint( span_lint(
cx, cx,
@ -299,6 +295,6 @@ impl EarlyLintPass for EnumVariantNames {
}; };
check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span, lint); check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span, lint);
} }
self.modules.push((item_name.as_interned_str(), item_camel)); self.modules.push((item.ident.name, item_camel));
} }
} }

View file

@ -5,7 +5,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint}; use rustc::{declare_lint_pass, declare_tool_lint};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use syntax::source_map::Span; use syntax::source_map::Span;
use syntax::symbol::LocalInternedString; use syntax::symbol::Symbol;
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Checks for unused labels. /// **What it does:** Checks for unused labels.
@ -28,7 +28,7 @@ declare_clippy_lint! {
} }
struct UnusedLabelVisitor<'a, 'tcx> { struct UnusedLabelVisitor<'a, 'tcx> {
labels: FxHashMap<LocalInternedString, Span>, labels: FxHashMap<Symbol, Span>,
cx: &'a LateContext<'a, 'tcx>, cx: &'a LateContext<'a, 'tcx>,
} }
@ -65,11 +65,11 @@ impl<'a, 'tcx> Visitor<'tcx> for UnusedLabelVisitor<'a, 'tcx> {
match expr.node { match expr.node {
hir::ExprKind::Break(destination, _) | hir::ExprKind::Continue(destination) => { hir::ExprKind::Break(destination, _) | hir::ExprKind::Continue(destination) => {
if let Some(label) = destination.label { if let Some(label) = destination.label {
self.labels.remove(&label.ident.as_str()); self.labels.remove(&label.ident.name);
} }
}, },
hir::ExprKind::Loop(_, Some(label), _) => { hir::ExprKind::Loop(_, Some(label), _) => {
self.labels.insert(label.ident.as_str(), expr.span); self.labels.insert(label.ident.name, expr.span);
}, },
_ => (), _ => (),
} }