self, Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, Item, ItemKind, Local, Pat, PatKind,
};
use rustc_ast::visit::{walk_block, walk_expr, walk_pat, Visitor};
-use rustc_lint::{EarlyContext, EarlyLintPass};
+use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::Span;
/// ### What it does
/// Checks for names that are very similar and thus confusing.
///
+ /// Note: this lint looks for similar names throughout each
+ /// scope. To allow it, you need to allow it on the scope
+ /// level, not on the name that is reported.
+ ///
/// ### Why is this bad?
/// It's hard to distinguish between names that differ only
/// by a single character.
self.cx,
MANY_SINGLE_CHAR_NAMES,
span,
- &format!(
- "{} bindings with single-character names in scope",
- num_single_char_names
- ),
+ &format!("{num_single_char_names} bindings with single-character names in scope"),
);
}
}
#[must_use]
fn get_exemptions(interned_name: &str) -> Option<&'static [&'static str]> {
- for &list in ALLOWED_TO_BE_SIMILAR {
- if allowed_to_be_similar(interned_name, list) {
- return Some(list);
- }
- }
- None
+ ALLOWED_TO_BE_SIMILAR
+ .iter()
+ .find(|&&list| allowed_to_be_similar(interned_name, list))
+ .copied()
}
#[must_use]
}
}
- #[allow(clippy::too_many_lines)]
+ #[expect(clippy::too_many_lines)]
fn check_ident(&mut self, ident: Ident) {
let interned_name = ident.name.as_str();
if interned_name.chars().any(char::is_uppercase) {
return;
}
- if interned_name.chars().all(|c| c.is_digit(10) || c == '_') {
+ if interned_name.chars().all(|c| c.is_ascii_digit() || c == '_') {
span_lint(
self.0.cx,
JUST_UNDERSCORES_AND_DIGITS,
// add the pattern after the expression because the bindings aren't available
// yet in the init
// expression
- SimilarNamesNameVisitor(self).visit_pat(&*local.pat);
+ SimilarNamesNameVisitor(self).visit_pat(&local.pat);
}
fn visit_block(&mut self, blk: &'tcx Block) {
self.single_char_names.push(vec![]);
impl EarlyLintPass for NonExpressiveNames {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
- if in_external_macro(cx.sess, item.span) {
+ if in_external_macro(cx.sess(), item.span) {
return;
}
}
fn check_impl_item(&mut self, cx: &EarlyContext<'_>, item: &AssocItem) {
- if in_external_macro(cx.sess, item.span) {
+ if in_external_macro(cx.sess(), item.span) {
return;
}