]> git.lizzy.rs Git - rust.git/commitdiff
add: `emit{,_spanned}_lint` for `LintLevelsBuilder`
authorRejyr <jerrylwang123@gmail.com>
Wed, 5 Oct 2022 12:23:00 +0000 (08:23 -0400)
committerRejyr <jerrylwang123@gmail.com>
Mon, 9 Jan 2023 22:07:25 +0000 (17:07 -0500)
add: `emit_spanned_lint` and `emit_lint` for `LintLevelsBuilder`
migrate: `DeprecatedLintName`

compiler/rustc_error_messages/locales/en-US/lint.ftl
compiler/rustc_lint/src/levels.rs
compiler/rustc_lint/src/lints.rs

index 2eb409a5ddd5e12297262ade97e6bb20ce48dd06..78b8f6aaae9e2cd8adfc81a1617fd19f11a94c56 100644 (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
index e9d3d44a3f9f9f6e6fede79abd3d00190aa2bd1d..edbf0c6f37a093580271bffe9547ce2ae6f8e694 100644 (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 @@ fn add(&mut self, attrs: &[ast::Attribute], is_crate_node: bool, source_hir_id:
                             }
                             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 @@ pub(crate) fn struct_lint(
         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) {
index 42deb455b0619781730fb766f44122bab5308d07..942497a1d57a7c9dd9a4bbfed13d1cf4f40a7755 100644 (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)]