Skip to content

Commit 70ce0c2

Browse files
committed
Remove requirement of fully qualified path for disallowed_method/type
1 parent f1f5ccd commit 70ce0c2

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

clippy_lints/src/disallowed_method.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::fn_def_id;
33

44
use rustc_data_structures::fx::FxHashSet;
5-
use rustc_hir::Expr;
5+
use rustc_hir::{def::Res, def_id::DefId, Crate, Expr};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_tool_lint, impl_lint_pass};
88
use rustc_span::Symbol;
@@ -52,6 +52,7 @@ declare_clippy_lint! {
5252
#[derive(Clone, Debug)]
5353
pub struct DisallowedMethod {
5454
disallowed: FxHashSet<Vec<Symbol>>,
55+
def_ids: FxHashSet<(DefId, Vec<Symbol>)>,
5556
}
5657

5758
impl DisallowedMethod {
@@ -61,17 +62,28 @@ impl DisallowedMethod {
6162
.iter()
6263
.map(|s| s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>())
6364
.collect(),
65+
def_ids: FxHashSet::default(),
6466
}
6567
}
6668
}
6769

6870
impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
6971

7072
impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
73+
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
74+
for path in &self.disallowed {
75+
let segs = path.iter().map(ToString::to_string).collect::<Vec<_>>();
76+
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &segs.iter().map(String::as_str).collect::<Vec<_>>())
77+
{
78+
self.def_ids.insert((id, path.clone()));
79+
}
80+
}
81+
}
82+
7183
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
7284
if let Some(def_id) = fn_def_id(cx, expr) {
73-
let func_path = cx.get_def_path(def_id);
74-
if self.disallowed.contains(&func_path) {
85+
if self.def_ids.iter().any(|(id, _)| def_id == *id) {
86+
let func_path = cx.get_def_path(def_id);
7587
let func_path_string = func_path
7688
.into_iter()
7789
.map(Symbol::to_ident_string)

clippy_lints/src/disallowed_type.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use clippy_utils::diagnostics::span_lint;
22

33
use rustc_data_structures::fx::FxHashSet;
4-
use rustc_hir::{def::Res, Item, ItemKind, PolyTraitRef, TraitBoundModifier, Ty, TyKind, UseKind};
4+
use rustc_hir::{
5+
def::Res, def_id::DefId, Crate, Item, ItemKind, PolyTraitRef, TraitBoundModifier, Ty, TyKind, UseKind,
6+
};
57
use rustc_lint::{LateContext, LateLintPass};
68
use rustc_session::{declare_tool_lint, impl_lint_pass};
79
use rustc_span::{Span, Symbol};
@@ -47,6 +49,7 @@ declare_clippy_lint! {
4749
#[derive(Clone, Debug)]
4850
pub struct DisallowedType {
4951
disallowed: FxHashSet<Vec<Symbol>>,
52+
def_ids: FxHashSet<(DefId, Vec<Symbol>)>,
5053
}
5154

5255
impl DisallowedType {
@@ -56,19 +59,29 @@ impl DisallowedType {
5659
.iter()
5760
.map(|s| s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>())
5861
.collect(),
62+
def_ids: FxHashSet::default(),
5963
}
6064
}
6165
}
6266

6367
impl_lint_pass!(DisallowedType => [DISALLOWED_TYPE]);
6468

6569
impl<'tcx> LateLintPass<'tcx> for DisallowedType {
70+
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
71+
for path in &self.disallowed {
72+
let segs = path.iter().map(ToString::to_string).collect::<Vec<_>>();
73+
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &segs.iter().map(String::as_str).collect::<Vec<_>>())
74+
{
75+
self.def_ids.insert((id, path.clone()));
76+
}
77+
}
78+
}
79+
6680
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
6781
if_chain! {
6882
if let ItemKind::Use(path, UseKind::Single) = &item.kind;
69-
if let Res::Def(_, id) = path.res;
70-
let use_path = cx.get_def_path(id);
71-
if let Some(name) = self.disallowed.iter().find(|path| **path == use_path);
83+
if let Res::Def(_, did) = path.res;
84+
if let Some((_, name)) = self.def_ids.iter().find(|(id, _)| *id == did);
7285
then {
7386
emit(cx, name, item.span,);
7487
}
@@ -79,8 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedType {
7992
if_chain! {
8093
if let TyKind::Path(path) = &ty.kind;
8194
if let Some(did) = cx.qpath_res(path, ty.hir_id).opt_def_id();
82-
let use_path = cx.get_def_path(did);
83-
if let Some(name) = self.disallowed.iter().find(|path| **path == use_path);
95+
if let Some((_, name)) = self.def_ids.iter().find(|(id, _)| *id == did);
8496
then {
8597
emit(cx, name, path.span());
8698
}
@@ -90,8 +102,7 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedType {
90102
fn check_poly_trait_ref(&mut self, cx: &LateContext<'tcx>, poly: &'tcx PolyTraitRef<'tcx>, _: TraitBoundModifier) {
91103
if_chain! {
92104
if let Res::Def(_, did) = poly.trait_ref.path.res;
93-
let use_path = cx.get_def_path(did);
94-
if let Some(name) = self.disallowed.iter().find(|path| **path == use_path);
105+
if let Some((_, name)) = self.def_ids.iter().find(|(id, _)| *id == did);
95106
then {
96107
emit(cx, name, poly.trait_ref.path.span);
97108
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
disallowed-methods = ["core::iter::traits::iterator::Iterator::sum", "regex::re_unicode::Regex::is_match", "regex::re_unicode::Regex::new"]
1+
disallowed-methods = [
2+
"std::iter::Iterator::sum",
3+
"regex::Regex::is_match",
4+
"regex::Regex::new"
5+
]

tests/ui-toml/toml_disallowed_type/clippy.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
disallowed-types = [
2-
"std::collections::hash::map::HashMap",
3-
"core::sync::atomic::AtomicU32",
4-
"syn::ty::TypePath",
2+
"std::collections::HashMap",
3+
"std::sync::atomic::AtomicU32",
4+
"syn::TypePath",
55
"proc_macro2::Ident",
66
"std::thread::Thread",
77
"std::time::Instant",

tests/ui-toml/toml_disallowed_type/conf_disallowed_type.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `core::sync::atomic::AtomicU32` is not allowed according to config
1+
error: `std::sync::atomic::AtomicU32` is not allowed according to config
22
--> $DIR/conf_disallowed_type.rs:7:1
33
|
44
LL | use std::sync::atomic::AtomicU32;
@@ -24,7 +24,7 @@ error: `std::time::Instant` is not allowed according to config
2424
LL | fn bad_arg_type(_: impl Fn(Sneaky) -> foo::atomic::AtomicU32) {
2525
| ^^^^^^
2626

27-
error: `core::sync::atomic::AtomicU32` is not allowed according to config
27+
error: `std::sync::atomic::AtomicU32` is not allowed according to config
2828
--> $DIR/conf_disallowed_type.rs:16:39
2929
|
3030
LL | fn bad_arg_type(_: impl Fn(Sneaky) -> foo::atomic::AtomicU32) {
@@ -36,13 +36,13 @@ error: `std::io::Read` is not allowed according to config
3636
LL | fn trait_obj(_: &dyn std::io::Read) {
3737
| ^^^^^^^^^^^^^
3838

39-
error: `std::collections::hash::map::HashMap` is not allowed according to config
39+
error: `std::collections::HashMap` is not allowed according to config
4040
--> $DIR/conf_disallowed_type.rs:28:48
4141
|
4242
LL | let _: std::collections::HashMap<(), ()> = std::collections::HashMap::new();
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4444

45-
error: `std::collections::hash::map::HashMap` is not allowed according to config
45+
error: `std::collections::HashMap` is not allowed according to config
4646
--> $DIR/conf_disallowed_type.rs:28:12
4747
|
4848
LL | let _: std::collections::HashMap<(), ()> = std::collections::HashMap::new();
@@ -54,25 +54,25 @@ error: `std::time::Instant` is not allowed according to config
5454
LL | let _ = Sneaky::now();
5555
| ^^^^^^
5656

57-
error: `core::sync::atomic::AtomicU32` is not allowed according to config
57+
error: `std::sync::atomic::AtomicU32` is not allowed according to config
5858
--> $DIR/conf_disallowed_type.rs:30:13
5959
|
6060
LL | let _ = foo::atomic::AtomicU32::new(0);
6161
| ^^^^^^^^^^^^^^^^^^^^^^
6262

63-
error: `core::sync::atomic::AtomicU32` is not allowed according to config
63+
error: `std::sync::atomic::AtomicU32` is not allowed according to config
6464
--> $DIR/conf_disallowed_type.rs:31:17
6565
|
6666
LL | static FOO: std::sync::atomic::AtomicU32 = foo::atomic::AtomicU32::new(1);
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6868

69-
error: `core::sync::atomic::AtomicU32` is not allowed according to config
69+
error: `std::sync::atomic::AtomicU32` is not allowed according to config
7070
--> $DIR/conf_disallowed_type.rs:31:48
7171
|
7272
LL | static FOO: std::sync::atomic::AtomicU32 = foo::atomic::AtomicU32::new(1);
7373
| ^^^^^^^^^^^^^^^^^^^^^^
7474

75-
error: `syn::ty::TypePath` is not allowed according to config
75+
error: `syn::TypePath` is not allowed according to config
7676
--> $DIR/conf_disallowed_type.rs:32:43
7777
|
7878
LL | let _: std::collections::BTreeMap<(), syn::TypePath> = Default::default();

0 commit comments

Comments
 (0)