]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #86009 - cjgillot:fwarn, r=davidtwco
authorbors <bors@rust-lang.org>
Tue, 29 Jun 2021 13:11:16 +0000 (13:11 +0000)
committerbors <bors@rust-lang.org>
Tue, 29 Jun 2021 13:11:16 +0000 (13:11 +0000)
Make ForceWarn a lint level.

Follow-up to #85788
cc `@rylev`

13 files changed:
compiler/rustc_lint/src/context.rs
compiler/rustc_lint/src/levels.rs
compiler/rustc_lint_defs/src/lib.rs
compiler/rustc_middle/src/lint.rs
src/test/ui/lint/force-warn/force-allowed-by-default-lint.stderr
src/test/ui/lint/force-warn/force-allowed-deny-by-default-lint.stderr
src/test/ui/lint/force-warn/force-allowed-warning.stderr
src/test/ui/lint/force-warn/force-deny-by-default-lint.stderr
src/test/ui/lint/force-warn/force-lint-allow-all-warnings.stderr
src/test/ui/lint/force-warn/force-lint-group-allow-all-warnings.stderr
src/test/ui/lint/force-warn/force-lint-in-allowed-group.stderr
src/test/ui/lint/force-warn/force-warn-group-allow-warning.stderr
src/test/ui/lint/force-warn/force-warn-group.stderr

index 933f7e47c3e9d1b36c75235ad791843f7051fee3..00869ac3c9a2bcdf0d41fc055c7996203f4d63c3 100644 (file)
@@ -334,14 +334,8 @@ pub fn find_lints(&self, mut lint_name: &str) -> Result<Vec<LintId>, FindLintErr
         }
     }
 
-    /// Checks the validity of lint names derived from the command line. Returns
-    /// true if the lint is valid, false otherwise.
-    pub fn check_lint_name_cmdline(
-        &self,
-        sess: &Session,
-        lint_name: &str,
-        level: Option<Level>,
-    ) -> bool {
+    /// Checks the validity of lint names derived from the command line
+    pub fn check_lint_name_cmdline(&self, sess: &Session, lint_name: &str, level: Level) {
         let db = match self.check_lint_name(lint_name, None) {
             CheckLintNameResult::Ok(_) => None,
             CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)),
@@ -367,23 +361,19 @@ pub fn check_lint_name_cmdline(
         };
 
         if let Some(mut db) = db {
-            if let Some(level) = level {
-                let msg = format!(
-                    "requested on the command line with `{} {}`",
-                    match level {
-                        Level::Allow => "-A",
-                        Level::Warn => "-W",
-                        Level::Deny => "-D",
-                        Level::Forbid => "-F",
-                    },
-                    lint_name
-                );
-                db.note(&msg);
-            }
+            let msg = format!(
+                "requested on the command line with `{} {}`",
+                match level {
+                    Level::Allow => "-A",
+                    Level::Warn => "-W",
+                    Level::ForceWarn => "--force-warns",
+                    Level::Deny => "-D",
+                    Level::Forbid => "-F",
+                },
+                lint_name
+            );
+            db.note(&msg);
             db.emit();
-            false
-        } else {
-            true
         }
     }
 
