]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/blacklisted_name.rs
Use hashset for name blacklist
[rust.git] / clippy_lints / src / blacklisted_name.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::utils::span_lint;
11 use rustc::hir::*;
12 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13 use rustc::{declare_tool_lint, lint_array};
14 use rustc_data_structures::fx::FxHashSet;
15
16 /// **What it does:** Checks for usage of blacklisted names for variables, such
17 /// as `foo`.
18 ///
19 /// **Why is this bad?** These names are usually placeholder names and should be
20 /// avoided.
21 ///
22 /// **Known problems:** None.
23 ///
24 /// **Example:**
25 /// ```rust
26 /// let foo = 3.14;
27 /// ```
28 declare_clippy_lint! {
29     pub BLACKLISTED_NAME,
30     style,
31     "usage of a blacklisted/placeholder name"
32 }
33
34 #[derive(Clone, Debug)]
35 pub struct BlackListedName {
36     blacklist: FxHashSet<String>,
37 }
38
39 impl BlackListedName {
40     pub fn new(blacklist: FxHashSet<String>) -> Self {
41         Self { blacklist }
42     }
43 }
44
45 impl LintPass for BlackListedName {
46     fn get_lints(&self) -> LintArray {
47         lint_array!(BLACKLISTED_NAME)
48     }
49 }
50
51 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlackListedName {
52     fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
53         if let PatKind::Binding(_, _, ident, _) = pat.node {
54             if self.blacklist.contains(&ident.name.to_string()) {
55                 span_lint(
56                     cx,
57                     BLACKLISTED_NAME,
58                     ident.span,
59                     &format!("use of a blacklisted/placeholder name `{}`", ident.name),
60                 );
61             }
62         }
63     }
64 }