Skip to content

Commit 18e7783

Browse files
committed
Warn when lint level is set on a match arm
1 parent 5cf6929 commit 18e7783

File tree

6 files changed

+105
-28
lines changed

6 files changed

+105
-28
lines changed

Diff for: compiler/rustc_mir_build/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ mir_build_non_exhaustive_omitted_pattern = some variants are not matched explici
221221
.help = ensure that all variants are matched explicitly by adding the suggested match arms
222222
.note = the matched value is of type `{$scrut_ty}` and the `non_exhaustive_omitted_patterns` attribute was found
223223
224+
mir_build_non_exhaustive_omitted_pattern_lint_on_arm = the `non_exhaustive_omitted_pattern` lint level must be set on the whole match
225+
.help = it used to make sense to set the lint level on an individual match arm, but that is no longer the case
226+
224227
mir_build_non_exhaustive_patterns_type_not_empty = non-exhaustive patterns: type `{$ty}` is non-empty
225228
.def_note = `{$peeled_ty}` defined here
226229
.type_note = the matched value is of type `{$ty}`

Diff for: compiler/rustc_mir_build/src/errors.rs

+5
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,11 @@ pub(crate) struct NonExhaustiveOmittedPattern<'tcx> {
794794
pub uncovered: Uncovered<'tcx>,
795795
}
796796

797+
#[derive(LintDiagnostic)]
798+
#[diag(mir_build_non_exhaustive_omitted_pattern_lint_on_arm)]
799+
#[help]
800+
pub(crate) struct NonExhaustiveOmittedPatternLintOnArm;
801+
797802
#[derive(Subdiagnostic)]
798803
#[label(mir_build_uncovered)]
799804
pub(crate) struct Uncovered<'tcx> {

Diff for: compiler/rustc_mir_build/src/thir/pattern/usefulness.rs

+40-23
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@
308308
use self::ArmType::*;
309309
use self::Usefulness::*;
310310
use super::deconstruct_pat::{Constructor, ConstructorSet, DeconstructedPat, WitnessPat};
311-
use crate::errors::{NonExhaustiveOmittedPattern, Uncovered};
311+
use crate::errors::{NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Uncovered};
312312

313313
use rustc_data_structures::captures::Captures;
314314

