]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/blacklisted_name.rs
Always include a position span in rustc_parse_format::Argument
[rust.git] / clippy_lints / src / blacklisted_name.rs
1 use clippy_utils::{diagnostics::span_lint, is_test_module_or_function};
2 use rustc_data_structures::fx::FxHashSet;
3 use rustc_hir::{Item, 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
9     /// Checks for usage of blacklisted names for variables, such
10     /// as `foo`.
11     ///
12     /// ### Why is this bad?
13     /// These names are usually placeholder names and should be
14     /// avoided.
15     ///
16     /// ### Example
17     /// ```rust
18     /// let foo = 3.14;
19     /// ```
20     #[clippy::version = "pre 1.29.0"]
21     pub BLACKLISTED_NAME,
22     style,
23     "usage of a blacklisted/placeholder name"
24 }
25
26 #[derive(Clone, Debug)]
27 pub struct BlacklistedName {
28     blacklist: FxHashSet<String>,
29     test_modules_deep: u32,
30 }
31
32 impl BlacklistedName {
33     pub fn new(blacklist: FxHashSet<String>) -> Self {
34         Self {
35             blacklist,
36             test_modules_deep: 0,
37         }
38     }
39
40     fn in_test_module(&self) -> bool {
41         self.test_modules_deep != 0
42     }
43 }
44
45 impl_lint_pass!(BlacklistedName => [BLACKLISTED_NAME]);
46
47 impl<'tcx> LateLintPass<'tcx> for BlacklistedName {
48     fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
49         if is_test_module_or_function(cx.tcx, item) {
50             self.test_modules_deep = self.test_modules_deep.saturating_add(1);
51         }
52     }
53
54     fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
55         // Check whether we are under the `test` attribute.
56         if self.in_test_module() {
57             return;
58         }
59
60         if let PatKind::Binding(.., ident, _) = pat.kind {
61             if self.blacklist.contains(&ident.name.to_string()) {
62                 span_lint(
63                     cx,
64                     BLACKLISTED_NAME,
65                     ident.span,
66                     &format!("use of a blacklisted/placeholder name `{}`", ident.name),
67                 );
68             }
69         }
70     }
71
72     fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
73         if is_test_module_or_function(cx.tcx, item) {
74             self.test_modules_deep = self.test_modules_deep.saturating_sub(1);
75         }
76     }
77 }