Skip to content

Commit 440b639

Browse files
committed
Check whether -F is ever overridden on the command line
1 parent 6936ca8 commit 440b639

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

compiler/rustc_lint/src/levels.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'s> LintLevelsBuilder<'s> {
8484
}
8585

8686
fn process_command_line(&mut self, sess: &Session, store: &LintStore) {
87-
let mut specs = FxHashMap::default();
87+
self.sets.list.push(LintSet::CommandLine { specs: FxHashMap::default() });
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 {
@@ -105,7 +105,17 @@ impl<'s> LintLevelsBuilder<'s> {
105105
for id in ids {
106106
self.check_gated_lint(id, DUMMY_SP);
107107
let src = LintLevelSource::CommandLine(lint_flag_val, orig_level);
108-
specs.insert(id, (level, src));
108+
let specs = match self.sets.list.last().unwrap() {
109+
LintSet::CommandLine { ref specs } => specs,
110+
_ => unreachable!(),
111+
};
112+
if self.check_before_insert_spec(specs, id, (level, src)) {
113+
let specs = match self.sets.list.last_mut().unwrap() {
114+
LintSet::CommandLine { ref mut specs } => specs,
115+
_ => unreachable!(),
116+
};
117+
specs.insert(id, (level, src));
118+
}
109119
}
110120
}
111121

@@ -122,15 +132,12 @@ impl<'s> LintLevelsBuilder<'s> {
122132
self.sets.list.push(LintSet::CommandLine { specs });
123133
}
124134

125-
/// Attempts to insert the `id` to `level_src` map entry. If unsuccessful
126-
/// (e.g. if a forbid was already inserted on the same scope), then emits a
127-
/// diagnostic with no change to `specs`.
128-
fn insert_spec(
129-
&mut self,
130-
specs: &mut FxHashMap<LintId, LevelAndSource>,
135+
fn check_before_insert_spec(
136+
&self,
137+
specs: &FxHashMap<LintId, LevelAndSource>,
131138
id: LintId,
132139
(level, src): LevelAndSource,
133-
) {
140+
) -> bool {
134141
// Setting to a non-forbid level is an error if the lint previously had
135142
// a forbid level. Note that this is not necessarily true even with a
136143
// `#[forbid(..)]` attribute present, as that is overriden by `--cap-lints`.
@@ -212,11 +219,25 @@ impl<'s> LintLevelsBuilder<'s> {
212219
// issuing a FCW. In the FCW case, we want to
213220
// respect the new setting.
214221
if !fcw_warning {
215-
return;
222+
return false;
216223
}
217224
}
218225
}
219-
specs.insert(id, (level, src));
226+
true
227+
}
228+
229+
/// Attempts to insert the `id` to `level_src` map entry. If unsuccessful
230+
/// (e.g. if a forbid was already inserted on the same scope), then emits a
231+
/// diagnostic with no change to `specs`.
232+
fn insert_spec(
233+
&mut self,
234+
specs: &mut FxHashMap<LintId, LevelAndSource>,
235+
id: LintId,
236+
(level, src): LevelAndSource,
237+
) {
238+
if self.check_before_insert_spec(specs, id, (level, src)) {
239+
specs.insert(id, (level, src));
240+
}
220241
}
221242

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

compiler/rustc_session/src/config.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1169,16 +1169,8 @@ pub fn get_cmd_lint_options(
11691169
let mut lint_opts_with_position = vec![];
11701170
let mut describe_lints = false;
11711171

1172-
for level in [lint::Allow, lint::Warn, lint::Deny, lint::Forbid] {
1173-
for (passed_arg_pos, lint_name) in matches.opt_strs_pos(level.as_str()) {
1174-
let arg_pos = if let lint::Forbid = level {
1175-
// HACK: forbid is always specified last, so it can't be overridden.
1176-
// FIXME: remove this once <https://github.com/rust-lang/rust/issues/70819> is
1177-
// fixed and `forbid` works as expected.
1178-
usize::MAX
1179-
} else {
1180-
passed_arg_pos
1181-
};
1172+
for &level in &[lint::Allow, lint::Warn, lint::Deny, lint::Forbid] {
1173+
for (arg_pos, lint_name) in matches.opt_strs_pos(level.as_str()) {
11821174
if lint_name == "help" {
11831175
describe_lints = true;
11841176
} else {

0 commit comments

Comments
 (0)