Skip to content

Commit d0f3577

Browse files
committed
Properly handle disallowed_types
1 parent f159a3e commit d0f3577

File tree

6 files changed

+58
-15
lines changed

6 files changed

+58
-15
lines changed

clippy_config/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl DisallowedPath {
4747
}
4848
}
4949

50-
pub fn reason(&self) -> Option<&str> {
50+
fn reason(&self) -> Option<&str> {
5151
match &self {
5252
Self::WithReason { reason, .. } => reason.as_deref(),
5353
Self::Simple(_) => None,

clippy_lints/src/disallowed_types.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_config::Conf;
2+
use clippy_config::types::DisallowedPath;
23
use clippy_utils::diagnostics::span_lint_and_then;
34
use rustc_data_structures::fx::FxHashMap;
45
use rustc_hir::def::Res;
@@ -31,7 +32,8 @@ declare_clippy_lint! {
3132
/// # When using an inline table, can add a `reason` for why the type
3233
/// # is disallowed.
3334
/// { path = "std::net::Ipv4Addr", reason = "no IPv4 allowed" },
34-
/// ]
35+
/// # Can also add a `replacement` that will be offered as a suggestion.
36+
/// { path = "std::sync::Mutex", reason = "prefer faster & simpler non-poisonable mutex", replacement = "parking_lot::Mutex" }, /// ]
3537
/// ```
3638
///
3739
/// ```rust,ignore
@@ -51,24 +53,23 @@ declare_clippy_lint! {
5153
}
5254

5355
pub struct DisallowedTypes {
54-
def_ids: DefIdMap<(&'static str, Option<&'static str>)>,
55-
prim_tys: FxHashMap<PrimTy, (&'static str, Option<&'static str>)>,
56+
def_ids: DefIdMap<(&'static str, &'static DisallowedPath)>,
57+
prim_tys: FxHashMap<PrimTy, (&'static str, &'static DisallowedPath)>,
5658
}
5759

5860
impl DisallowedTypes {
5961
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
6062
let mut def_ids = DefIdMap::default();
6163
let mut prim_tys = FxHashMap::default();
62-
for x in &conf.disallowed_types {
63-
let path: Vec<_> = x.path().split("::").collect::<Vec<_>>();
64-
let reason = x.reason();
64+
for disallowed_path in &conf.disallowed_types {
65+
let path: Vec<_> = disallowed_path.path().split("::").collect::<Vec<_>>();
6566
for res in clippy_utils::def_path_res(tcx, &path) {
6667
match res {
6768
Res::Def(_, id) => {
68-
def_ids.insert(id, (x.path(), reason));
69+
def_ids.insert(id, (disallowed_path.path(), disallowed_path));
6970
},
7071
Res::PrimTy(ty) => {
71-
prim_tys.insert(ty, (x.path(), reason));
72+
prim_tys.insert(ty, (disallowed_path.path(), disallowed_path));
7273
},
7374
_ => {},
7475
}
@@ -78,7 +79,7 @@ impl DisallowedTypes {
7879
}
7980

8081
fn check_res_emit(&self, cx: &LateContext<'_>, res: &Res, span: Span) {
81-
let (path, reason) = match res {
82+
let (path, disallowed_path) = match res {
8283
Res::Def(_, did) if let Some(&x) = self.def_ids.get(did) => x,
8384
Res::PrimTy(prim) if let Some(&x) = self.prim_tys.get(prim) => x,
8485
_ => return,
@@ -88,11 +89,7 @@ impl DisallowedTypes {
8889
DISALLOWED_TYPES,
8990
span,
9091
format!("use of a disallowed type `{path}`"),
91-
|diag| {
92-
if let Some(reason) = reason {
93-
diag.note(reason);
94-
}
95-
},
92+
disallowed_path.diag_amendment(span),
9693
);
9794
}
9895
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
disallowed-types = [
2+
{ path = "std::string::String", replacement = "wrapper::String" },
3+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![warn(clippy::disallowed_types)]
2+
3+
#[allow(clippy::disallowed_types)]
4+
mod wrapper {
5+
pub struct String(std::string::String);
6+
7+
impl From<&str> for String {
8+
fn from(value: &str) -> Self {
9+
Self(std::string::String::from(value))
10+
}
11+
}
12+
}
13+
14+
fn main() {
15+
let _ = wrapper::String::from("x");
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![warn(clippy::disallowed_types)]
2+
3+
#[allow(clippy::disallowed_types)]
4+
mod wrapper {
5+
pub struct String(std::string::String);
6+
7+
impl From<&str> for String {
8+
fn from(value: &str) -> Self {
9+
Self(std::string::String::from(value))
10+
}
11+
}
12+
}
13+
14+
fn main() {
15+
let _ = String::from("x");
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: use of a disallowed type `std::string::String`
2+
--> tests/ui-toml/replaceable_disallowed_types/replaceable_disallowed_types.rs:15:13
3+
|
4+
LL | let _ = String::from("x");
5+
| ^^^^^^ help: use: `wrapper::String`
6+
|
7+
= note: `-D clippy::disallowed-types` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::disallowed_types)]`
9+
10+
error: aborting due to 1 previous error
11+

0 commit comments

Comments
 (0)