Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ab66ea6

Browse files
committed
add: emit{,_spanned}_lint for LintLevelsBuilder
add: `emit_spanned_lint` and `emit_lint` for `LintLevelsBuilder` migrate: `DeprecatedLintName`
1 parent f9289c3 commit ab66ea6

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

compiler/rustc_error_messages/locales/en-US/lint.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ lint_enum_intrinsics_mem_variant =
1616
lint_expectation = this lint expectation is unfulfilled
1717
.note = the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
1818
19+
lint_deprecated_lint_name =
20+
lint name `{$name}` is deprecated and may not have an effect in the future.
21+
.suggestion = change it to
22+
1923
lint_hidden_unicode_codepoints = unicode codepoint changing visible direction of text present in {$label}
2024
.label = this {$label} contains {$count ->
2125
[one] an invisible

compiler/rustc_lint/src/levels.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use crate::context::{CheckLintNameResult, LintStore};
22
use crate::late::unerased_lint_store;
3+
use crate::lints::DeprecatedLintName;
34
use rustc_ast as ast;
45
use rustc_ast_pretty::pprust;
56
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, DiagnosticMessage, MultiSpan};
7+
use rustc_errors::{
8+
Applicability, DecorateLint, Diagnostic, DiagnosticBuilder, DiagnosticMessage, MultiSpan,
9+
};
710
use rustc_hir as hir;
811
use rustc_hir::intravisit::{self, Visitor};
912
use rustc_hir::HirId;
@@ -858,25 +861,13 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
858861
}
859862
Err((Some(ids), ref new_lint_name)) => {
860863
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
861-
let (lvl, src) = self.provider.get_lint_level(lint, &sess);
862-
struct_lint_level(
863-
self.sess,
864+
self.emit_spanned_lint(
864865
lint,
865-
lvl,
866-
src,
867-
Some(sp.into()),
868-
format!(
869-
"lint name `{}` is deprecated \
870-
and may not have an effect in the future.",
871-
name
872-
),
873-
|lint| {
874-
lint.span_suggestion(
875-
sp,
876-
"change it to",
877-
new_lint_name,
878-
Applicability::MachineApplicable,
879-
)
866+
sp.into(),
867+
DeprecatedLintName {
868+
name,
869+
suggestion: sp,
870+
replace: &new_lint_name,
880871
},
881872
);
882873

@@ -1086,6 +1077,25 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
10861077
let (level, src) = self.lint_level(lint);
10871078
struct_lint_level(self.sess, lint, level, src, span, msg, decorate)
10881079
}
1080+
1081+
pub fn emit_spanned_lint(
1082+
&self,
1083+
lint: &'static Lint,
1084+
span: MultiSpan,
1085+
decorate: impl for<'a> DecorateLint<'a, ()>,
1086+
) {
1087+
let (level, src) = self.lint_level(lint);
1088+
struct_lint_level(self.sess, lint, level, src, Some(span), decorate.msg(), |lint| {
1089+
decorate.decorate_lint(lint)
1090+
});
1091+
}
1092+
1093+
pub fn emit_lint(&self, lint: &'static Lint, decorate: impl for<'a> DecorateLint<'a, ()>) {
1094+
let (level, src) = self.lint_level(lint);
1095+
struct_lint_level(self.sess, lint, level, src, None, decorate.msg(), |lint| {
1096+
decorate.decorate_lint(lint)
1097+
});
1098+
}
10891099
}
10901100

10911101
pub(crate) fn provide(providers: &mut Providers) {

compiler/rustc_lint/src/lints.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ pub struct EnumIntrinsicsMemVariant<'a> {
4949
pub ty_param: Ty<'a>,
5050
}
5151

52+
// levels.rs
53+
#[derive(LintDiagnostic)]
54+
#[diag(lint::deprecated_lint_name)]
55+
pub struct DeprecatedLintName<'a> {
56+
pub name: String,
57+
#[suggestion(code = "{replace}", applicability = "machine-applicable")]
58+
pub suggestion: Span,
59+
pub replace: &'a str,
60+
}
61+
5262
// methods.rs
5363
#[derive(LintDiagnostic)]
5464
#[diag(lint_cstring_ptr)]

0 commit comments

Comments
 (0)