Skip to content

Lint disallowed function names #14601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions clippy_lints/src/disallowed_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_in_test;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::{Pat, PatKind};
use rustc_hir::intravisit::FnKind;
use rustc_hir::{HirId, Pat, PatKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::impl_lint_pass;
use rustc_span::Symbol;
use rustc_span::{Ident, Symbol};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -36,16 +37,9 @@ impl DisallowedNames {
disallow: conf.disallowed_names.iter().map(|x| Symbol::intern(x)).collect(),
}
}
}

impl_lint_pass!(DisallowedNames => [DISALLOWED_NAMES]);

impl<'tcx> LateLintPass<'tcx> for DisallowedNames {
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
if let PatKind::Binding(.., ident, _) = pat.kind
&& self.disallow.contains(&ident.name)
&& !is_in_test(cx.tcx, pat.hir_id)
{
fn check(&mut self, cx: &LateContext<'_>, ident: Ident, hir_id: HirId) {
if self.disallow.contains(&ident.name) && !is_in_test(cx.tcx, hir_id) {
span_lint(
cx,
DISALLOWED_NAMES,
Expand All @@ -55,3 +49,33 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedNames {
}
}
}

impl_lint_pass!(DisallowedNames => [DISALLOWED_NAMES]);

impl<'tcx> LateLintPass<'tcx> for DisallowedNames {
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
if let PatKind::Binding(.., ident, _) = pat.kind {
self.check(cx, ident, pat.hir_id);
}
}

fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
kind: FnKind<'tcx>,
_: &'tcx rustc_hir::FnDecl<'tcx>,
_: &'tcx rustc_hir::Body<'tcx>,
_: rustc_span::Span,
_: rustc_span::def_id::LocalDefId,
) {
match kind {
FnKind::ItemFn(ident, ..) => {
self.check(cx, ident, cx.last_node_with_lint_attrs);
},
FnKind::Method(ident, _) => {
self.check(cx, ident, cx.last_node_with_lint_attrs);
},
_ => {},
}
}
}
22 changes: 22 additions & 0 deletions tests/ui/disallowed_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,25 @@ mod tests {
fn test_with_disallowed_name() {
let foo = 0;
}

mod functions_test {
fn foo() {}
//~^ disallowed_names

pub fn quux(_some_meaningful_arg: i32) {}
//~^ disallowed_names

pub async fn baz(_more_meaningful_arg: bool) {}
//~^ disallowed_names

fn do_not_lint_foo() {}

struct SomeMeaningfulStruct {}
impl SomeMeaningfulStruct {
fn foo(&self) {}
//~^ disallowed_names

const fn baz(&self) {}
//~^ disallowed_names
}
}
32 changes: 31 additions & 1 deletion tests/ui/disallowed_names.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,35 @@ error: use of a disallowed/placeholder name `quux`
LL | if let Some(ref mut quux) = Some(42) {}
| ^^^^

error: aborting due to 14 previous errors
error: use of a disallowed/placeholder name `foo`
--> tests/ui/disallowed_names.rs:88:8
|
LL | fn foo() {}
| ^^^

error: use of a disallowed/placeholder name `quux`
--> tests/ui/disallowed_names.rs:91:12
|
LL | pub fn quux(_some_meaningful_arg: i32) {}
| ^^^^

error: use of a disallowed/placeholder name `baz`
--> tests/ui/disallowed_names.rs:94:18
|
LL | pub async fn baz(_more_meaningful_arg: bool) {}
| ^^^

error: use of a disallowed/placeholder name `foo`
--> tests/ui/disallowed_names.rs:101:12
|
LL | fn foo(&self) {}
| ^^^

error: use of a disallowed/placeholder name `baz`
--> tests/ui/disallowed_names.rs:104:18
|
LL | const fn baz(&self) {}
| ^^^

error: aborting due to 19 previous errors

Loading