]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/blacklisted_name.rs
Auto merge of #3705 - matthiaskrgr:rustup, r=phansch
[rust.git] / clippy_lints / src / blacklisted_name.rs
1 use crate::utils::span_lint;
2 use rustc::hir::*;
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
5 use rustc_data_structures::fx::FxHashSet;
6
7 /// **What it does:** Checks for usage of blacklisted names for variables, such
8 /// as `foo`.
9 ///
10 /// **Why is this bad?** These names are usually placeholder names and should be
11 /// avoided.
12 ///
13 /// **Known problems:** None.
14 ///
15 /// **Example:**
16 /// ```rust
17 /// let foo = 3.14;
18 /// ```
19 declare_clippy_lint! {
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 LintPass for BlackListedName {
37     fn get_lints(&self) -> LintArray {
38         lint_array!(BLACKLISTED_NAME)
39     }
40     fn name(&self) -> &'static str {
41         "BlacklistedName"
42     }
43 }
44
45 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlackListedName {
46     fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
47         if let PatKind::Binding(_, _, ident, _) = pat.node {
48             if self.blacklist.contains(&ident.name.to_string()) {
49                 span_lint(
50                     cx,
51                     BLACKLISTED_NAME,
52                     ident.span,
53                     &format!("use of a blacklisted/placeholder name `{}`", ident.name),
54                 );
55             }
56         }
57     }
58 }