Skip to content

Commit 8971fff

Browse files
committed
Auto merge of rust-lang#86009 - cjgillot:fwarn, r=davidtwco
Make ForceWarn a lint level. Follow-up to rust-lang#85788 cc `@rylev`
2 parents 5d0fae5 + e42271d commit 8971fff

13 files changed

+48
-76
lines changed

compiler/rustc_lint/src/context.rs

+14-24
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,8 @@ impl LintStore {
334334
}
335335
}
336336

337-
/// Checks the validity of lint names derived from the command line. Returns
338-
/// true if the lint is valid, false otherwise.
339-
pub fn check_lint_name_cmdline(
340-
&self,
341-
sess: &Session,
342-
lint_name: &str,
343-
level: Option<Level>,
344-
) -> bool {
337+
/// Checks the validity of lint names derived from the command line
338+
pub fn check_lint_name_cmdline(&self, sess: &Session, lint_name: &str, level: Level) {
345339
let db = match self.check_lint_name(lint_name, None) {
346340
CheckLintNameResult::Ok(_) => None,
347341
CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)),
@@ -367,23 +361,19 @@ impl LintStore {
367361
};
368362

369363
if let Some(mut db) = db {
370-
if let Some(level) = level {
371-
let msg = format!(
372-
"requested on the command line with `{} {}`",
373-
match level {
374-
Level::Allow => "-A",
375-
Level::Warn => "-W",
376-
Level::Deny => "-D",
377-
Level::Forbid => "-F",
378-
},
379-
lint_name
380-
);
381-
db.note(&msg);
382-
}
364+
let msg = format!(
365+
"requested on the command line with `{} {}`",
366+
match level {
367+
Level::Allow => "-A",
368+
Level::Warn => "-W",
369+
Level::ForceWarn => "--force-warns",
370+
Level::Deny => "-D",
371+
Level::Forbid => "-F",
372+
},
373+
lint_name
374+
);
375+
db.note(&msg);
383376
db.emit();
384-
false
385-
} else {
386-
true
387377
}
388378
}
389379

