Skip to content

Commit bc0b47c

Browse files
committed
New internal lint: unusual_names
This lint aims at detecting unusual names used in Clippy source codes, such as `appl` or `application` for a `rustc_errors::Applicability` variable, while `app` and `applicability` are commonly used throughout Clippy. This helps maintaining consistency.
1 parent 1ee4fcb commit bc0b47c

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

clippy_lints_internal/src/internal_paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use clippy_utils::paths::{PathLookup, PathNS};
22
use clippy_utils::{sym, type_path, value_path};
33

44
// Paths inside rustc
5+
pub static APPLICABILITY: PathLookup = type_path!(rustc_errors::Applicability);
56
pub static EARLY_LINT_PASS: PathLookup = type_path!(rustc_lint::passes::EarlyLintPass);
67
pub static KW_MODULE: PathLookup = type_path!(rustc_span::symbol::kw);
78
pub static LINT: PathLookup = type_path!(rustc_lint_defs::Lint);

clippy_lints_internal/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod produce_ice;
4141
mod symbols;
4242
mod unnecessary_def_path;
4343
mod unsorted_clippy_utils_paths;
44+
mod unusual_names;
4445

4546
use rustc_lint::{Lint, LintStore};
4647

@@ -59,6 +60,7 @@ static LINTS: &[&Lint] = &[
5960
symbols::SYMBOL_AS_STR,
6061
unnecessary_def_path::UNNECESSARY_DEF_PATH,
6162
unsorted_clippy_utils_paths::UNSORTED_CLIPPY_UTILS_PATHS,
63+
unusual_names::UNUSUAL_NAMES,
6264
];
6365

6466
pub fn register_lints(store: &mut LintStore) {
@@ -74,4 +76,5 @@ pub fn register_lints(store: &mut LintStore) {
7476
store.register_late_pass(|_| Box::new(outer_expn_data_pass::OuterExpnDataPass));
7577
store.register_late_pass(|_| Box::new(msrv_attr_impl::MsrvAttrImpl));
7678
store.register_late_pass(|_| Box::new(almost_standard_lint_formulation::AlmostStandardFormulation::new()));
79+
store.register_late_pass(|_| Box::new(unusual_names::UnusualNames));
7780
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::sym;
3+
use rustc_hir::{PatKind, Stmt, StmtKind};
4+
use rustc_lint::{LateContext, LateLintPass};
5+
use rustc_session::{declare_lint_pass, declare_tool_lint};
6+
7+
use crate::internal_paths::APPLICABILITY;
8+
9+
declare_tool_lint! {
10+
/// ### What it does
11+
/// Checks if variables of some types use the usual name.
12+
///
13+
/// ### Why is this bad?
14+
/// Restricting the identifiers used for common things in
15+
/// Clippy sources increases consistency.
16+
///
17+
/// ### Example
18+
/// Check that an `rustc_errors::Applicability` variable is
19+
/// named either `app` or `applicability`, and not
20+
/// `a` or `appl`.
21+
pub clippy::UNUSUAL_NAMES,
22+
Warn,
23+
"commonly used concepts should use usual same variable name.",
24+
report_in_external_macro: true
25+
}
26+
27+
declare_lint_pass!(UnusualNames => [UNUSUAL_NAMES]);
28+
29+
impl<'tcx> LateLintPass<'tcx> for UnusualNames {
30+
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
31+
if let StmtKind::Let(let_stmt) = stmt.kind
32+
&& let PatKind::Binding(_, _, ident, _) = let_stmt.pat.kind
33+
&& let Some(init_expr) = let_stmt.init
34+
&& !matches!(ident.name, sym::app | sym::applicability)
35+
&& APPLICABILITY.matches_ty(cx, cx.typeck_results().expr_ty(init_expr))
36+
{
37+
span_lint_and_help(
38+
cx,
39+
UNUSUAL_NAMES,
40+
ident.span,
41+
"unusual name for a variable of type `rustc_errors::Applicability`",
42+
None,
43+
"consider using `app` or `applicability`",
44+
);
45+
}
46+
}
47+
}

clippy_utils/src/sym.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ macro_rules! generate {
3434
//
3535
// `cargo dev fmt` ensures that the content of the `generate!()` macro call stays sorted.
3636
generate! {
37+
Applicability,
3738
AsyncReadExt,
3839
AsyncWriteExt,
3940
BACKSLASH_SINGLE_QUOTE: r"\'",
@@ -75,7 +76,9 @@ generate! {
7576
Weak,
7677
abs,
7778
ambiguous_glob_reexports,
79+
app,
7880
append,
81+
applicability,
7982
arg,
8083
as_bytes,
8184
as_deref,
@@ -285,6 +288,7 @@ generate! {
285288
rsplit_terminator,
286289
rsplitn,
287290
rsplitn_mut,
291+
rustc_errors,
288292
rustc_lint,
289293
rustc_lint_defs,
290294
rustc_span,

0 commit comments

Comments
 (0)