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