Skip to content

Commit 736ac3d

Browse files
committed
Undo most of last commit and add type alias
1 parent fef10d4 commit 736ac3d

File tree

5 files changed

+33
-72
lines changed

5 files changed

+33
-72
lines changed

clippy_config/src/types.rs

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,24 @@ pub struct Rename {
1414
pub rename: String,
1515
}
1616

17-
#[derive(Debug, Serialize)]
18-
pub struct DisallowedPathWithoutReplacement {
19-
path: String,
20-
reason: Option<String>,
21-
}
17+
pub type DisallowedPathWithoutReplacement = DisallowedPath<false>;
2218

23-
#[derive(Clone, Debug, Serialize)]
24-
pub struct DisallowedPath {
19+
#[derive(Debug, Serialize)]
20+
pub struct DisallowedPath<const REPLACEMENT_ALLOWED: bool = true> {
2521
path: String,
2622
reason: Option<String>,
2723
replacement: Option<String>,
2824
}
2925

30-
impl<'de> Deserialize<'de> for DisallowedPathWithoutReplacement {
26+
impl<'de, const REPLACEMENT_ALLOWED: bool> Deserialize<'de> for DisallowedPath<REPLACEMENT_ALLOWED> {
3127
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
3228
where
3329
D: Deserializer<'de>,
3430
{
3531
let enum_ = DisallowedPathEnum::deserialize(deserializer)?;
36-
if enum_.replacement().is_some() {
32+
if !REPLACEMENT_ALLOWED && enum_.replacement().is_some() {
3733
return Err(de::Error::custom("replacement not allowed for this configuration"));
3834
}
39-
Ok(Self {
40-
path: enum_.path().to_owned(),
41-
reason: enum_.reason().map(ToOwned::to_owned),
42-
})
43-
}
44-
}
45-
46-
impl<'de> Deserialize<'de> for DisallowedPath {
47-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
48-
where
49-
D: Deserializer<'de>,
50-
{
51-
let enum_ = DisallowedPathEnum::deserialize(deserializer)?;
5235
Ok(Self {
5336
path: enum_.path().to_owned(),
5437
reason: enum_.reason().map(ToOwned::to_owned),
@@ -68,45 +51,24 @@ pub enum DisallowedPathEnum {
6851
},
6952
}
7053

71-
pub trait AmendDiag {
72-
fn path(&self) -> &str;
73-
fn reason(&self) -> Option<&str>;
74-
fn replacement(&self) -> Option<&str>;
75-
fn amend_diag(&self, span: Span, diag: &mut Diag<'_, ()>) {
76-
if let Some(replacement) = &self.replacement() {
77-
diag.span_suggestion(
78-
span,
79-
self.reason().map_or_else(|| String::from("use"), ToOwned::to_owned),
80-
replacement,
81-
Applicability::MachineApplicable,
82-
);
83-
} else if let Some(reason) = self.reason() {
84-
diag.note(reason.to_owned());
85-
}
86-
}
87-
}
88-
89-
impl AmendDiag for DisallowedPathWithoutReplacement {
90-
fn path(&self) -> &str {
54+
impl<const REPLACEMENT_ALLOWED: bool> DisallowedPath<REPLACEMENT_ALLOWED> {
55+
pub fn path(&self) -> &str {
9156
&self.path
9257
}
93-
fn reason(&self) -> Option<&str> {
94-
self.reason.as_deref()
95-
}
96-
fn replacement(&self) -> Option<&str> {
97-
None
98-
}
99-
}
10058

101-
impl AmendDiag for DisallowedPath {
102-
fn path(&self) -> &str {
103-
&self.path
104-
}
105-
fn reason(&self) -> Option<&str> {
106-
self.reason.as_deref()
107-
}
108-
fn replacement(&self) -> Option<&str> {
109-
self.replacement.as_deref()
59+
pub fn diag_amendment(&self, span: Span) -> impl FnOnce(&mut Diag<'_, ()>) + use<'_, REPLACEMENT_ALLOWED> {
60+
move |diag| {
61+
if let Some(replacement) = &self.replacement {
62+
diag.span_suggestion(
63+
span,
64+
self.reason.as_ref().map_or_else(|| String::from("use"), Clone::clone),
65+
replacement,
66+
Applicability::MachineApplicable,
67+
);
68+
} else if let Some(reason) = &self.reason {
69+
diag.note(reason.clone());
70+
}
71+
}
11072
}
11173
}
11274

@@ -133,10 +95,10 @@ impl DisallowedPathEnum {
13395
}
13496

13597
/// Creates a map of disallowed items to the reason they were disallowed.
136-
pub fn create_disallowed_map<T: AmendDiag>(
98+
pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>(
13799
tcx: TyCtxt<'_>,
138-
disallowed: &'static [T],
139-
) -> DefIdMap<(&'static str, &'static T)> {
100+
disallowed: &'static [DisallowedPath<REPLACEMENT_ALLOWED>],
101+
) -> DefIdMap<(&'static str, &'static DisallowedPath<REPLACEMENT_ALLOWED>)> {
140102
disallowed
141103
.iter()
142104
.map(|x| (x.path(), x.path().split("::").collect::<Vec<_>>(), x))

clippy_lints/src/await_holding_invalid.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_config::types::{AmendDiag, DisallowedPathWithoutReplacement, create_disallowed_map};
2+
use clippy_config::types::{DisallowedPathWithoutReplacement, create_disallowed_map};
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::{match_def_path, paths};
55
use rustc_hir as hir;
@@ -266,7 +266,7 @@ fn emit_invalid_type(
266266
AWAIT_HOLDING_INVALID_TYPE,
267267
span,
268268
format!("holding a disallowed type across an await point `{path}`"),
269-
|diag| disallowed_path.amend_diag(span, diag),
269+
disallowed_path.diag_amendment(span),
270270
);
271271
}
272272

clippy_lints/src/disallowed_macros.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_config::types::{AmendDiag, DisallowedPath, create_disallowed_map};
2+
use clippy_config::types::{DisallowedPath, create_disallowed_map};
33
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
44
use clippy_utils::macros::macro_backtrace;
55
use rustc_data_structures::fx::FxHashSet;
@@ -92,6 +92,7 @@ impl DisallowedMacros {
9292

9393
if let Some(&(path, disallowed_path)) = self.disallowed.get(&mac.def_id) {
9494
let msg = format!("use of a disallowed macro `{path}`");
95+
let add_note = disallowed_path.diag_amendment(mac.span);
9596
if matches!(mac.kind, MacroKind::Derive)
9697
&& let Some(derive_src) = derive_src
9798
{
@@ -101,12 +102,10 @@ impl DisallowedMacros {
101102
cx.tcx.local_def_id_to_hir_id(derive_src.def_id),
102103
mac.span,
103104
msg,
104-
|diag| disallowed_path.amend_diag(mac.span, diag),
105+
add_note,
105106
);
106107
} else {
107-
span_lint_and_then(cx, DISALLOWED_MACROS, mac.span, msg, |diag| {
108-
disallowed_path.amend_diag(mac.span, diag);
109-
});
108+
span_lint_and_then(cx, DISALLOWED_MACROS, mac.span, msg, add_note);
110109
}
111110
}
112111
}

clippy_lints/src/disallowed_methods.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_config::types::{AmendDiag, DisallowedPath, create_disallowed_map};
2+
use clippy_config::types::{DisallowedPath, create_disallowed_map};
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use rustc_hir::def::{CtorKind, DefKind, Res};
55
use rustc_hir::def_id::DefIdMap;
@@ -93,7 +93,7 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
9393
DISALLOWED_METHODS,
9494
span,
9595
format!("use of a disallowed method `{path}`"),
96-
|diag| disallowed_path.amend_diag(span, diag),
96+
disallowed_path.diag_amendment(span),
9797
);
9898
}
9999
}

clippy_lints/src/disallowed_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_config::types::{AmendDiag, DisallowedPath};
2+
use clippy_config::types::DisallowedPath;
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_hir::def::Res;
@@ -89,7 +89,7 @@ impl DisallowedTypes {
8989
DISALLOWED_TYPES,
9090
span,
9191
format!("use of a disallowed type `{path}`"),
92-
|diag| disallowed_path.amend_diag(span, diag),
92+
disallowed_path.diag_amendment(span),
9393
);
9494
}
9595
}

0 commit comments

Comments
 (0)