Remove BuiltinLintDiag usage in rustc_ast_passes

This commit is contained in:
Guillaume Gomez
2026-03-22 18:35:02 +01:00
parent faddfb5001
commit 33fbecc394
5 changed files with 98 additions and 110 deletions

View File

@@ -27,10 +27,9 @@ use rustc_ast::*;
use rustc_ast_pretty::pprust::{self, State};
use rustc_attr_parsing::validate_attr;
use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::{DiagCtxtHandle, LintBuffer};
use rustc_errors::{DiagCtxtHandle, Diagnostic, LintBuffer};
use rustc_feature::Features;
use rustc_session::Session;
use rustc_session::lint::BuiltinLintDiag;
use rustc_session::lint::builtin::{
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, MISSING_UNSAFE_ON_EXTERN,
PATTERNS_IN_FNS_WITHOUT_BODY, UNUSED_VISIBILITIES,
@@ -1424,7 +1423,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
UNUSED_VISIBILITIES,
item.id,
item.vis.span,
BuiltinLintDiag::UnusedVisibility(item.vis.span),
errors::UnusedVisibility { span: item.vis.span },
)
}
@@ -1731,14 +1730,19 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
if let Some(ident) = ident {
self.lint_buffer.buffer_lint(
let is_foreign = matches!(ctxt, FnCtxt::Foreign);
self.lint_buffer.dyn_buffer_lint(
PATTERNS_IN_FNS_WITHOUT_BODY,
id,
span,
BuiltinLintDiag::PatternsInFnsWithoutBody {
span,
ident,
is_foreign: matches!(ctxt, FnCtxt::Foreign),
move |dcx, level| {
let sub = errors::PatternsInFnsWithoutBodySub { ident, span };
if is_foreign {
errors::PatternsInFnsWithoutBody::Foreign { sub }
} else {
errors::PatternsInFnsWithoutBody::Bodiless { sub }
}
.into_diag(dcx, level)
},
)
}
@@ -1828,11 +1832,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
Some((right, snippet))
}
};
self.lint_buffer.buffer_lint(
let left_sp = err.span;
self.lint_buffer.dyn_buffer_lint(
DEPRECATED_WHERE_CLAUSE_LOCATION,
item.id,
err.span,
BuiltinLintDiag::DeprecatedWhereclauseLocation(err.span, sugg),
move |dcx, level| {
let suggestion = match sugg {
Some((right_sp, sugg)) => {
errors::DeprecatedWhereClauseLocationSugg::MoveToEnd {
left: left_sp,
right: right_sp,
sugg,
}
}
None => {
errors::DeprecatedWhereClauseLocationSugg::RemoveWhere { span: left_sp }
}
};
errors::DeprecatedWhereClauseLocation { suggestion }.into_diag(dcx, level)
},
);
}

View File

@@ -1150,3 +1150,72 @@ pub(crate) struct RequiresRustAbi {
#[label("not using the Rust ABI because of this")]
pub extern_abi_span: Span,
}
#[derive(Diagnostic)]
#[diag("visibility qualifiers have no effect on `const _` declarations")]
#[note("`const _` does not declare a name, so there is nothing for the qualifier to apply to")]
pub(crate) struct UnusedVisibility {
#[suggestion(
"remove the qualifier",
style = "short",
code = "",
applicability = "machine-applicable"
)]
pub span: Span,
}
#[derive(Subdiagnostic)]
#[suggestion(
"remove `mut` from the parameter",
code = "{ident}",
applicability = "machine-applicable"
)]
pub(crate) struct PatternsInFnsWithoutBodySub {
#[primary_span]
pub span: Span,
pub ident: Ident,
}
#[derive(Diagnostic)]
pub(crate) enum PatternsInFnsWithoutBody {
#[diag("patterns aren't allowed in foreign function declarations")]
Foreign {
#[subdiagnostic]
sub: PatternsInFnsWithoutBodySub,
},
#[diag("patterns aren't allowed in functions without bodies")]
Bodiless {
#[subdiagnostic]
sub: PatternsInFnsWithoutBodySub,
},
}
#[derive(Diagnostic)]
#[diag("where clause not allowed here")]
#[note("see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information")]
pub(crate) struct DeprecatedWhereClauseLocation {
#[subdiagnostic]
pub suggestion: DeprecatedWhereClauseLocationSugg,
}
#[derive(Subdiagnostic)]
pub(crate) enum DeprecatedWhereClauseLocationSugg {
#[multipart_suggestion(
"move it to the end of the type declaration",
applicability = "machine-applicable"
)]
MoveToEnd {
#[suggestion_part(code = "")]
left: Span,
#[suggestion_part(code = "{sugg}")]
right: Span,
sugg: String,
},
#[suggestion("remove this `where`", code = "", applicability = "machine-applicable")]
RemoveWhere {
#[primary_span]
span: Span,
},
}

