about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRyan Levick <me@ryanlevick.com>2021-07-01 12:29:20 +0200
committerRyan Levick <me@ryanlevick.com>2021-07-01 12:29:20 +0200
commit33cc7b1fe21c82d31204e549cd2471ac8ec22aa3 (patch)
tree6f60870bfaaaa7c5377f11f33b0dc9b84d9bda46
parenta3d6905053ad60e836717976e6e4d38de67888f9 (diff)
downloadrust-33cc7b1fe21c82d31204e549cd2471ac8ec22aa3.tar.gz
rust-33cc7b1fe21c82d31204e549cd2471ac8ec22aa3.zip
New force_warn diagnostic builder and ensure cap-lints doesn't reduce force_warn level
-rw-r--r--compiler/rustc_errors/src/lib.rs18
-rw-r--r--compiler/rustc_middle/src/lint.rs25
-rw-r--r--compiler/rustc_session/src/session.rs10
-rw-r--r--src/test/ui/lint/force-warn/force-warn-cap-lints-warn.rs15
-rw-r--r--src/test/ui/lint/force-warn/force-warn-cap-lints-warn.stderr12
-rw-r--r--src/test/ui/lint/force-warn/force-warns-cap-lints-allow.rs (renamed from src/test/ui/lint/force-warn/force-warns-cap-lints.rs)0
-rw-r--r--src/test/ui/lint/force-warn/force-warns-cap-lints-allow.stderr (renamed from src/test/ui/lint/force-warn/force-warns-cap-lints.stderr)2
7 files changed, 67 insertions, 15 deletions
diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs
index c5399282a1e..a5e11541c8b 100644
--- a/compiler/rustc_errors/src/lib.rs
+++ b/compiler/rustc_errors/src/lib.rs
@@ -521,12 +521,24 @@ impl Handler {
     }
 
     /// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
+    /// Cancel the builder if warnings cannot be emitted
     pub fn struct_span_warn(&self, span: impl Into<MultiSpan>, msg: &str) -> DiagnosticBuilder<'_> {
         let mut result = self.struct_warn(msg);
         result.set_span(span);
         result
     }
 
+    /// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
+    pub fn struct_span_force_warn(
+        &self,
+        span: impl Into<MultiSpan>,
+        msg: &str,
+    ) -> DiagnosticBuilder<'_> {
+        let mut result = self.struct_force_warn(msg);
+        result.set_span(span);
+        result
+    }
+
     /// Construct a builder at the `Allow` level at the given `span` and with the `msg`.
     pub fn struct_span_allow(
         &self,
@@ -552,6 +564,7 @@ impl Handler {
     }
 
     /// Construct a builder at the `Warning` level with the `msg`.
+    /// Cancel the builder if warnings cannot be emitted
     pub fn struct_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
         let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
         if !self.flags.can_emit_warnings {
@@ -560,6 +573,11 @@ impl Handler {
         result
     }
 
+    /// Construct a builder at the `Warning` level with the `msg`.
+    pub fn struct_force_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
+        DiagnosticBuilder::new(self, Level::Warning, msg)
+    }
+
     /// Construct a builder at the `Allow` level with the `msg`.
     pub fn struct_allow(&self, msg: &str) -> DiagnosticBuilder<'_> {
         DiagnosticBuilder::new(self, Level::Allow, msg)
diff --git a/compiler/rustc_middle/src/lint.rs b/compiler/rustc_middle/src/lint.rs
index 71cf5b2249c..b5eab8be17f 100644
--- a/compiler/rustc_middle/src/lint.rs
+++ b/compiler/rustc_middle/src/lint.rs
@@ -108,8 +108,13 @@ impl LintLevelSets {
             }
         }
 
-        // Ensure that we never exceed the `--cap-lints` argument.
-        level = cmp::min(level, self.lint_cap);
+        // Ensure that we never exceed the `--cap-lints` argument
+        // unless the source is a --force-warn
+        level = if let LintLevelSource::CommandLine(_, Level::ForceWarn) = src {
+            level
+        } else {
+            cmp::min(level, self.lint_cap)
+        };
 
         if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
             // Ensure that we never exceed driver level.
@@ -257,22 +262,14 @@ pub fn struct_lint_level<'s, 'd>(
                     } else {
                         sess.struct_allow("")
                     }
-                } else if is_force_warn {
-                    let mut err = if let Some(span) = span {
-                        sess.struct_span_warn(span, "")
-                    } else {
-                        sess.struct_warn("")
-                    };
-                    // Ensure force-warn warns even if the diagnostic has
-                    // been canceled for reasons like `--cap-lints`
-                    err.level = rustc_errors::Level::Warning;
-                    err
                 } else {
                     return;
                 }
             }
