Skip to content

Commit 1462f84

Browse files
committed
fix confliction with needless_match
1 parent 9ae6281 commit 1462f84

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ enum IfBlockType<'hir> {
8282
/// ```
8383
///
8484
/// If it matches, it will suggest to use the question mark operator instead
85-
fn check_is_none_or_err_and_early_return(cx: &LateContext<'_>, expr: &Expr<'_>) {
85+
fn check_is_none_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
8686
if_chain! {
8787
if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr);
8888
if !is_else_clause(cx.tcx, expr);
@@ -109,7 +109,7 @@ fn check_is_none_or_err_and_early_return(cx: &LateContext<'_>, expr: &Expr<'_>)
109109
}
110110
}
111111

112-
fn check_if_let_some_or_err_and_early_return(cx: &LateContext<'_>, expr: &Expr<'_>) {
112+
fn check_if_let_some_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
113113
if_chain! {
114114
if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else }) = higher::IfLet::hir(cx, expr);
115115
if !is_else_clause(cx.tcx, expr);
@@ -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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ fn if_let_result() {
9999
let _: Result<i32, i32> = x;
100100
let _: Result<i32, i32> = x;
101101
// Input type mismatch, don't trigger
102+
#[allow(clippy::question_mark)]
102103
let _: Result<i32, i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
103104
}
104105

tests/ui/needless_match.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ fn if_let_result() {
122122
let _: Result<i32, i32> = if let Err(e) = x { Err(e) } else { x };
123123
let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x };
124124
// Input type mismatch, don't trigger
125+
#[allow(clippy::question_mark)]
125126
let _: Result<i32, i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
126127
}
127128

tests/ui/needless_match.stderr

Lines changed: 2 additions & 2 deletions
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:130:21
8888
|
8989
LL | let _: Simple = if let Simple::A = x {
9090
| _____________________^
@@ -97,7 +97,7 @@ LL | | };
9797
| |_____^ help: replace it with: `x`
9898

9999
error: this match expression is unnecessary
100-
--> $DIR/needless_match.rs:168:26
100+
--> $DIR/needless_match.rs:169:26
101101
|
102102
LL | let _: Complex = match ce {
103103
| __________________________^

0 commit comments

Comments
 (0)