Skip to content

Commit c35130a

Browse files
committed
fix collision between needless_match
1 parent 9ae6281 commit c35130a

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ fn check_if_let_some_or_err_and_early_return(cx: &LateContext<'_>, expr: &Expr<'
120120
if (is_early_return(sym::Option, cx, &if_block) && path_to_local_id(peel_blocks(if_then), bind_id))
121121
|| is_early_return(sym::Result, cx, &if_block);
122122
then {
123+
if let Some(else_expr) = if_else {
124+
if eq_expr_value(cx, let_expr, peel_blocks(else_expr)) {
125+
return;
126+
}
127+
}
123128
let mut applicability = Applicability::MachineApplicable;
124129
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
125130
let by_ref = matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut);

tests/ui/needless_match.fixed

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ fn result_match() {
6363
fn if_let_option() {
6464
let _ = Some(1);
6565

66-
fn do_something() {}
66+
fn if_let_result(x: Result<(), i32>) {
67+
let _: Result<(), i32> = x;
68+
let _: Result<(), i32> = x;
69+
// Input type mismatch, don't trigger
70+
#[allow(clippy::question_mark)]
71+
let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
72+
}
6773

6874
// Don't trigger
6975
let _ = if let Some(a) = Some(1) {

tests/ui/needless_match.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,29 @@ fn result_match() {
7575
Err(err) => Err(err),
7676
Ok(a) => Ok(a),
7777
};
78-
// as ref, don't trigger
79-
let res = &func_ret_err(0_i32);
80-
let _: Result<&i32, &i32> = match *res {
81-
Ok(ref x) => Ok(x),
82-
Err(ref x) => Err(x),
78+
}
79+
80+
fn if_let_option() -> Option<i32> {
81+
if let Some(a) = Some(1) { Some(a) } else { None }
82+
}
83+
84+
fn if_let_result(x: Result<(), i32>) {
85+
let _: Result<(), i32> = if let Err(e) = x { Err(e) } else { x };
86+
let _: Result<(), i32> = if let Ok(val) = x { Ok(val) } else { x };
87+
// Input type mismatch, don't trigger
88+
#[allow(clippy::question_mark)]
89+
let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
90+
}
91+
92+
fn if_let_custom_enum(x: Choice) {
93+
let _: Choice = if let Choice::A = x {
94+
Choice::A
95+
} else if let Choice::B = x {
96+
Choice::B
97+
} else if let Choice::C = x {
98+
Choice::C
99+
} else {
100+
x
83101
};
84102
}
85103

tests/ui/needless_match.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ LL | let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x };
8484
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
8585

8686
error: this if-let expression is unnecessary
87-
--> $DIR/needless_match.rs:129:21
87+
--> $DIR/needless_match.rs:104:21
8888
|
8989
LL | let _: Simple = if let Simple::A = x {
9090
| _____________________^

0 commit comments

Comments
 (0)