]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/blacklisted_name.rs
Auto merge of #3593 - mikerite:readme-syspath-2, r=phansch
[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
15 /// **What it does:** Checks for usage of blacklisted names for variables, such
16 /// as `foo`.
17 ///
18 /// **Why is this bad?** These names are usually placeholder names and should be
19 /// avoided.
20 ///
21 /// **Known problems:** None.
22 ///
23 /// **Example:**
24 /// ```rust
25 /// let foo = 3.14;
26 /// ```
27 declare_clippy_lint! {
28     pub BLACKLISTED_NAME,
29     style,
30     "usage of a blacklisted/placeholder name"
31 }
32
33 #[derive(Clone, Debug)]
34 pub struct BlackListedName {
35     blacklist: Vec<String>,
36 }
37
38 impl BlackListedName {
39     pub fn new(blacklist: Vec<String>) -> Self {
40         Self { blacklist }
41     }
42 }
43
44 impl LintPass for BlackListedName {
45     fn get_lints(&self) -> LintArray {
46         lint_array!(BLACKLISTED_NAME)
47     }
48 }
49
50 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlackListedName {
51     fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
52         if let PatKind::Binding(_, _, ident, _) = pat.node {
53             if self.blacklist.iter().any(|s| ident.name == *s) {
54                 span_lint(
55                     cx,
56                     BLACKLISTED_NAME,
57                     ident.span,
58                     &format!("use of a blacklisted/placeholder name `{}`", ident.name),
59                 );
60             }
61         }
62     }
63 }