Avoid storing SymbolStr in a struct.

It's intended only for very temporary use.
This commit is contained in:
Nicholas Nethercote 2020-07-13 16:45:35 +10:00
parent 9f0080801d
commit 002af4d0c7
2 changed files with 9 additions and 8 deletions

View file

@ -8,7 +8,7 @@ use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::symbol::{Ident, SymbolStr}; use rustc_span::symbol::{Ident, Symbol};
use std::cmp::Ordering; use std::cmp::Ordering;
declare_clippy_lint! { declare_clippy_lint! {
@ -75,7 +75,7 @@ pub struct NonExpressiveNames {
impl_lint_pass!(NonExpressiveNames => [SIMILAR_NAMES, MANY_SINGLE_CHAR_NAMES, JUST_UNDERSCORES_AND_DIGITS]); impl_lint_pass!(NonExpressiveNames => [SIMILAR_NAMES, MANY_SINGLE_CHAR_NAMES, JUST_UNDERSCORES_AND_DIGITS]);
struct ExistingName { struct ExistingName {
interned: SymbolStr, interned: Symbol,
span: Span, span: Span,
len: usize, len: usize,
exemptions: &'static [&'static str], exemptions: &'static [&'static str],
@ -218,18 +218,19 @@ impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
let mut split_at = None; let mut split_at = None;
match existing_name.len.cmp(&count) { match existing_name.len.cmp(&count) {
Ordering::Greater => { Ordering::Greater => {
if existing_name.len - count != 1 || levenstein_not_1(&interned_name, &existing_name.interned) { if existing_name.len - count != 1 || levenstein_not_1(&interned_name, &existing_name.interned.as_str()) {
continue; continue;
} }
}, },
Ordering::Less => { Ordering::Less => {
if count - existing_name.len != 1 || levenstein_not_1(&existing_name.interned, &interned_name) { if count - existing_name.len != 1 || levenstein_not_1(&existing_name.interned.as_str(), &interned_name) {
continue; continue;
} }
}, },
Ordering::Equal => { Ordering::Equal => {
let mut interned_chars = interned_name.chars(); let mut interned_chars = interned_name.chars();
let mut existing_chars = existing_name.interned.chars(); let interned_str = existing_name.interned.as_str();
let mut existing_chars = interned_str.chars();
let first_i = interned_chars.next().expect("we know we have at least one char"); let first_i = interned_chars.next().expect("we know we have at least one char");
let first_e = existing_chars.next().expect("we know we have at least one char"); let first_e = existing_chars.next().expect("we know we have at least one char");
let eq_or_numeric = |(a, b): (char, char)| a == b || a.is_numeric() && b.is_numeric(); let eq_or_numeric = |(a, b): (char, char)| a == b || a.is_numeric() && b.is_numeric();
@ -302,7 +303,7 @@ impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
} }
self.0.names.push(ExistingName { self.0.names.push(ExistingName {
exemptions: get_exemptions(&interned_name).unwrap_or(&[]), exemptions: get_exemptions(&interned_name).unwrap_or(&[]),
interned: interned_name, interned: ident.name,
span: ident.span, span: ident.span,
len: count, len: count,
}); });

View file

@ -3,7 +3,7 @@ use rustc_ast::ast::{Item, ItemKind, UseTree, UseTreeKind};
use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::symbol::{Ident, SymbolStr}; use rustc_span::symbol::Ident;
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Checks for imports that remove "unsafe" from an item's /// **What it does:** Checks for imports that remove "unsafe" from an item's
@ -73,6 +73,6 @@ fn unsafe_to_safe_check(old_name: Ident, new_name: Ident, cx: &EarlyContext<'_>,
} }
#[must_use] #[must_use]
fn contains_unsafe(name: &SymbolStr) -> bool { fn contains_unsafe(name: &str) -> bool {
name.contains("Unsafe") || name.contains("unsafe") name.contains("Unsafe") || name.contains("unsafe")
} }