@@ -1021,30 +1021,47 @@ pub(crate) fn compute_match_usefulness<'p, 'tcx>(
10211021

10221022
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
10231023
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.
1024-
if cx.refutable
1025-
&& non_exhaustiveness_witnesses.is_empty()
1026-
&& !matches!(
1024+
if cx.refutable && non_exhaustiveness_witnesses.is_empty() {
1025+
if !matches!(
10271026
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, lint_root).0,
10281027
rustc_session::lint::Level::Allow
1029-
)
1030-
{
1031-
let pat_column = arms.iter().flat_map(|arm| arm.pat.flatten_or_pat()).collect::<Vec<_>>();
1032-
let witnesses = collect_nonexhaustive_missing_variants(cx, &pat_column);
1033-
1034-
if !witnesses.is_empty() {
1035-
// Report that a match of a `non_exhaustive` enum marked with `non_exhaustive_omitted_patterns`
1036-
// is not exhaustive enough.
1037-
//
1038-
// NB: The partner lint for structs lives in `compiler/rustc_hir_analysis/src/check/pat.rs`.
1039-
cx.tcx.emit_spanned_lint(
1040-
NON_EXHAUSTIVE_OMITTED_PATTERNS,
1041-
lint_root,
1042-
scrut_span,
1043-
NonExhaustiveOmittedPattern {
1044-
scrut_ty,
1045-
uncovered: Uncovered::new(scrut_span, cx, witnesses),
1046-
},
1047-
);
1028+
) {
1029+
let pat_column =
1030+
arms.iter().flat_map(|arm| arm.pat.flatten_or_pat()).collect::<Vec<_>>();
1031+
let witnesses = collect_nonexhaustive_missing_variants(cx, &pat_column);
1032+
1033+
if !witnesses.is_empty() {
1034+
// Report that a match of a `non_exhaustive` enum marked with `non_exhaustive_omitted_patterns`
1035+
// is not exhaustive enough.
1036+
//
1037+
// NB: The partner lint for structs lives in `compiler/rustc_hir_analysis/src/check/pat.rs`.
1038+
cx.tcx.emit_spanned_lint(
1039+
NON_EXHAUSTIVE_OMITTED_PATTERNS,
1040+
lint_root,
1041+
scrut_span,
1042+
NonExhaustiveOmittedPattern {
1043+
scrut_ty,
1044+
uncovered: Uncovered::new(scrut_span, cx, witnesses),
1045+
},
1046+
);
1047+
}
1048+
} else {
1049+
// We used to allow putting the `#[allow(non_exhaustive_omitted_patterns)]` on a match
1050+
// arm. This no longer makes sense so we warn users, to avoid silently breaking their
1051+
// usage of the lint.
1052+
for arm in arms {
1053+
if !matches!(
1054+
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, arm.hir_id).0,
1055+
rustc_session::lint::Level::Allow
1056+
) {
1057+
cx.tcx.emit_spanned_lint(
1058+
NON_EXHAUSTIVE_OMITTED_PATTERNS,
1059+
arm.hir_id,
1060+
arm.pat.span(),
1061+
NonExhaustiveOmittedPatternLintOnArm,
1062+
);
1063+
}
1064+
}
10481065
}
10491066
}
10501067

Diff for: tests/ui/rfcs/rfc-2008-non-exhaustive/omitted-patterns-dont-lint-on-arm.lint.stderr

+40-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,44 @@ note: the lint level is defined here
2626
LL | #[cfg_attr(lint, deny(non_exhaustive_omitted_patterns))]
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2828

29-
error: aborting due to 2 previous errors
29+
error: the `non_exhaustive_omitted_pattern` lint level must be set on the whole match
30+
--> $DIR/omitted-patterns-dont-lint-on-arm.rs:34:9
31+
|
32+
LL | _ => {}
33+
| ^
34+
|
35+
= help: it used to make sense to set the lint level on an individual match arm, but that is no longer the case
36+
note: the lint level is defined here
37+
--> $DIR/omitted-patterns-dont-lint-on-arm.rs:33:16
38+
|
39+
LL | #[deny(non_exhaustive_omitted_patterns)]
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
42+
error: the `non_exhaustive_omitted_pattern` lint level must be set on the whole match
43+
--> $DIR/omitted-patterns-dont-lint-on-arm.rs:41:9
44+
|
45+
LL | _ => {}
46+
| ^
47+
|
48+
= help: it used to make sense to set the lint level on an individual match arm, but that is no longer the case
49+
note: the lint level is defined here
50+
--> $DIR/omitted-patterns-dont-lint-on-arm.rs:40:31
51+
|
52+
LL | #[cfg_attr(lint, deny(non_exhaustive_omitted_patterns))]
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54+
55+
warning: the `non_exhaustive_omitted_pattern` lint level must be set on the whole match
56+
--> $DIR/omitted-patterns-dont-lint-on-arm.rs:48:9
57+
|
58+
LL | _ => {}
59+
| ^
60+
|
61+
= help: it used to make sense to set the lint level on an individual match arm, but that is no longer the case
62+
note: the lint level is defined here
63+
--> $DIR/omitted-patterns-dont-lint-on-arm.rs:47:31
64+
|
65+
LL | #[cfg_attr(lint, warn(non_exhaustive_omitted_patterns))]
66+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
68+
error: aborting due to 4 previous errors; 1 warning emitted
3069

Diff for: tests/ui/rfcs/rfc-2008-non-exhaustive/omitted-patterns-dont-lint-on-arm.normal.stderr

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,18 @@ note: the lint level is defined here
1212
LL | #[deny(non_exhaustive_omitted_patterns)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

15-
error: aborting due to previous error
15+
error: the `non_exhaustive_omitted_pattern` lint level must be set on the whole match
16+
--> $DIR/omitted-patterns-dont-lint-on-arm.rs:34:9
17+
|
18+
LL | _ => {}
19+
| ^
20+
|
21+
= help: it used to make sense to set the lint level on an individual match arm, but that is no longer the case
22+
note: the lint level is defined here
23+
--> $DIR/omitted-patterns-dont-lint-on-arm.rs:33:16
24+
|
25+
LL | #[deny(non_exhaustive_omitted_patterns)]
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27+
28+
error: aborting due to 2 previous errors
1629

Diff for: tests/ui/rfcs/rfc-2008-non-exhaustive/omitted-patterns-dont-lint-on-arm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ fn main() {
3131
NonExhaustiveEnum::Unit => {}
3232
NonExhaustiveEnum::Tuple(_) => {}
3333
#[deny(non_exhaustive_omitted_patterns)]
34-
_ => {}
34+
_ => {} //~ ERROR lint level must be set on the whole match
3535
}
3636

3737
match val {
3838
NonExhaustiveEnum::Unit => {}
3939
NonExhaustiveEnum::Tuple(_) => {}
4040
#[cfg_attr(lint, deny(non_exhaustive_omitted_patterns))]
41-
_ => {}
41+
_ => {} //[lint]~ ERROR lint level must be set on the whole match
4242
}
4343

4444
match val {
4545
NonExhaustiveEnum::Unit => {}
4646
NonExhaustiveEnum::Tuple(_) => {}
4747
#[cfg_attr(lint, warn(non_exhaustive_omitted_patterns))]
48-
_ => {}
48+
_ => {} //[lint]~ WARN lint level must be set on the whole match
4949
}
5050
}

0 commit comments

Comments
 (0)