1 use clippy_utils::{diagnostics::span_lint, is_test_module_or_function};
2 use rustc_data_structures::fx::FxHashSet;
3 use rustc_hir::{Item, Pat, PatKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 /// Checks for usage of blacklisted names for variables, such
12 /// ### Why is this bad?
13 /// These names are usually placeholder names and should be
20 #[clippy::version = "pre 1.29.0"]
23 "usage of a blacklisted/placeholder name"
26 #[derive(Clone, Debug)]
27 pub struct BlacklistedName {
28 blacklist: FxHashSet<String>,
29 test_modules_deep: u32,
32 impl BlacklistedName {
33 pub fn new(blacklist: FxHashSet<String>) -> Self {
40 fn in_test_module(&self) -> bool {
41 self.test_modules_deep != 0
45 impl_lint_pass!(BlacklistedName => [BLACKLISTED_NAME]);
47 impl<'tcx> LateLintPass<'tcx> for BlacklistedName {
48 fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
49 if is_test_module_or_function(cx.tcx, item) {
50 self.test_modules_deep = self.test_modules_deep.saturating_add(1);
54 fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
55 // Check whether we are under the `test` attribute.
56 if self.in_test_module() {
60 if let PatKind::Binding(.., ident, _) = pat.kind {
61 if self.blacklist.contains(&ident.name.to_string()) {
66 &format!("use of a blacklisted/placeholder name `{}`", ident.name),
72 fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
73 if is_test_module_or_function(cx.tcx, item) {
74 self.test_modules_deep = self.test_modules_deep.saturating_sub(1);