Skip to content

Commit 33cc7b1

Browse files
committed
New force_warn diagnostic builder and ensure cap-lints doesn't reduce force_warn level
1 parent a3d6905 commit 33cc7b1

File tree

7 files changed

+67
-15
lines changed

7 files changed

+67
-15
lines changed

compiler/rustc_errors/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,24 @@ impl Handler {
521521
}
522522

523523
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
524+
/// Cancel the builder if warnings cannot be emitted
524525
pub fn struct_span_warn(&self, span: impl Into<MultiSpan>, msg: &str) -> DiagnosticBuilder<'_> {
525526
let mut result = self.struct_warn(msg);
526527
result.set_span(span);
527528
result
528529
}
529530

531+
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
532+
pub fn struct_span_force_warn(
533+
&self,
534+
span: impl Into<MultiSpan>,
535+
msg: &str,
536+
) -> DiagnosticBuilder<'_> {
537+
let mut result = self.struct_force_warn(msg);
538+
result.set_span(span);
539+
result
540+
}
541+
530542
/// Construct a builder at the `Allow` level at the given `span` and with the `msg`.
531543
pub fn struct_span_allow(
532544
&self,
@@ -552,6 +564,7 @@ impl Handler {
552564
}
553565

554566
/// Construct a builder at the `Warning` level with the `msg`.
567+
/// Cancel the builder if warnings cannot be emitted
555568
pub fn struct_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
556569
let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
557570
if !self.flags.can_emit_warnings {
@@ -560,6 +573,11 @@ impl Handler {
560573
result
561574
}
562575

576+
/// Construct a builder at the `Warning` level with the `msg`.
577+
pub fn struct_force_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
578+
DiagnosticBuilder::new(self, Level::Warning, msg)
579+
}
580+
563581
/// Construct a builder at the `Allow` level with the `msg`.
564582
pub fn struct_allow(&self, msg: &str) -> DiagnosticBuilder<'_> {
565583
DiagnosticBuilder::new(self, Level::Allow, msg)

compiler/rustc_middle/src/lint.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ impl LintLevelSets {
108108
}
109109
}
110110

111-
// Ensure that we never exceed the `--cap-lints` argument.
112-
level = cmp::min(level, self.lint_cap);
111+
// Ensure that we never exceed the `--cap-lints` argument
112+
// unless the source is a --force-warn
113+
level = if let LintLevelSource::CommandLine(_, Level::ForceWarn) = src {
114+
level
115+
} else {
116+
cmp::min(level, self.lint_cap)
117+
};
113118

114119
if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
115120
// Ensure that we never exceed driver level.
@@ -257,22 +262,14 @@ pub fn struct_lint_level<'s, 'd>(
257262
} else {
258263
sess.struct_allow("")
259264
}
260-
} else if is_force_warn {
261-
let mut err = if let Some(span) = span {
262-
sess.struct_span_warn(span, "")
263-
} else {
264-
sess.struct_warn("")
265-
};
266-
// Ensure force-warn warns even if the diagnostic has
267-
// been canceled for reasons like `--cap-lints`
268-
err.level = rustc_errors::Level::Warning;
269-
err
270265
} else {
271266
return;
272267
}
273268
}
274-
(Level::Warn | Level::ForceWarn, Some(span)) => sess.struct_span_warn(span, ""),
275-
(Level::Warn | Level::ForceWarn, None) => sess.struct_warn(""),
269+
(Level::Warn, Some(span)) => sess.struct_span_warn(span, ""),
270+
(Level::Warn, None) => sess.struct_warn(""),
271+
(Level::ForceWarn, Some(span)) => sess.struct_span_force_warn(span, ""),
272+
(Level::ForceWarn, None) => sess.struct_force_warn(""),
276273
(Level::Deny | Level::Forbid, Some(span)) => sess.struct_span_err(span, ""),
277274
(Level::Deny | Level::Forbid, None) => sess.struct_err(""),
278275
};

compiler/rustc_session/src/session.rs

+10
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,13 @@ impl Session {
369369
pub fn struct_span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'_> {
370370
self.diagnostic().struct_span_warn(sp, msg)
371371
}
372+
pub fn struct_span_force_warn<S: Into<MultiSpan>>(
373+
&self,
374+
sp: S,
375+
msg: &str,
376+
) -> DiagnosticBuilder<'_> {
377+
self.diagnostic().struct_span_force_warn(sp, msg)
378+
}
372379
pub fn struct_span_warn_with_code<S: Into<MultiSpan>>(
373380
&self,
374381
sp: S,
@@ -380,6 +387,9 @@ impl Session {
380387
pub fn struct_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
381388
self.diagnostic().struct_warn(msg)
382389
}
390+
pub fn struct_force_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
391+
self.diagnostic().struct_force_warn(msg)
392+
}
383393
pub fn struct_span_allow<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'_> {
384394
self.diagnostic().struct_span_allow(sp, msg)
385395
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// compile-flags: --cap-lints warn --force-warns rust-2021-compatibility -Zunstable-options
2+
// check-pass
3+
#![allow(ellipsis_inclusive_range_patterns)]
4+
5+
pub fn f() -> bool {
6+
let x = 123;
7+
match x {
8+
0...100 => true,
9+
//~^ WARN range patterns are deprecated
10+
//~| WARN this is accepted in the current edition
11+
_ => false,
12+
}
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
warning: `...` range patterns are deprecated
2+
--> $DIR/force-warn-cap-lints-warn.rs:8:10
3+
|
4+
LL | 0...100 => true,
5+
| ^^^ help: use `..=` for an inclusive range
6+
|
7+
= note: `--force-warns ellipsis-inclusive-range-patterns` implied by `--force-warns rust-2021-compatibility`
8+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
9+
= note: for more information, see issue #80165 <https://github.com/rust-lang/rust/issues/80165>
10+
11+
warning: 1 warning emitted
12+

src/test/ui/lint/force-warn/force-warns-cap-lints.stderr renamed to src/test/ui/lint/force-warn/force-warns-cap-lints-allow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: trait objects without an explicit `dyn` are deprecated
2-
--> $DIR/force-warns-cap-lints.rs:6:25
2+
--> $DIR/force-warns-cap-lints-allow.rs:6:25
33
|
44
LL | pub fn function(_x: Box<SomeTrait>) {}
55
| ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`

0 commit comments

Comments
 (0)