Skip to content

Commit

Permalink
Make ForceWarn a lint level.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jun 26, 2021
1 parent 6830052 commit e42271d
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 76 deletions.
38 changes: 14 additions & 24 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,8 @@ impl LintStore {
}
}

/// 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)),
Expand All @@ -367,23 +361,19 @@ impl LintStore {
};

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
}
}

Expand Down
31 changes: 16 additions & 15 deletions compiler/rustc_lint/src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<'s> LintLevelsBuilder<'s> {
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
Expand All @@ -110,12 +110,13 @@ impl<'s> LintLevelsBuilder<'s> {
}

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));
}
}

Expand All @@ -131,16 +132,16 @@ impl<'s> LintLevelsBuilder<'s> {
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`.
//
// 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)`
Expand All @@ -152,9 +153,6 @@ impl<'s> LintLevelsBuilder<'s> {
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={:?}",
Expand All @@ -179,7 +177,6 @@ impl<'s> LintLevelsBuilder<'s> {
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();
};
Expand Down Expand Up @@ -216,7 +213,11 @@ impl<'s> LintLevelsBuilder<'s> {
}
}
}
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.
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub enum Applicability {
pub enum Level {
Allow,
Warn,
ForceWarn,
Deny,
Forbid,
}
Expand All @@ -63,6 +64,7 @@ impl Level {
match self {
Level::Allow => "allow",
Level::Warn => "warn",
Level::ForceWarn => "force-warns",
Level::Deny => "deny",
Level::Forbid => "forbid",
}
Expand Down
35 changes: 7 additions & 28 deletions compiler/rustc_middle/src/lint.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -39,7 +36,6 @@ impl LintLevelSource {
LintLevelSource::Default => symbol::kw::Default,
LintLevelSource::Node(name, _, _) => name,
LintLevelSource::CommandLine(name, _) => name,
LintLevelSource::ForceWarn(name) => name,
}
}

Expand All @@ -48,7 +44,6 @@ impl LintLevelSource {
LintLevelSource::Default => DUMMY_SP,
LintLevelSource::Node(_, span, _) => span,
LintLevelSource::CommandLine(_, _) => DUMMY_SP,
LintLevelSource::ForceWarn(_) => DUMMY_SP,
}
}
}
Expand All @@ -60,7 +55,6 @@ pub type LevelAndSource = (Level, LintLevelSource);
pub struct LintLevelSets {
pub list: Vec<LintSet>,
pub lint_cap: Level,
pub force_warns: FxHashSet<LintId>,
}

#[derive(Debug)]
Expand All @@ -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(
Expand All @@ -93,11 +83,6 @@ impl LintLevelSets {
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
Expand Down Expand Up @@ -191,11 +176,11 @@ impl LintLevelMap {
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);

Expand Down Expand Up @@ -273,8 +258,8 @@ pub fn struct_lint_level<'s, 'd>(
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(""),
};
Expand Down Expand Up @@ -316,6 +301,7 @@ pub fn struct_lint_level<'s, 'd>(
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 {
Expand Down Expand Up @@ -361,13 +347,6 @@ pub fn struct_lint_level<'s, 'd>(
);
}
}
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 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/force-warn/force-allowed-warning.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/force-warn/force-warn-group.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down

0 comments on commit e42271d

Please sign in to comment.