View File

@@ -116,26 +116,6 @@ impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
stability::Deprecated { sub, kind: "macro".to_owned(), path, note, since_kind }
.into_diag(dcx, level)
}
BuiltinLintDiag::PatternsInFnsWithoutBody { span: remove_span, ident, is_foreign } => {
let sub = lints::PatternsInFnsWithoutBodySub { ident, span: remove_span };
if is_foreign {
lints::PatternsInFnsWithoutBody::Foreign { sub }
} else {
lints::PatternsInFnsWithoutBody::Bodiless { sub }
}
.into_diag(dcx, level)
}
BuiltinLintDiag::DeprecatedWhereclauseLocation(left_sp, sugg) => {
let suggestion = match sugg {
Some((right_sp, sugg)) => lints::DeprecatedWhereClauseLocationSugg::MoveToEnd {
left: left_sp,
right: right_sp,
sugg,
},
None => lints::DeprecatedWhereClauseLocationSugg::RemoveWhere { span: left_sp },
};
lints::DeprecatedWhereClauseLocation { suggestion }.into_diag(dcx, level)
}
BuiltinLintDiag::SingleUseLifetime {
param_span,
use_span: Some((use_span, elide)),
@@ -257,9 +237,6 @@ impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
BuiltinLintDiag::UnusedCrateDependency { extern_crate, local_crate } => {
lints::UnusedCrateDependency { extern_crate, local_crate }.into_diag(dcx, level)
}
BuiltinLintDiag::UnusedVisibility(span) => {
lints::UnusedVisibility { span }.into_diag(dcx, level)
}
BuiltinLintDiag::AttributeLint(kind) => {
DecorateAttrLint { sess: self.sess, tcx: self.tcx, diagnostic: &kind }
.into_diag(dcx, level)

View File

@@ -3125,62 +3125,6 @@ pub(crate) enum RedundantImportSub {
},
}
#[derive(Diagnostic)]
pub(crate) enum PatternsInFnsWithoutBody {
#[diag("patterns aren't allowed in foreign function declarations")]
Foreign {
#[subdiagnostic]
sub: PatternsInFnsWithoutBodySub,
},
#[diag("patterns aren't allowed in functions without bodies")]
Bodiless {
#[subdiagnostic]
sub: PatternsInFnsWithoutBodySub,
},
}
#[derive(Subdiagnostic)]
#[suggestion(
"remove `mut` from the parameter",
code = "{ident}",
applicability = "machine-applicable"
)]
pub(crate) struct PatternsInFnsWithoutBodySub {
#[primary_span]
pub span: Span,
pub ident: Ident,
}
#[derive(Diagnostic)]
#[diag("where clause not allowed here")]
#[note("see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information")]
pub(crate) struct DeprecatedWhereClauseLocation {
#[subdiagnostic]
pub suggestion: DeprecatedWhereClauseLocationSugg,
}
#[derive(Subdiagnostic)]
pub(crate) enum DeprecatedWhereClauseLocationSugg {
#[multipart_suggestion(
"move it to the end of the type declaration",
applicability = "machine-applicable"
)]
MoveToEnd {
#[suggestion_part(code = "")]
left: Span,
#[suggestion_part(code = "{sugg}")]
right: Span,
sugg: String,
},
#[suggestion("remove this `where`", code = "", applicability = "machine-applicable")]
RemoveWhere {
#[primary_span]
span: Span,
},
}
#[derive(Diagnostic)]
#[diag("lifetime parameter `{$ident}` only used once")]
pub(crate) struct SingleUseLifetime {
@@ -3644,19 +3588,6 @@ pub(crate) struct UnsafeAttrOutsideUnsafeSuggestion {
pub right: Span,
}
#[derive(Diagnostic)]
#[diag("visibility qualifiers have no effect on `const _` declarations")]
#[note("`const _` does not declare a name, so there is nothing for the qualifier to apply to")]
pub(crate) struct UnusedVisibility {
#[suggestion(
"remove the qualifier",
style = "short",
code = "",
applicability = "machine-applicable"
)]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("doc alias is duplicated")]
pub(crate) struct DocAliasDuplicated {

View File

@@ -673,13 +673,6 @@ pub enum BuiltinLintDiag {
path: String,
since_kind: DeprecatedSinceKind,
},
PatternsInFnsWithoutBody {
span: Span,
ident: Ident,
is_foreign: bool,
},
/// `##` or `#"` in edition < 2024.
DeprecatedWhereclauseLocation(Span, Option<(Span, String)>),
SingleUseLifetime {
/// Span of the parameter which declares this lifetime.
param_span: Span,
@@ -737,7 +730,6 @@ pub enum BuiltinLintDiag {
extern_crate: Symbol,
local_crate: Symbol,
},
UnusedVisibility(Span),
AttributeLint(AttributeLintKind),
UnreachableCfg {
span: Span,