]> git.lizzy.rs Git - rust.git/commitdiff
Apply lint restrictions from renamed lints
authorJoshua Nelson <jyn514@gmail.com>
Sun, 28 Feb 2021 05:12:54 +0000 (00:12 -0500)
committerJoshua Nelson <jyn514@gmail.com>
Sun, 28 Feb 2021 06:04:34 +0000 (01:04 -0500)
Previously, if you denied the old name of a renamed lint, it would warn
about using the new name, but otherwise do nothing. Now, it will behave
the same as if you'd used the new name.

compiler/rustc_lint/src/levels.rs
src/test/ui/lint/renamed-lints-still-apply.rs [new file with mode: 0644]
src/test/ui/lint/renamed-lints-still-apply.stderr [new file with mode: 0644]

index cc5b7ef098756b078f91f67107898febc796a363..dbcab72ddf2eb6c115823be31d98c8f16f47fb54 100644 (file)
@@ -321,17 +321,18 @@ pub(crate) fn push(
                     None
                 };
                 let name = meta_item.path.segments.last().expect("empty lint name").ident.name;
-                match store.check_lint_name(&name.as_str(), tool_name) {
+                let lint_result = store.check_lint_name(&name.as_str(), tool_name);
+                match &lint_result {
                     CheckLintNameResult::Ok(ids) => {
                         let src = LintLevelSource::Node(name, li.span(), reason);
-                        for &id in ids {
+                        for &id in *ids {
                             self.check_gated_lint(id, attr.span);
                             self.insert_spec(&mut specs, id, (level, src));
                         }
                     }
 
                     CheckLintNameResult::Tool(result) => {
-                        match result {
+                        match *result {
                             Ok(ids) => {
                                 let complete_name = &format!("{}::{}", tool_name.unwrap(), name);
                                 let src = LintLevelSource::Node(
@@ -343,7 +344,7 @@ pub(crate) fn push(
                                     self.insert_spec(&mut specs, *id, (level, src));
                                 }
                             }
-                            Err((Some(ids), new_lint_name)) => {
+                            Err((Some(ids), ref new_lint_name)) => {
                                 let lint = builtin::RENAMED_AND_REMOVED_LINTS;
                                 let (lvl, src) =
                                     self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
@@ -392,21 +393,21 @@ pub(crate) fn push(
 
                     CheckLintNameResult::Warning(msg, renamed) => {
                         let lint = builtin::RENAMED_AND_REMOVED_LINTS;
-                        let (level, src) =
+                        let (renamed_lint_level, src) =
                             self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
                         struct_lint_level(
                             self.sess,
                             lint,
-                            level,
+                            renamed_lint_level,
                             src,
                             Some(li.span().into()),
                             |lint| {
                                 let mut err = lint.build(&msg);
-                                if let Some(new_name) = renamed {
+                                if let Some(new_name) = &renamed {
                                     err.span_suggestion(
                                         li.span(),
                                         "use the new name",
-                                        new_name,
+                                        new_name.to_string(),
                                         Applicability::MachineApplicable,
                                     );
                                 }
@@ -444,6 +445,22 @@ pub(crate) fn push(
                         );
                     }
                 }
+                // If this lint was renamed, apply the new lint instead of ignoring the attribute.
+                // This happens outside of the match because the new lint should be applied even if
+                // we don't warn about the name change.
+                if let CheckLintNameResult::Warning(_, Some(new_name)) = lint_result {
+                    // Ignore any errors or warnings that happen because the new name is inaccurate
+                    if let CheckLintNameResult::Ok(ids) =
+                        store.check_lint_name(&new_name, tool_name)
+                    {
+                        let src =
+                            LintLevelSource::Node(Symbol::intern(&new_name), li.span(), reason);
+                        for &id in ids {
+                            self.check_gated_lint(id, attr.span);
+                            self.insert_spec(&mut specs, id, (level, src));
+                        }
+                    }
+                }
             }
         }
 
diff --git a/src/test/ui/lint/renamed-lints-still-apply.rs b/src/test/ui/lint/renamed-lints-still-apply.rs
new file mode 100644 (file)
index 0000000..01cd325
--- /dev/null
@@ -0,0 +1,9 @@
+// compile-flags: --crate-type lib
+#![deny(single_use_lifetime)]
+//~^ WARNING renamed
+//~| NOTE `#[warn(renamed_and_removed_lints)]` on by default
+//~| NOTE defined here
+fn _foo<'a>(_x: &'a u32) {}
+//~^ ERROR only used once
+//~| NOTE this lifetime
+//~| NOTE is used only here
diff --git a/src/test/ui/lint/renamed-lints-still-apply.stderr b/src/test/ui/lint/renamed-lints-still-apply.stderr
new file mode 100644 (file)
index 0000000..33e5a03
--- /dev/null
@@ -0,0 +1,28 @@
+warning: lint `single_use_lifetime` has been renamed to `single_use_lifetimes`
+  --> $DIR/renamed-lints-still-apply.rs:2:9
+   |
+LL | #![deny(single_use_lifetime)]
+   |         ^^^^^^^^^^^^^^^^^^^ help: use the new name: `single_use_lifetimes`
+   |
+   = note: `#[warn(renamed_and_removed_lints)]` on by default
+
+error: lifetime parameter `'a` only used once
+  --> $DIR/renamed-lints-still-apply.rs:6:9
+   |
+LL | fn _foo<'a>(_x: &'a u32) {}
+   |         ^^       -- ...is used only here
+   |         |
+   |         this lifetime...
+   |
+note: the lint level is defined here
+  --> $DIR/renamed-lints-still-apply.rs:2:9
+   |
+LL | #![deny(single_use_lifetime)]
+   |         ^^^^^^^^^^^^^^^^^^^
+help: elide the single-use lifetime
+   |
+LL | fn _foo(_x: &u32) {}
+   |       --    --
+
+error: aborting due to previous error; 1 warning emitted
+