compiler/rustc_lint/src/levels.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'s> LintLevelsBuilder<'s> {
8888
self.sets.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
8989

9090
for &(ref lint_name, level) in &sess.opts.lint_opts {
91-
store.check_lint_name_cmdline(sess, &lint_name, Some(level));
91+
store.check_lint_name_cmdline(sess, &lint_name, level);
9292
let orig_level = level;
9393

9494
// If the cap is less than this specified level, e.g., if we've got
@@ -110,12 +110,13 @@ impl<'s> LintLevelsBuilder<'s> {
110110
}
111111

112112
for lint_name in &sess.opts.force_warns {
113-
let valid = store.check_lint_name_cmdline(sess, lint_name, None);
114-
if valid {
115-
let lints = store
116-
.find_lints(lint_name)
117-
.unwrap_or_else(|_| bug!("A valid lint failed to produce a lint ids"));
118-
self.sets.force_warns.extend(&lints);
113+
store.check_lint_name_cmdline(sess, lint_name, Level::ForceWarn);
114+
let lints = store
115+
.find_lints(lint_name)
116+
.unwrap_or_else(|_| bug!("A valid lint failed to produce a lint ids"));
117+
for id in lints {
118+
let src = LintLevelSource::CommandLine(Symbol::intern(lint_name), Level::ForceWarn);
119+
specs.insert(id, (Level::ForceWarn, src));
119120
}
120121
}
121122

@@ -131,16 +132,16 @@ impl<'s> LintLevelsBuilder<'s> {
131132
id: LintId,
132133
(level, src): LevelAndSource,
133134
) {
135+
let (old_level, old_src) =
136+
self.sets.get_lint_level(id.lint, self.cur, Some(&specs), &self.sess);
134137
// Setting to a non-forbid level is an error if the lint previously had
135138
// a forbid level. Note that this is not necessarily true even with a
136139
// `#[forbid(..)]` attribute present, as that is overriden by `--cap-lints`.
137140
//
138141
// This means that this only errors if we're truly lowering the lint
139142
// level from forbid.
140143
if level != Level::Forbid {
141-
if let (Level::Forbid, old_src) =
142-
self.sets.get_lint_level(id.lint, self.cur, Some(&specs), &self.sess)
143-
{
144+
if let Level::Forbid = old_level {
144145
// Backwards compatibility check:
145146
//
146147
// We used to not consider `forbid(lint_group)`
@@ -152,9 +153,6 @@ impl<'s> LintLevelsBuilder<'s> {
152153
LintLevelSource::Default => false,
153154
LintLevelSource::Node(symbol, _, _) => self.store.is_lint_group(symbol),
154155
LintLevelSource::CommandLine(symbol, _) => self.store.is_lint_group(symbol),
155-
LintLevelSource::ForceWarn(_symbol) => {
156-
bug!("forced warn lint returned a forbid lint level")
157-
}
158156
};
159157
debug!(
160158
"fcw_warning={:?}, specs.get(&id) = {:?}, old_src={:?}, id_name={:?}",
@@ -179,7 +177,6 @@ impl<'s> LintLevelsBuilder<'s> {
179177
LintLevelSource::CommandLine(_, _) => {
180178
diag_builder.note("`forbid` lint level was set on command line");
181179
}
182-
_ => bug!("forced warn lint returned a forbid lint level"),
183180
}
184181
diag_builder.emit();
185182
};
@@ -216,7 +213,11 @@ impl<'s> LintLevelsBuilder<'s> {
216213
}
217214
}
218215
}
219-
specs.insert(id, (level, src));
216+
if let Level::ForceWarn = old_level {
217+
specs.insert(id, (old_level, old_src));
218+
} else {
219+
specs.insert(id, (level, src));
220+
}
220221
}
221222

222223
/// Pushes a list of AST lint attributes onto this context.

compiler/rustc_lint_defs/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub enum Applicability {
5151
pub enum Level {
5252
Allow,
5353
Warn,
54+
ForceWarn,
5455
Deny,
5556
Forbid,
5657
}
@@ -63,6 +64,7 @@ impl Level {
6364
match self {
6465
Level::Allow => "allow",
6566
Level::Warn => "warn",
67+
Level::ForceWarn => "force-warns",
6668
Level::Deny => "deny",
6769
Level::Forbid => "forbid",
6870
}

compiler/rustc_middle/src/lint.rs

+7-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cmp;
22

33
use crate::ich::StableHashingContext;
4-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4+
use rustc_data_structures::fx::FxHashMap;
55
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
66
use rustc_errors::{DiagnosticBuilder, DiagnosticId};
77
use rustc_hir::HirId;
@@ -28,9 +28,6 @@ pub enum LintLevelSource {
2828
/// The provided `Level` is the level specified on the command line.
2929
/// (The actual level may be lower due to `--cap-lints`.)
3030
CommandLine(Symbol, Level),
31-
32-
/// Lint is being forced to warn no matter what.
33-
ForceWarn(Symbol),
3431
}
3532

3633
impl LintLevelSource {
@@ -39,7 +36,6 @@ impl LintLevelSource {
3936
LintLevelSource::Default => symbol::kw::Default,
4037
LintLevelSource::Node(name, _, _) => name,
4138
LintLevelSource::CommandLine(name, _) => name,
42-
LintLevelSource::ForceWarn(name) => name,
4339
}
4440
}
4541

@@ -48,7 +44,6 @@ impl LintLevelSource {
4844
LintLevelSource::Default => DUMMY_SP,
4945
LintLevelSource::Node(_, span, _) => span,
5046
LintLevelSource::CommandLine(_, _) => DUMMY_SP,
51-
LintLevelSource::ForceWarn(_) => DUMMY_SP,
5247
}
5348
}
5449
}
@@ -60,7 +55,6 @@ pub type LevelAndSource = (Level, LintLevelSource);
6055
pub struct LintLevelSets {
6156
pub list: Vec<LintSet>,
6257
pub lint_cap: Level,
63-
pub force_warns: FxHashSet<LintId>,
6458
}
6559

6660
#[derive(Debug)]
@@ -79,11 +73,7 @@ pub enum LintSet {
7973

8074
impl LintLevelSets {
8175
pub fn new() -> Self {
82-
LintLevelSets {
83-
list: Vec::new(),
84-
lint_cap: Level::Forbid,
85-
force_warns: FxHashSet::default(),
86-
}
76+
LintLevelSets { list: Vec::new(), lint_cap: Level::Forbid }
8777
}
8878

8979
pub fn get_lint_level(
@@ -93,11 +83,6 @@ impl LintLevelSets {
9383
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
9484
sess: &Session,
9585
) -> LevelAndSource {
96-
// Check whether we should always warn
97-
if self.force_warns.contains(&LintId::of(lint)) {
98-
return (Level::Warn, LintLevelSource::ForceWarn(Symbol::intern(lint.name)));
99-
}
100-
10186
let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
10287

10388
// If `level` is none then we actually assume the default level for this
@@ -191,11 +176,11 @@ impl LintLevelMap {
191176
impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
192177
#[inline]
193178
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
194-
let LintLevelMap { ref sets, ref id_to_set, .. } = *self;
179+
let LintLevelMap { ref sets, ref id_to_set } = *self;
195180

196181
id_to_set.hash_stable(hcx, hasher);
197182

198-
let LintLevelSets { ref list, lint_cap, .. } = *sets;
183+
let LintLevelSets { ref list, lint_cap } = *sets;
199184

200185
lint_cap.hash_stable(hcx, hasher);
201186

@@ -273,8 +258,8 @@ pub fn struct_lint_level<'s, 'd>(
273258
return;
274259
}
275260
}
276-
(Level::Warn, Some(span)) => sess.struct_span_warn(span, ""),
277-
(Level::Warn, None) => sess.struct_warn(""),
261+
(Level::Warn | Level::ForceWarn, Some(span)) => sess.struct_span_warn(span, ""),
262+
(Level::Warn | Level::ForceWarn, None) => sess.struct_warn(""),
278263
(Level::Deny | Level::Forbid, Some(span)) => sess.struct_span_err(span, ""),
279264
(Level::Deny | Level::Forbid, None) => sess.struct_err(""),
280265
};
@@ -316,6 +301,7 @@ pub fn struct_lint_level<'s, 'd>(
316301
Level::Deny => "-D",
317302
Level::Forbid => "-F",
318303
Level::Allow => "-A",
304+
Level::ForceWarn => "--force-warns",
319305
};
320306
let hyphen_case_lint_name = name.replace("_", "-");
321307
if lint_flag_val.as_str() == name {
@@ -361,13 +347,6 @@ pub fn struct_lint_level<'s, 'd>(
361347
);
362348
}
363349
}
364-
LintLevelSource::ForceWarn(_) => {
365-
sess.diag_note_once(
366-
&mut err,
367-
DiagnosticMessageId::from(lint),
368-
"warning forced by `force-warns` commandline option",
369-
);
370-
}
371350
}
372351

373352
err.code(DiagnosticId::Lint { name, has_future_breakage });

src/test/ui/lint/force-warn/force-allowed-by-default-lint.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: hidden lifetime parameters in types are deprecated
44
LL | fn foo(x: &Foo) {}
55
| ^^^- help: indicate the anonymous lifetime: `<'_>`
66
|
7-
= note: warning forced by `force-warns` commandline option
7+
= note: requested on the command line with `--force-warns elided-lifetimes-in-paths`
88

99
warning: 1 warning emitted
1010

src/test/ui/lint/force-warn/force-allowed-deny-by-default-lint.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | const C: i32 = 1 / 0;
66
| |
77
| attempt to divide `1_i32` by zero
88
|
9-
= note: warning forced by `force-warns` commandline option
9+
= note: requested on the command line with `--force-warns const-err`
1010
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1111
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
1212

src/test/ui/lint/force-warn/force-allowed-warning.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: function is never used: `dead_function`
44
LL | fn dead_function() {}
55
| ^^^^^^^^^^^^^
66
|
7-
= note: warning forced by `force-warns` commandline option
7+
= note: requested on the command line with `--force-warns dead-code`
88

99
warning: 1 warning emitted
1010

src/test/ui/lint/force-warn/force-deny-by-default-lint.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | const C: i32 = 1 / 0;
66
| |
77
| attempt to divide `1_i32` by zero
88
|
9-
= note: warning forced by `force-warns` commandline option
9+
= note: requested on the command line with `--force-warns const-err`
1010
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1111
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
1212

src/test/ui/lint/force-warn/force-lint-allow-all-warnings.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: function is never used: `dead_function`
44
LL | fn dead_function() {}
55
| ^^^^^^^^^^^^^
66
|
7-
= note: warning forced by `force-warns` commandline option
7+
= note: requested on the command line with `--force-warns dead-code`
88

99
warning: 1 warning emitted
1010

src/test/ui/lint/force-warn/force-lint-group-allow-all-warnings.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: function `FUNCTION` should have a snake case name
44
LL | pub fn FUNCTION() {}
55
| ^^^^^^^^ help: convert the identifier to snake case: `function`
66
|
7-
= note: warning forced by `force-warns` commandline option
7+
= note: `--force-warns non-snake-case` implied by `--force-warns nonstandard-style`
88

99
warning: 1 warning emitted
1010

src/test/ui/lint/force-warn/force-lint-in-allowed-group.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: trait objects without an explicit `dyn` are deprecated
44
LL | pub fn function(_x: Box<SomeTrait>) {}
55
| ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
66
|
7-
= note: warning forced by `force-warns` commandline option
7+
= note: requested on the command line with `--force-warns bare-trait-objects`
88
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
99
= note: for more information, see issue #80165 <https://github.com/rust-lang/rust/issues/80165>
1010

src/test/ui/lint/force-warn/force-warn-group-allow-warning.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: trait objects without an explicit `dyn` are deprecated
44
LL | pub fn function(_x: Box<SomeTrait>) {}
55
| ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
66
|
7-
= note: warning forced by `force-warns` commandline option
7+
= note: `--force-warns bare-trait-objects` implied by `--force-warns rust-2018-idioms`
88
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
99
= note: for more information, see issue #80165 <https://github.com/rust-lang/rust/issues/80165>
1010

src/test/ui/lint/force-warn/force-warn-group.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: trait objects without an explicit `dyn` are deprecated
44
LL | pub fn function(_x: Box<SomeTrait>) {}
55
| ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
66
|
7-
= note: warning forced by `force-warns` commandline option
7+
= note: `--force-warns bare-trait-objects` implied by `--force-warns rust-2018-idioms`
88
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
99
= note: for more information, see issue #80165 <https://github.com/rust-lang/rust/issues/80165>
1010

0 commit comments

Comments
 (0)