Skip to content

Emit disallowed_names lint for functions #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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
  •  
  •  
  •  
43 changes: 32 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,30 @@ 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, ..) | FnKind::Method(ident, _) => {
self.check(cx, ident, cx.last_node_with_lint_attrs);
},
FnKind::Closure => {},
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(clippy::await_holding_invalid_type)]
#![allow(clippy::disallowed_names)]
use std::net::Ipv4Addr;

async fn bad() -> u32 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: holding a disallowed type across an await point `std::string::String`
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:5:9
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:6:9
|
LL | let _x = String::from("hello");
| ^^
Expand All @@ -9,13 +9,13 @@ LL | let _x = String::from("hello");
= help: to override `-D warnings` add `#[allow(clippy::await_holding_invalid_type)]`

error: holding a disallowed type across an await point `std::net::Ipv4Addr`
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:11:9
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:12:9
|
LL | let x = Ipv4Addr::new(127, 0, 0, 1);
| ^

error: holding a disallowed type across an await point `std::string::String`
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:35:13
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:36:13
|
LL | let _x = String::from("hi!");
| ^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-toml/dbg_macro/dbg_macro.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@compile-flags: --test
#![warn(clippy::dbg_macro)]
#![allow(clippy::unnecessary_operation, clippy::no_effect)]
#![allow(clippy::unnecessary_operation, clippy::no_effect, clippy::disallowed_names)]

fn foo(n: u32) -> u32 {
if let Some(n) = n.checked_sub(4) { n } else { n }
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-toml/dbg_macro/dbg_macro.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@compile-flags: --test
#![warn(clippy::dbg_macro)]
#![allow(clippy::unnecessary_operation, clippy::no_effect)]
#![allow(clippy::unnecessary_operation, clippy::no_effect, clippy::disallowed_names)]

fn foo(n: u32) -> u32 {
if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![warn(clippy::module_name_repetitions)]
#![allow(dead_code)]
#![allow(dead_code, clippy::disallowed_names)]

pub mod foo {
// this line should produce a warning:
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/print_macro/print_macro.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@compile-flags: --test
#![warn(clippy::print_stdout)]
#![warn(clippy::print_stderr)]
#![allow(clippy::disallowed_names)]

fn foo(n: u32) {
print!("{n}");
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-toml/print_macro/print_macro.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: use of `print!`
--> tests/ui-toml/print_macro/print_macro.rs:6:5
--> tests/ui-toml/print_macro/print_macro.rs:7:5
|
LL | print!("{n}");
| ^^^^^^^^^^^^^
Expand All @@ -8,7 +8,7 @@ LL | print!("{n}");
= help: to override `-D warnings` add `#[allow(clippy::print_stdout)]`

error: use of `eprint!`
--> tests/ui-toml/print_macro/print_macro.rs:8:5
--> tests/ui-toml/print_macro/print_macro.rs:9:5
|
LL | eprint!("{n}");
| ^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/renamed_function_params/default
//@[extend] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/renamed_function_params/extend
#![warn(clippy::renamed_function_params)]
#![allow(clippy::partialeq_ne_impl, clippy::to_string_trait_impl)]
#![allow(clippy::disallowed_names, clippy::partialeq_ne_impl, clippy::to_string_trait_impl)]
#![allow(unused)]

use std::hash::{Hash, Hasher};
Expand Down
Loading