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