Skip to content

Commit

Permalink
Undo most of last commit and add type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Jan 28, 2025
1 parent fef10d4 commit 736ac3d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 72 deletions.
84 changes: 23 additions & 61 deletions clippy_config/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,24 @@ pub struct Rename {
pub rename: String,
}

#[derive(Debug, Serialize)]
pub struct DisallowedPathWithoutReplacement {
path: String,
reason: Option<String>,
}
pub type DisallowedPathWithoutReplacement = DisallowedPath<false>;

#[derive(Clone, Debug, Serialize)]
pub struct DisallowedPath {
#[derive(Debug, Serialize)]
pub struct DisallowedPath<const REPLACEMENT_ALLOWED: bool = true> {
path: String,
reason: Option<String>,
replacement: Option<String>,
}

impl<'de> Deserialize<'de> for DisallowedPathWithoutReplacement {
impl<'de, const REPLACEMENT_ALLOWED: bool> Deserialize<'de> for DisallowedPath<REPLACEMENT_ALLOWED> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let enum_ = DisallowedPathEnum::deserialize(deserializer)?;
if enum_.replacement().is_some() {
if !REPLACEMENT_ALLOWED && enum_.replacement().is_some() {
return Err(de::Error::custom("replacement not allowed for this configuration"));
}
Ok(Self {
path: enum_.path().to_owned(),
reason: enum_.reason().map(ToOwned::to_owned),
})
}
}

impl<'de> Deserialize<'de> for DisallowedPath {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let enum_ = DisallowedPathEnum::deserialize(deserializer)?;
Ok(Self {
path: enum_.path().to_owned(),
reason: enum_.reason().map(ToOwned::to_owned),
Expand All @@ -68,45 +51,24 @@ pub enum DisallowedPathEnum {
},
}

pub trait AmendDiag {
fn path(&self) -> &str;
fn reason(&self) -> Option<&str>;
fn replacement(&self) -> Option<&str>;
fn amend_diag(&self, span: Span, diag: &mut Diag<'_, ()>) {
if let Some(replacement) = &self.replacement() {
diag.span_suggestion(
span,
self.reason().map_or_else(|| String::from("use"), ToOwned::to_owned),
replacement,
Applicability::MachineApplicable,
);
} else if let Some(reason) = self.reason() {
diag.note(reason.to_owned());
}
}
}

impl AmendDiag for DisallowedPathWithoutReplacement {
fn path(&self) -> &str {
impl<const REPLACEMENT_ALLOWED: bool> DisallowedPath<REPLACEMENT_ALLOWED> {
pub fn path(&self) -> &str {
&self.path
}
fn reason(&self) -> Option<&str> {
self.reason.as_deref()
}
fn replacement(&self) -> Option<&str> {
None
}
}

impl AmendDiag for DisallowedPath {
fn path(&self) -> &str {
&self.path
}
fn reason(&self) -> Option<&str> {
self.reason.as_deref()
}
fn replacement(&self) -> Option<&str> {
self.replacement.as_deref()
pub fn diag_amendment(&self, span: Span) -> impl FnOnce(&mut Diag<'_, ()>) + use<'_, REPLACEMENT_ALLOWED> {
move |diag| {
if let Some(replacement) = &self.replacement {
diag.span_suggestion(
span,
self.reason.as_ref().map_or_else(|| String::from("use"), Clone::clone),
replacement,
Applicability::MachineApplicable,
);
} else if let Some(reason) = &self.reason {
diag.note(reason.clone());
}
}
}
}

Expand All @@ -133,10 +95,10 @@ impl DisallowedPathEnum {
}

/// Creates a map of disallowed items to the reason they were disallowed.
pub fn create_disallowed_map<T: AmendDiag>(
pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>(
tcx: TyCtxt<'_>,
disallowed: &'static [T],
) -> DefIdMap<(&'static str, &'static T)> {
disallowed: &'static [DisallowedPath<REPLACEMENT_ALLOWED>],
) -> DefIdMap<(&'static str, &'static DisallowedPath<REPLACEMENT_ALLOWED>)> {
disallowed
.iter()
.map(|x| (x.path(), x.path().split("::").collect::<Vec<_>>(), x))
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/await_holding_invalid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_config::Conf;
use clippy_config::types::{AmendDiag, DisallowedPathWithoutReplacement, create_disallowed_map};
use clippy_config::types::{DisallowedPathWithoutReplacement, create_disallowed_map};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::{match_def_path, paths};
use rustc_hir as hir;
Expand Down Expand Up @@ -266,7 +266,7 @@ fn emit_invalid_type(
AWAIT_HOLDING_INVALID_TYPE,
span,
format!("holding a disallowed type across an await point `{path}`"),
|diag| disallowed_path.amend_diag(span, diag),
disallowed_path.diag_amendment(span),
);
}

Expand Down
9 changes: 4 additions & 5 deletions clippy_lints/src/disallowed_macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_config::Conf;
use clippy_config::types::{AmendDiag, DisallowedPath, create_disallowed_map};
use clippy_config::types::{DisallowedPath, create_disallowed_map};
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
use clippy_utils::macros::macro_backtrace;
use rustc_data_structures::fx::FxHashSet;
Expand Down Expand Up @@ -92,6 +92,7 @@ impl DisallowedMacros {

if let Some(&(path, disallowed_path)) = self.disallowed.get(&mac.def_id) {
let msg = format!("use of a disallowed macro `{path}`");
let add_note = disallowed_path.diag_amendment(mac.span);
if matches!(mac.kind, MacroKind::Derive)
&& let Some(derive_src) = derive_src
{
Expand All @@ -101,12 +102,10 @@ impl DisallowedMacros {
cx.tcx.local_def_id_to_hir_id(derive_src.def_id),
mac.span,
msg,
|diag| disallowed_path.amend_diag(mac.span, diag),
add_note,
);
} else {
span_lint_and_then(cx, DISALLOWED_MACROS, mac.span, msg, |diag| {
disallowed_path.amend_diag(mac.span, diag);
});
span_lint_and_then(cx, DISALLOWED_MACROS, mac.span, msg, add_note);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/disallowed_methods.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_config::Conf;
use clippy_config::types::{AmendDiag, DisallowedPath, create_disallowed_map};
use clippy_config::types::{DisallowedPath, create_disallowed_map};
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::DefIdMap;
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
DISALLOWED_METHODS,
span,
format!("use of a disallowed method `{path}`"),
|diag| disallowed_path.amend_diag(span, diag),
disallowed_path.diag_amendment(span),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/disallowed_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_config::Conf;
use clippy_config::types::{AmendDiag, DisallowedPath};
use clippy_config::types::DisallowedPath;
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::Res;
Expand Down Expand Up @@ -89,7 +89,7 @@ impl DisallowedTypes {
DISALLOWED_TYPES,
span,
format!("use of a disallowed type `{path}`"),
|diag| disallowed_path.amend_diag(span, diag),
disallowed_path.diag_amendment(span),
);
}
}
Expand Down

0 comments on commit 736ac3d

Please sign in to comment.