]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/blacklisted_name.rs
Auto merge of #81728 - Qwaz:fix-80335, r=joshtriplett
[rust.git] / src / tools / clippy / clippy_lints / src / blacklisted_name.rs
1 use clippy_utils::diagnostics::span_lint;
2 use rustc_data_structures::fx::FxHashSet;
3 use rustc_hir::{Pat, PatKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_tool_lint, impl_lint_pass};
6
7 declare_clippy_lint! {
8     /// **What it does:** Checks for usage of blacklisted names for variables, such
9     /// as `foo`.
10     ///
11     /// **Why is this bad?** These names are usually placeholder names and should be
12     /// avoided.
13     ///
14     /// **Known problems:** None.
15     ///
16     /// **Example:**
17     /// ```rust
18     /// let foo = 3.14;
19     /// ```
20     pub BLACKLISTED_NAME,
21     style,
22     "usage of a blacklisted/placeholder name"
23 }
24
25 #[derive(Clone, Debug)]
26 pub struct BlacklistedName {
27     blacklist: FxHashSet<String>,
28 }
29
30 impl BlacklistedName {
31     pub fn new(blacklist: FxHashSet<String>) -> Self {
32         Self { blacklist }
33     }
34 }
35
36 impl_lint_pass!(BlacklistedName => [BLACKLISTED_NAME]);
37
38 impl<'tcx> LateLintPass<'tcx> for BlacklistedName {
39     fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
40         if let PatKind::Binding(.., ident, _) = pat.kind {
41             if self.blacklist.contains(&ident.name.to_string()) {
42                 span_lint(
43                     cx,
44                     BLACKLISTED_NAME,
45                     ident.span,
46                     &format!("use of a blacklisted/placeholder name `{}`", ident.name),
47                 );
48             }
49         }
50     }
51 }