]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/blacklisted_name.rs
Merge branch 'master' into typos
[rust.git] / clippy_lints / src / blacklisted_name.rs
1 use rustc::lint::*;
2 use rustc::{declare_lint, lint_array};
3 use rustc::hir::*;
4 use crate::utils::span_lint;
5
6 /// **What it does:** Checks for usage of blacklisted names for variables, such
7 /// as `foo`.
8 ///
9 /// **Why is this bad?** These names are usually placeholder names and should be
10 /// avoided.
11 ///
12 /// **Known problems:** None.
13 ///
14 /// **Example:**
15 /// ```rust
16 /// let foo = 3.14;
17 /// ```
18 declare_clippy_lint! {
19     pub BLACKLISTED_NAME,
20     style,
21     "usage of a blacklisted/placeholder name"
22 }
23
24 #[derive(Clone, Debug)]
25 pub struct BlackListedName {
26     blacklist: Vec<String>,
27 }
28
29 impl BlackListedName {
30     pub fn new(blacklist: Vec<String>) -> Self {
31         Self {
32             blacklist,
33         }
34     }
35 }
36
37 impl LintPass for BlackListedName {
38     fn get_lints(&self) -> LintArray {
39         lint_array!(BLACKLISTED_NAME)
40     }
41 }
42
43 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlackListedName {
44     fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
45         if let PatKind::Binding(_, _, ident, _) = pat.node {
46             if self.blacklist.iter().any(|s| ident.name == *s) {
47                 span_lint(
48                     cx,
49                     BLACKLISTED_NAME,
50                     ident.span,
51                     &format!("use of a blacklisted/placeholder name `{}`", ident.name),
52                 );
53             }
54         }
55     }
56 }