index 0ee434f5fb50bae3e359d986b90936c9027c1470..c0a059b92aa9ff27536a06c0528ab25799c29bca 100644 (file)
@@ -88,7 +88,7 @@ fn process_command_line(&mut self, sess: &Session, store: &LintStore) {
         self.sets.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
 
         for &(ref lint_name, level) in &sess.opts.lint_opts {
-            store.check_lint_name_cmdline(sess, &lint_name, Some(level));
+            store.check_lint_name_cmdline(sess, &lint_name, level);
             let orig_level = level;
 
             // If the cap is less than this specified level, e.g., if we've got
@@ -110,12 +110,13 @@ fn process_command_line(&mut self, sess: &Session, store: &LintStore) {
         }
 
         for lint_name in &sess.opts.force_warns {
-            let valid = store.check_lint_name_cmdline(sess, lint_name, None);
-            if valid {
-                let lints = store
-                    .find_lints(lint_name)
-                    .unwrap_or_else(|_| bug!("A valid lint failed to produce a lint ids"));
-                self.sets.force_warns.extend(&lints);
+            store.check_lint_name_cmdline(sess, lint_name, Level::ForceWarn);
+            let lints = store
+                .find_lints(lint_name)
+                .unwrap_or_else(|_| bug!("A valid lint failed to produce a lint ids"));
+            for id in lints {
+                let src = LintLevelSource::CommandLine(Symbol::intern(lint_name), Level::ForceWarn);
+                specs.insert(id, (Level::ForceWarn, src));
             }
         }
 
@@ -131,6 +132,8 @@ fn insert_spec(
         id: LintId,
         (level, src): LevelAndSource,
     ) {
+        let (old_level, old_src) =
+            self.sets.get_lint_level(id.lint, self.cur, Some(&specs), &self.sess);
         // Setting to a non-forbid level is an error if the lint previously had
         // a forbid level. Note that this is not necessarily true even with a
         // `#[forbid(..)]` attribute present, as that is overriden by `--cap-lints`.
@@ -138,9 +141,7 @@ fn insert_spec(
         // This means that this only errors if we're truly lowering the lint
         // level from forbid.
         if level != Level::Forbid {
-            if let (Level::Forbid, old_src) =
-                self.sets.get_lint_level(id.lint, self.cur, Some(&specs), &self.sess)
-            {
+            if let Level::Forbid = old_level {
                 // Backwards compatibility check:
                 //
                 // We used to not consider `forbid(lint_group)`
@@ -152,9 +153,6 @@ fn insert_spec(
                     LintLevelSource::Default => false,
                     LintLevelSource::Node(symbol, _, _) => self.store.is_lint_group(symbol),
                     LintLevelSource::CommandLine(symbol, _) => self.store.is_lint_group(symbol),
-                    LintLevelSource::ForceWarn(_symbol) => {
-                        bug!("forced warn lint returned a forbid lint level")
-                    }
                 };
                 debug!(
                     "fcw_warning={:?}, specs.get(&id) = {:?}, old_src={:?}, id_name={:?}",
@@ -179,7 +177,6 @@ fn insert_spec(
                         LintLevelSource::CommandLine(_, _) => {
                             diag_builder.note("`forbid` lint level was set on command line");
                         }
-                        _ => bug!("forced warn lint returned a forbid lint level"),
                     }
                     diag_builder.emit();
                 };
@@ -216,7 +213,11 @@ fn insert_spec(
                 }
             }
         }
-        specs.insert(id, (level, src));
+        if let Level::ForceWarn = old_level {
+            specs.insert(id, (old_level, old_src));
+        } else {
+            specs.insert(id, (level, src));
+        }
     }
 
     /// Pushes a list of AST lint attributes onto this context.
index 202297152de584312c5b5bb7d0ad9349d652630f..89453e8e73a2bc4fd7c440136995a9d2bf34b5f5 100644 (file)
@@ -51,6 +51,7 @@ pub enum Applicability {
 pub enum Level {
     Allow,
     Warn,
+    ForceWarn,
     Deny,
     Forbid,
 }
@@ -63,6 +64,7 @@ pub fn as_str(self) -> &'static str {
         match self {
             Level::Allow => "allow",
             Level::Warn => "warn",
+            Level::ForceWarn => "force-warns",
             Level::Deny => "deny",
             Level::Forbid => "forbid",
         }
index 8d0fdf9128a51d70c315e84e0084497ef3291422..7ac7720a7c56367afbf09be4eeb1975d2ccf9ff6 100644 (file)
@@ -1,7 +1,7 @@
 use std::cmp;
 
 use crate::ich::StableHashingContext;
-use rustc_data_structures::fx::{FxHashMap, FxHashSet};
+use rustc_data_structures::fx::FxHashMap;
 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
 use rustc_errors::{DiagnosticBuilder, DiagnosticId};
 use rustc_hir::HirId;
@@ -28,9 +28,6 @@ pub enum LintLevelSource {
     /// The provided `Level` is the level specified on the command line.
     /// (The actual level may be lower due to `--cap-lints`.)
     CommandLine(Symbol, Level),
-
-    /// Lint is being forced to warn no matter what.
-    ForceWarn(Symbol),
 }
 
 impl LintLevelSource {
@@ -39,7 +36,6 @@ pub fn name(&self) -> Symbol {
             LintLevelSource::Default => symbol::kw::Default,
             LintLevelSource::Node(name, _, _) => name,
             LintLevelSource::CommandLine(name, _) => name,
-            LintLevelSource::ForceWarn(name) => name,
         }
     }
 
@@ -48,7 +44,6 @@ pub fn span(&self) -> Span {
             LintLevelSource::Default => DUMMY_SP,
             LintLevelSource::Node(_, span, _) => span,
             LintLevelSource::CommandLine(_, _) => DUMMY_SP,
-            LintLevelSource::ForceWarn(_) => DUMMY_SP,
         }
     }
 }
@@ -60,7 +55,6 @@ pub fn span(&self) -> Span {
 pub struct LintLevelSets {
     pub list: Vec<LintSet>,
     pub lint_cap: Level,
-    pub force_warns: FxHashSet<LintId>,
 }
 
 #[derive(Debug)]
@@ -79,11 +73,7 @@ pub enum LintSet {
 
 impl LintLevelSets {
     pub fn new() -> Self {
-        LintLevelSets {
-            list: Vec::new(),
-            lint_cap: Level::Forbid,
-            force_warns: FxHashSet::default(),
-        }
+        LintLevelSets { list: Vec::new(), lint_cap: Level::Forbid }
     }
 
     pub fn get_lint_level(
@@ -93,11 +83,6 @@ pub fn get_lint_level(
         aux: Option<&FxHashMap<LintId, LevelAndSource>>,
         sess: &Session,
     ) -> LevelAndSource {
-        // Check whether we should always warn
-        if self.force_warns.contains(&LintId::of(lint)) {
-            return (Level::Warn, LintLevelSource::ForceWarn(Symbol::intern(lint.name)));
-        }
-
         let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
 
         // If `level` is none then we actually assume the default level for this
@@ -191,11 +176,11 @@ pub fn level_and_source(
 impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
     #[inline]
     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
-        let LintLevelMap { ref sets, ref id_to_set, .. } = *self;
+        let LintLevelMap { ref sets, ref id_to_set } = *self;
 
         id_to_set.hash_stable(hcx, hasher);
 
-        let LintLevelSets { ref list, lint_cap, .. } = *sets;
+        let LintLevelSets { ref list, lint_cap } = *sets;
 
         lint_cap.hash_stable(hcx, hasher);
 
@@ -273,8 +258,8 @@ fn struct_lint_level_impl(
                     return;
                 }
             }
-            (Level::Warn, Some(span)) => sess.struct_span_warn(span, ""),
-            (Level::Warn, None) => sess.struct_warn(""),
+            (Level::Warn | Level::ForceWarn, Some(span)) => sess.struct_span_warn(span, ""),
+            (Level::Warn | Level::ForceWarn, None) => sess.struct_warn(""),
             (Level::Deny | Level::Forbid, Some(span)) => sess.struct_span_err(span, ""),
             (Level::Deny | Level::Forbid, None) => sess.struct_err(""),
         };
@@ -316,6 +301,7 @@ fn struct_lint_level_impl(
                     Level::Deny => "-D",
                     Level::Forbid => "-F",
                     Level::Allow => "-A",
+                    Level::ForceWarn => "--force-warns",
                 };
                 let hyphen_case_lint_name = name.replace("_", "-");
                 if lint_flag_val.as_str() == name {
@@ -361,13 +347,6 @@ fn struct_lint_level_impl(
                     );
                 }
             }
-            LintLevelSource::ForceWarn(_) => {
-                sess.diag_note_once(
-                    &mut err,
-                    DiagnosticMessageId::from(lint),
-                    "warning forced by `force-warns` commandline option",
-                );
-            }
         }
 
         err.code(DiagnosticId::Lint { name, has_future_breakage });
index 0e0e934c7655f5796d4b2ace32b2a18a62913552..37c61e614f3e338f2a9db94d16fb7e851ac5d142 100644 (file)
@@ -4,7 +4,7 @@ warning: hidden lifetime parameters in types are deprecated
 LL | fn foo(x: &Foo) {}
    |            ^^^- help: indicate the anonymous lifetime: `<'_>`
    |
-   = note: warning forced by `force-warns` commandline option
+   = note: requested on the command line with `--force-warns elided-lifetimes-in-paths`
 
 warning: 1 warning emitted
 
index bad12f94b18053cec336742f321f7ce3b1e37b2c..56b2f0236a5d373c4f037e0eef314b98141fc011 100644 (file)
@@ -6,7 +6,7 @@ LL | const C: i32 = 1 / 0;
    |                |
    |                attempt to divide `1_i32` by zero
    |
-   = note: warning forced by `force-warns` commandline option
+   = note: requested on the command line with `--force-warns const-err`
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
 
index 145798a32a9eff5c8aa051de6e9d8f30087c6c71..7eb980a129708d8f4e2bc562d9463bb1f767ff1e 100644 (file)
@@ -4,7 +4,7 @@ warning: function is never used: `dead_function`
 LL | fn dead_function() {}
    |    ^^^^^^^^^^^^^
    |
-   = note: warning forced by `force-warns` commandline option
+   = note: requested on the command line with `--force-warns dead-code`
 
 warning: 1 warning emitted
 
index 4b004cf367d94cb4b525ac4c866762bfeb779835..8b9bb5a74cfc10762a0174b8f8849d5ac7640515 100644 (file)
@@ -6,7 +6,7 @@ LL | const C: i32 = 1 / 0;
    |                |
    |                attempt to divide `1_i32` by zero
    |
-   = note: warning forced by `force-warns` commandline option
+   = note: requested on the command line with `--force-warns const-err`
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
 
index 577dbe1fea8df452eddc5de2cef378ee62c8ea0e..ebdb022f2a20b05770ff6b0f5805beaff854ff44 100644 (file)
@@ -4,7 +4,7 @@ warning: function is never used: `dead_function`
 LL | fn dead_function() {}
    |    ^^^^^^^^^^^^^
    |
-   = note: warning forced by `force-warns` commandline option
+   = note: requested on the command line with `--force-warns dead-code`
 
 warning: 1 warning emitted
 
index 8665fa2610a3a08f392f08dad25ee59ca240a08d..7429e77fe83e11aae881ec46aca12eb86b4d634e 100644 (file)
@@ -4,7 +4,7 @@ warning: function `FUNCTION` should have a snake case name
 LL | pub fn FUNCTION() {}
    |        ^^^^^^^^ help: convert the identifier to snake case: `function`
    |
-   = note: warning forced by `force-warns` commandline option
+   = note: `--force-warns non-snake-case` implied by `--force-warns nonstandard-style`
 
 warning: 1 warning emitted
 
index 8ecfe3a15b8f6cf9ed34c86f5a91ff3b3666c4d2..4f7bba6bba1c9fdb5b5e282350631ec323432c18 100644 (file)
@@ -4,7 +4,7 @@ warning: trait objects without an explicit `dyn` are deprecated
 LL | pub fn function(_x: Box<SomeTrait>) {}
    |                         ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
    |
-   = note: warning forced by `force-warns` commandline option
+   = note: requested on the command line with `--force-warns bare-trait-objects`
    = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
    = note: for more information, see issue #80165 <https://github.com/rust-lang/rust/issues/80165>
 
index 232edf4f1ef25b06902aaaf2dfd3b6aa5d1428cb..65de6c9e287f0045f52a30c7ae0efe9b77aba8d2 100644 (file)
@@ -4,7 +4,7 @@ warning: trait objects without an explicit `dyn` are deprecated
 LL | pub fn function(_x: Box<SomeTrait>) {}
    |                         ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
    |
-   = note: warning forced by `force-warns` commandline option
+   = note: `--force-warns bare-trait-objects` implied by `--force-warns rust-2018-idioms`
    = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
    = note: for more information, see issue #80165 <https://github.com/rust-lang/rust/issues/80165>
 
index 82781984f0cea55a781695cdb156c1f51165a2b1..fd3397c916a6dc9c3580f1ac35f8576ece1ba685 100644 (file)
@@ -4,7 +4,7 @@ warning: trait objects without an explicit `dyn` are deprecated
 LL | pub fn function(_x: Box<SomeTrait>) {}
    |                         ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
    |
-   = note: warning forced by `force-warns` commandline option
+   = note: `--force-warns bare-trait-objects` implied by `--force-warns rust-2018-idioms`
    = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
    = note: for more information, see issue #80165 <https://github.com/rust-lang/rust/issues/80165>