-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-use crate::utils::span_lint;
-use rustc::hir::*;
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
-
-/// **What it does:** Checks for usage of blacklisted names for variables, such
-/// as `foo`.
-///
-/// **Why is this bad?** These names are usually placeholder names and should be
-/// avoided.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// let foo = 3.14;
-/// ```
+use clippy_utils::{diagnostics::span_lint, is_test_module_or_function};
+use rustc_data_structures::fx::FxHashSet;
+use rustc_hir::{Item, Pat, PatKind};
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_tool_lint, impl_lint_pass};
+
declare_clippy_lint! {
+ /// ### What it does
+ /// Checks for usage of blacklisted names for variables, such
+ /// as `foo`.
+ ///
+ /// ### Why is this bad?
+ /// These names are usually placeholder names and should be
+ /// avoided.
+ ///
+ /// ### Example
+ /// ```rust
+ /// let foo = 3.14;
+ /// ```
pub BLACKLISTED_NAME,
style,
"usage of a blacklisted/placeholder name"
}
#[derive(Clone, Debug)]
-pub struct BlackListedName {
- blacklist: Vec<String>,
+pub struct BlacklistedName {
+ blacklist: FxHashSet<String>,
+ test_modules_deep: u32,
}
-impl BlackListedName {
- pub fn new(blacklist: Vec<String>) -> Self {
- Self { blacklist }
+impl BlacklistedName {
+ pub fn new(blacklist: FxHashSet<String>) -> Self {
+ Self {
+ blacklist,
+ test_modules_deep: 0,
+ }
}
-}
-impl LintPass for BlackListedName {
- fn get_lints(&self) -> LintArray {
- lint_array!(BLACKLISTED_NAME)
+ fn in_test_module(&self) -> bool {
+ self.test_modules_deep != 0
}
}
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlackListedName {
- fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
- if let PatKind::Binding(_, _, ident, _) = pat.node {
- if self.blacklist.iter().any(|s| ident.name == *s) {
+impl_lint_pass!(BlacklistedName => [BLACKLISTED_NAME]);
+
+impl<'tcx> LateLintPass<'tcx> for BlacklistedName {
+ fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
+ if is_test_module_or_function(cx.tcx, item) {
+ self.test_modules_deep = self.test_modules_deep.saturating_add(1);
+ }
+ }
+
+ fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
+ // Check whether we are under the `test` attribute.
+ if self.in_test_module() {
+ return;
+ }
+
+ if let PatKind::Binding(.., ident, _) = pat.kind {
+ if self.blacklist.contains(&ident.name.to_string()) {
span_lint(
cx,
BLACKLISTED_NAME,
}
}
}
+
+ fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
+ if is_test_module_or_function(cx.tcx, item) {
+ self.test_modules_deep = self.test_modules_deep.saturating_sub(1);
+ }
+ }
}