add: emit{,_spanned}_lint for LintLevelsBuilder

add: `emit_spanned_lint` and `emit_lint` for `LintLevelsBuilder`
migrate: `DeprecatedLintName`
This commit is contained in:
Rejyr 2022-10-05 08:23:00 -04:00
parent f9289c35fb
commit ab66ea61cf
3 changed files with 43 additions and 19 deletions

View file

@ -16,6 +16,10 @@ lint_enum_intrinsics_mem_variant =
lint_expectation = this lint expectation is unfulfilled
.note = the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
lint_deprecated_lint_name =
lint name `{$name}` is deprecated and may not have an effect in the future.
.suggestion = change it to
lint_hidden_unicode_codepoints = unicode codepoint changing visible direction of text present in {$label}
.label = this {$label} contains {$count ->
[one] an invisible

View file

@ -1,9 +1,12 @@
use crate::context::{CheckLintNameResult, LintStore};
use crate::late::unerased_lint_store;
use crate::lints::DeprecatedLintName;
use rustc_ast as ast;
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, DiagnosticMessage, MultiSpan};
use rustc_errors::{
Applicability, DecorateLint, Diagnostic, DiagnosticBuilder, DiagnosticMessage, MultiSpan,
};
use rustc_hir as hir;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::HirId;
@ -858,25 +861,13 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
}
Err((Some(ids), ref new_lint_name)) => {
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
let (lvl, src) = self.provider.get_lint_level(lint, &sess);
struct_lint_level(
self.sess,
self.emit_spanned_lint(
lint,
lvl,
src,
Some(sp.into()),
format!(
"lint name `{}` is deprecated \
and may not have an effect in the future.",
name
),
|lint| {
lint.span_suggestion(
sp,
"change it to",
new_lint_name,
Applicability::MachineApplicable,
)
sp.into(),
DeprecatedLintName {
name,
suggestion: sp,
replace: &new_lint_name,
},
);
@ -1086,6 +1077,25 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
let (level, src) = self.lint_level(lint);
struct_lint_level(self.sess, lint, level, src, span, msg, decorate)
}
pub fn emit_spanned_lint(
&self,
lint: &'static Lint,
span: MultiSpan,
decorate: impl for<'a> DecorateLint<'a, ()>,
) {
let (level, src) = self.lint_level(lint);
struct_lint_level(self.sess, lint, level, src, Some(span), decorate.msg(), |lint| {
decorate.decorate_lint(lint)
});
}
pub fn emit_lint(&self, lint: &'static Lint, decorate: impl for<'a> DecorateLint<'a, ()>) {
let (level, src) = self.lint_level(lint);
struct_lint_level(self.sess, lint, level, src, None, decorate.msg(), |lint| {
decorate.decorate_lint(lint)
});
}
}
pub(crate) fn provide(providers: &mut Providers) {

View file

@ -49,6 +49,16 @@ pub struct EnumIntrinsicsMemVariant<'a> {
pub ty_param: Ty<'a>,
}
// levels.rs
#[derive(LintDiagnostic)]
#[diag(lint::deprecated_lint_name)]
pub struct DeprecatedLintName<'a> {
pub name: String,
#[suggestion(code = "{replace}", applicability = "machine-applicable")]
pub suggestion: Span,
pub replace: &'a str,
}
// methods.rs
#[derive(LintDiagnostic)]
#[diag(lint_cstring_ptr)]