-            (Level::Warn | Level::ForceWarn, Some(span)) => sess.struct_span_warn(span, ""),
-            (Level::Warn | Level::ForceWarn, None) => sess.struct_warn(""),
+            (Level::Warn, Some(span)) => sess.struct_span_warn(span, ""),
+            (Level::Warn, None) => sess.struct_warn(""),
+            (Level::ForceWarn, Some(span)) => sess.struct_span_force_warn(span, ""),
+            (Level::ForceWarn, None) => sess.struct_force_warn(""),
             (Level::Deny | Level::Forbid, Some(span)) => sess.struct_span_err(span, ""),
             (Level::Deny | Level::Forbid, None) => sess.struct_err(""),
         };
diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs
index 2bcb7c94d53..f6b7797c2e3 100644
--- a/compiler/rustc_session/src/session.rs
+++ b/compiler/rustc_session/src/session.rs
@@ -369,6 +369,13 @@ impl Session {
     pub fn struct_span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'_> {
         self.diagnostic().struct_span_warn(sp, msg)
     }
+    pub fn struct_span_force_warn<S: Into<MultiSpan>>(
+        &self,
+        sp: S,
+        msg: &str,
+    ) -> DiagnosticBuilder<'_> {
+        self.diagnostic().struct_span_force_warn(sp, msg)
+    }
     pub fn struct_span_warn_with_code<S: Into<MultiSpan>>(
         &self,
         sp: S,
@@ -380,6 +387,9 @@ impl Session {
     pub fn struct_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
         self.diagnostic().struct_warn(msg)
     }
+    pub fn struct_force_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
+        self.diagnostic().struct_force_warn(msg)
+    }
     pub fn struct_span_allow<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'_> {
         self.diagnostic().struct_span_allow(sp, msg)
     }
diff --git a/src/test/ui/lint/force-warn/force-warn-cap-lints-warn.rs b/src/test/ui/lint/force-warn/force-warn-cap-lints-warn.rs
new file mode 100644
index 00000000000..bcfe6e5a5bd
--- /dev/null
+++ b/src/test/ui/lint/force-warn/force-warn-cap-lints-warn.rs
@@ -0,0 +1,15 @@
+// compile-flags: --cap-lints warn  --force-warns rust-2021-compatibility -Zunstable-options
+// check-pass
+#![allow(ellipsis_inclusive_range_patterns)]
+
+pub fn f() -> bool {
+    let x = 123;
+    match x {
+        0...100 => true,
+        //~^ WARN range patterns are deprecated
+        //~| WARN this is accepted in the current edition
+        _ => false,
+    }
+}
+
+fn main() {}
diff --git a/src/test/ui/lint/force-warn/force-warn-cap-lints-warn.stderr b/src/test/ui/lint/force-warn/force-warn-cap-lints-warn.stderr
new file mode 100644
index 00000000000..07e786ce7d2
--- /dev/null
+++ b/src/test/ui/lint/force-warn/force-warn-cap-lints-warn.stderr
@@ -0,0 +1,12 @@
+warning: `...` range patterns are deprecated
+  --> $DIR/force-warn-cap-lints-warn.rs:8:10
+   |
+LL |         0...100 => true,
+   |          ^^^ help: use `..=` for an inclusive range
+   |
+   = note: `--force-warns ellipsis-inclusive-range-patterns` implied by `--force-warns rust-2021-compatibility`
+   = 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>
+
+warning: 1 warning emitted
+
diff --git a/src/test/ui/lint/force-warn/force-warns-cap-lints.rs b/src/test/ui/lint/force-warn/force-warns-cap-lints-allow.rs
index e364897ec4e..e364897ec4e 100644
--- a/src/test/ui/lint/force-warn/force-warns-cap-lints.rs
+++ b/src/test/ui/lint/force-warn/force-warns-cap-lints-allow.rs
diff --git a/src/test/ui/lint/force-warn/force-warns-cap-lints.stderr b/src/test/ui/lint/force-warn/force-warns-cap-lints-allow.stderr
index 73971e9d4e5..21532024f1c 100644
--- a/src/test/ui/lint/force-warn/force-warns-cap-lints.stderr
+++ b/src/test/ui/lint/force-warn/force-warns-cap-lints-allow.stderr
@@ -1,5 +1,5 @@
 warning: trait objects without an explicit `dyn` are deprecated
-  --> $DIR/force-warns-cap-lints.rs:6:25
+  --> $DIR/force-warns-cap-lints-allow.rs:6:25
    |
 LL | pub fn function(_x: Box<SomeTrait>) {}
    |                         ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`