Skip to content

Commit fef10d4

Browse files
committed
Use two structs rather than a const generic
1 parent b97ad4e commit fef10d4

File tree

6 files changed

+78
-37
lines changed

6 files changed

+78
-37
lines changed

clippy_config/src/conf.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::ClippyConfiguration;
22
use crate::types::{
3-
DisallowedPath, MacroMatcher, MatchLintBehaviour, PubUnderscoreFieldsBehaviour, Rename, SourceItemOrdering,
4-
SourceItemOrderingCategory, SourceItemOrderingModuleItemGroupings, SourceItemOrderingModuleItemKind,
5-
SourceItemOrderingTraitAssocItemKind, SourceItemOrderingTraitAssocItemKinds,
3+
DisallowedPath, DisallowedPathWithoutReplacement, MacroMatcher, MatchLintBehaviour, PubUnderscoreFieldsBehaviour,
4+
Rename, SourceItemOrdering, SourceItemOrderingCategory, SourceItemOrderingModuleItemGroupings,
5+
SourceItemOrderingModuleItemKind, SourceItemOrderingTraitAssocItemKind, SourceItemOrderingTraitAssocItemKinds,
66
};
77
use clippy_utils::msrvs::Msrv;
88
use rustc_errors::Applicability;
@@ -445,7 +445,7 @@ define_Conf! {
445445
avoid_breaking_exported_api: bool = true,
446446
/// The list of types which may not be held across an await point.
447447
#[lints(await_holding_invalid_type)]
448-
await_holding_invalid_types: Vec<DisallowedPath<false>> = Vec::new(),
448+
await_holding_invalid_types: Vec<DisallowedPathWithoutReplacement> = Vec::new(),
449449
/// DEPRECATED LINT: BLACKLISTED_NAME.
450450
///
451451
/// Use the Disallowed Names lint instead

clippy_config/src/types.rs

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,40 @@ pub struct Rename {
1515
}
1616

1717
#[derive(Debug, Serialize)]
18-
pub struct DisallowedPath<const REPLACEMENT_ALLOWED: bool = true> {
18+
pub struct DisallowedPathWithoutReplacement {
19+
path: String,
20+
reason: Option<String>,
21+
}
22+
23+
#[derive(Clone, Debug, Serialize)]
24+
pub struct DisallowedPath {
1925
path: String,
2026
reason: Option<String>,
2127
replacement: Option<String>,
2228
}
2329

24-
impl<'de, const REPLACEMENT_ALLOWED: bool> Deserialize<'de> for DisallowedPath<REPLACEMENT_ALLOWED> {
30+
impl<'de> Deserialize<'de> for DisallowedPathWithoutReplacement {
2531
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
2632
where
2733
D: Deserializer<'de>,
2834
{
2935
let enum_ = DisallowedPathEnum::deserialize(deserializer)?;
30-
if !REPLACEMENT_ALLOWED && enum_.replacement().is_some() {
36+
if enum_.replacement().is_some() {
3137
return Err(de::Error::custom("replacement not allowed for this configuration"));
3238
}
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)?;
3352
Ok(Self {
3453
path: enum_.path().to_owned(),
3554
reason: enum_.reason().map(ToOwned::to_owned),
@@ -49,24 +68,45 @@ pub enum DisallowedPathEnum {
4968
},
5069
}
5170

52-
impl<const REPLACEMENT_ALLOWED: bool> DisallowedPath<REPLACEMENT_ALLOWED> {
53-
pub fn path(&self) -> &str {
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 {
5491
&self.path
5592
}
93+
fn reason(&self) -> Option<&str> {
94+
self.reason.as_deref()
95+
}
96+
fn replacement(&self) -> Option<&str> {
97+
None
98+
}
99+
}
56100

57-
pub fn diag_amendment(&self, span: Span) -> impl FnOnce(&mut Diag<'_, ()>) + use<'_, REPLACEMENT_ALLOWED> {
58-
move |diag| {
59-
if let Some(replacement) = &self.replacement {
60-
diag.span_suggestion(
61-
span,
62-
self.reason.as_ref().map_or_else(|| String::from("use"), Clone::clone),
63-
replacement,
64-
Applicability::MachineApplicable,
65-
);
66-
} else if let Some(reason) = &self.reason {
67-
diag.note(reason.clone());
68-
}
69-
}
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()
70110
}
71111
}
72112

@@ -93,10 +133,10 @@ impl DisallowedPathEnum {
93133
}
94134

95135
/// Creates a map of disallowed items to the reason they were disallowed.
96-
pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>(
136+
pub fn create_disallowed_map<T: AmendDiag>(
97137
tcx: TyCtxt<'_>,
98-
disallowed: &'static [DisallowedPath<REPLACEMENT_ALLOWED>],
99-
) -> DefIdMap<(&'static str, &'static DisallowedPath<REPLACEMENT_ALLOWED>)> {
138+
disallowed: &'static [T],
139+
) -> DefIdMap<(&'static str, &'static T)> {
100140
disallowed
101141
.iter()
102142
.map(|x| (x.path(), x.path().split("::").collect::<Vec<_>>(), x))

clippy_lints/src/await_holding_invalid.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_config::types::{DisallowedPath, create_disallowed_map};
2+
use clippy_config::types::{AmendDiag, 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;
@@ -174,7 +174,7 @@ declare_clippy_lint! {
174174
impl_lint_pass!(AwaitHolding => [AWAIT_HOLDING_LOCK, AWAIT_HOLDING_REFCELL_REF, AWAIT_HOLDING_INVALID_TYPE]);
175175

176176
pub struct AwaitHolding {
177-
def_ids: DefIdMap<(&'static str, &'static DisallowedPath<false>)>,
177+
def_ids: DefIdMap<(&'static str, &'static DisallowedPathWithoutReplacement)>,
178178
}
179179

180180
impl AwaitHolding {
@@ -259,14 +259,14 @@ fn emit_invalid_type(
259259
cx: &LateContext<'_>,
260260
span: Span,
261261
path: &'static str,
262-
disallowed_path: &'static DisallowedPath<false>,
262+
disallowed_path: &'static DisallowedPathWithoutReplacement,
263263
) {
264264
span_lint_and_then(
265265
cx,
266266
AWAIT_HOLDING_INVALID_TYPE,
267267
span,
268268
format!("holding a disallowed type across an await point `{path}`"),
269-
disallowed_path.diag_amendment(span),
269+
|diag| disallowed_path.amend_diag(span, diag),
270270
);
271271
}
272272

clippy_lints/src/disallowed_macros.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_config::types::{DisallowedPath, create_disallowed_map};
2+
use clippy_config::types::{AmendDiag, 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,7 +92,6 @@ 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);
9695
if matches!(mac.kind, MacroKind::Derive)
9796
&& let Some(derive_src) = derive_src
9897
{
@@ -102,10 +101,12 @@ impl DisallowedMacros {
102101
cx.tcx.local_def_id_to_hir_id(derive_src.def_id),
103102
mac.span,
104103
msg,
105-
add_note,
104+
|diag| disallowed_path.amend_diag(mac.span, diag),
106105
);
107106
} else {
108-
span_lint_and_then(cx, DISALLOWED_MACROS, mac.span, msg, add_note);
107+
span_lint_and_then(cx, DISALLOWED_MACROS, mac.span, msg, |diag| {
108+
disallowed_path.amend_diag(mac.span, diag);
109+
});
109110
}
110111
}
111112
}

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::{DisallowedPath, create_disallowed_map};
2+
use clippy_config::types::{AmendDiag, 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-
disallowed_path.diag_amendment(span),
96+
|diag| disallowed_path.amend_diag(span, diag),
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::DisallowedPath;
2+
use clippy_config::types::{AmendDiag, 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-
disallowed_path.diag_amendment(span),
92+
|diag| disallowed_path.amend_diag(span, diag),
9393
);
9494
}
9595
}

0 commit comments

Comments
 (0)