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}
;
8 /// **What it does:** Checks for usage of blacklisted names for variables, such
11 /// **Why is this bad?** These names are usually placeholder names and should be
14 /// **Known problems:** None.
22 "usage of a blacklisted/placeholder name"
25 #[derive(Clone, Debug)]
26 pub struct BlacklistedName
{
27 blacklist
: FxHashSet
<String
>,
28 test_modules_deep
: u32,
31 impl BlacklistedName
{
32 pub fn new(blacklist
: FxHashSet
<String
>) -> Self {
39 fn in_test_module(&self) -> bool
{
40 self.test_modules_deep
!= 0
44 impl_lint_pass
!(BlacklistedName
=> [BLACKLISTED_NAME
]);
46 impl<'tcx
> LateLintPass
<'tcx
> for BlacklistedName
{
47 fn check_item(&mut self, cx
: &LateContext
<'_
>, item
: &Item
<'_
>) {
48 if is_test_module_or_function(cx
.tcx
, item
) {
49 self.test_modules_deep
= self.test_modules_deep
.saturating_add(1);
53 fn check_pat(&mut self, cx
: &LateContext
<'tcx
>, pat
: &'tcx Pat
<'_
>) {
54 // Check whether we are under the `test` attribute.
55 if self.in_test_module() {
59 if let PatKind
::Binding(.., ident
, _
) = pat
.kind
{
60 if self.blacklist
.contains(&ident
.name
.to_string()) {
65 &format
!("use of a blacklisted/placeholder name `{}`", ident
.name
),
71 fn check_item_post(&mut self, cx
: &LateContext
<'_
>, item
: &Item
<'_
>) {
72 if is_test_module_or_function(cx
.tcx
, item
) {
73 self.test_modules_deep
= self.test_modules_deep
.saturating_sub(1);