|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::paths::PathLookup; |
| 3 | +use clippy_utils::sym; |
| 4 | +use itertools::Itertools; |
| 5 | +use rustc_hir::def_id::LocalDefId; |
| 6 | +use rustc_hir::intravisit::FnKind; |
| 7 | +use rustc_hir::{Body, FnDecl, Pat, PatKind, Stmt, StmtKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_middle::ty::Ty; |
| 10 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 11 | +use rustc_span::symbol::kw; |
| 12 | +use rustc_span::{Span, Symbol}; |
| 13 | + |
| 14 | +use crate::internal_paths::{APPLICABILITY, EARLY_CONTEXT, LATE_CONTEXT, TY_CTXT}; |
| 15 | + |
| 16 | +declare_tool_lint! { |
| 17 | + /// ### What it does |
| 18 | + /// Checks if variables of some types use the usual name. |
| 19 | + /// |
| 20 | + /// ### Why is this bad? |
| 21 | + /// Restricting the identifiers used for common things in |
| 22 | + /// Clippy sources increases consistency. |
| 23 | + /// |
| 24 | + /// ### Example |
| 25 | + /// Check that an `rustc_errors::Applicability` variable is |
| 26 | + /// named either `app` or `applicability`, and not |
| 27 | + /// `a` or `appl`. |
| 28 | + pub clippy::UNUSUAL_NAMES, |
| 29 | + Warn, |
| 30 | + "commonly used concepts should use usual same variable name.", |
| 31 | + report_in_external_macro: true |
| 32 | +} |
| 33 | + |
| 34 | +declare_lint_pass!(UnusualNames => [UNUSUAL_NAMES]); |
| 35 | + |
| 36 | +const USUAL_NAMES: [(&PathLookup, &str, &[Symbol]); 4] = [ |
| 37 | + ( |
| 38 | + &APPLICABILITY, |
| 39 | + "rustc_errors::Applicability", |
| 40 | + &[sym::app, sym::applicability], |
| 41 | + ), |
| 42 | + (&EARLY_CONTEXT, "rustc_lint::EarlyContext", &[sym::cx]), |
| 43 | + (&LATE_CONTEXT, "rustc_lint::LateContext", &[sym::cx]), |
| 44 | + (&TY_CTXT, "rustc_middle::ty::TyCtxt", &[sym::tcx]), |
| 45 | +]; |
| 46 | + |
| 47 | +impl<'tcx> LateLintPass<'tcx> for UnusualNames { |
| 48 | + fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) { |
| 49 | + if let StmtKind::Let(let_stmt) = stmt.kind |
| 50 | + && let Some(init_expr) = let_stmt.init |
| 51 | + { |
| 52 | + check_pat_name_for_ty(cx, let_stmt.pat, cx.typeck_results().expr_ty(init_expr), "variable"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + fn check_fn( |
| 57 | + &mut self, |
| 58 | + cx: &LateContext<'tcx>, |
| 59 | + kind: FnKind<'tcx>, |
| 60 | + _decl: &'tcx FnDecl<'_>, |
| 61 | + body: &'tcx Body<'_>, |
| 62 | + _span: Span, |
| 63 | + def_id: LocalDefId, |
| 64 | + ) { |
| 65 | + if matches!(kind, FnKind::Closure) { |
| 66 | + return; |
| 67 | + } |
| 68 | + for (param, ty) in body |
| 69 | + .params |
| 70 | + .iter() |
| 71 | + .zip(cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder().inputs()) |
| 72 | + { |
| 73 | + check_pat_name_for_ty(cx, param.pat, *ty, "parameter"); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +fn check_pat_name_for_ty(cx: &LateContext<'_>, pat: &Pat<'_>, ty: Ty<'_>, kind: &str) { |
| 79 | + if let PatKind::Binding(_, _, ident, _) = pat.kind { |
| 80 | + let ty = ty.peel_refs(); |
| 81 | + for (usual_ty, ty_str, usual_names) in USUAL_NAMES { |
| 82 | + if usual_ty.matches_ty(cx, ty) |
| 83 | + && !usual_names.contains(&ident.name) |
| 84 | + && ident.name != kw::SelfLower |
| 85 | + && !ident.name.as_str().starts_with('_') |
| 86 | + { |
| 87 | + let usual_names = usual_names.iter().map(|name| format!("`{name}`")).join(" or "); |
| 88 | + span_lint_and_help( |
| 89 | + cx, |
| 90 | + UNUSUAL_NAMES, |
| 91 | + ident.span, |
| 92 | + format!("unusual name for a {kind} of type `{ty_str}`"), |
| 93 | + None, |
| 94 | + format!("prefer using {usual_names}"), |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments