Skip to content

Commit 7319864

Browse files
committed
Auto merge of #10811 - y21:issue10635, r=Manishearth
[`match_wild_err_arm`]: do not lint in const contexts Fixes #10635. changelog: [`match_wild_err_arm`]: do not lint in const contexts as `Result::{unwrap, expect}` is not const-stable
2 parents 435a8ad + 3e1302f commit 7319864

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

clippy_lints/src/matches/match_wild_err_arm.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ use clippy_utils::diagnostics::span_lint_and_note;
22
use clippy_utils::macros::{is_panic, root_macro_call};
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use clippy_utils::visitors::is_local_used;
5-
use clippy_utils::{is_wild, peel_blocks_with_stmt};
5+
use clippy_utils::{in_constant, is_wild, peel_blocks_with_stmt};
66
use rustc_hir::{Arm, Expr, PatKind};
77
use rustc_lint::LateContext;
88
use rustc_span::symbol::{kw, sym};
99

1010
use super::MATCH_WILD_ERR_ARM;
1111

1212
pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<'tcx>]) {
13+
// `unwrap`/`expect` is not (yet) const, so we want to allow this in const contexts for now
14+
if in_constant(cx, ex.hir_id) {
15+
return;
16+
}
17+
1318
let ex_ty = cx.typeck_results().expr_ty(ex).peel_refs();
1419
if is_type_diagnostic_item(cx, ex_ty, sym::Result) {
1520
for arm in arms {

tests/ui/match_wild_err_arm.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
#![feature(exclusive_range_pattern)]
2-
#![allow(clippy::match_same_arms)]
2+
#![allow(clippy::match_same_arms, dead_code)]
33
#![warn(clippy::match_wild_err_arm)]
44

5+
fn issue_10635() {
6+
enum Error {
7+
A,
8+
B,
9+
}
10+
11+
// Don't trigger in const contexts. Const unwrap is not yet stable
12+
const X: () = match Ok::<_, Error>(()) {
13+
Ok(x) => x,
14+
Err(_) => panic!(),
15+
};
16+
}
17+
518
fn match_wild_err_arm() {
619
let x: Result<i32, &str> = Ok(3);
720

tests/ui/match_wild_err_arm.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: `Err(_)` matches all errors
2-
--> $DIR/match_wild_err_arm.rs:11:9
2+
--> $DIR/match_wild_err_arm.rs:24:9
33
|
44
LL | Err(_) => panic!("err"),
55
| ^^^^^^
@@ -8,23 +8,23 @@ LL | Err(_) => panic!("err"),
88
= note: `-D clippy::match-wild-err-arm` implied by `-D warnings`
99

1010
error: `Err(_)` matches all errors
11-
--> $DIR/match_wild_err_arm.rs:17:9
11+
--> $DIR/match_wild_err_arm.rs:30:9
1212
|
1313
LL | Err(_) => panic!(),
1414
| ^^^^^^
1515
|
1616
= note: match each error separately or use the error output, or use `.expect(msg)` if the error case is unreachable
1717

1818
error: `Err(_)` matches all errors
19-
--> $DIR/match_wild_err_arm.rs:23:9
19+
--> $DIR/match_wild_err_arm.rs:36:9
2020
|
2121
LL | Err(_) => {
2222
| ^^^^^^
2323
|
2424
= note: match each error separately or use the error output, or use `.expect(msg)` if the error case is unreachable
2525

2626
error: `Err(_e)` matches all errors
27-
--> $DIR/match_wild_err_arm.rs:31:9
27+
--> $DIR/match_wild_err_arm.rs:44:9
2828
|
2929
LL | Err(_e) => panic!(),
3030
| ^^^^^^^

0 commit comments

Comments
 (0)