]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/blacklisted_name.rs
Auto merge of #3646 - matthiaskrgr:travis, 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 }
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(_, _, ident, _) = pat.node {
45             if self.blacklist.contains(&ident.name.to_string()) {
46                 span_lint(
47                     cx,
48                     BLACKLISTED_NAME,
49                     ident.span,
50                     &format!("use of a blacklisted/placeholder name `{}`", ident.name),
51                 );
52             }
53         }
54     }
55 }