mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 05:33:27 +00:00
Fix needless_match false positive for if-let
when the else block doesn't match to given expr
This commit is contained in:
parent
e5ebece910
commit
b94e24e95f
4 changed files with 29 additions and 5 deletions
|
@ -99,7 +99,7 @@ fn check_if_let(cx: &LateContext<'_>, if_let: &higher::IfLet<'_>) -> bool {
|
||||||
if let ExprKind::Path(ref qpath) = ret.kind {
|
if let ExprKind::Path(ref qpath) = ret.kind {
|
||||||
return is_lang_ctor(cx, qpath, OptionNone) || eq_expr_value(cx, if_let.let_expr, ret);
|
return is_lang_ctor(cx, qpath, OptionNone) || eq_expr_value(cx, if_let.let_expr, ret);
|
||||||
}
|
}
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
return eq_expr_value(cx, if_let.let_expr, ret);
|
return eq_expr_value(cx, if_let.let_expr, ret);
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,6 +80,18 @@ fn if_let_option() {
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Don't trigger
|
||||||
|
let _ = if let Some(a) = Some(1) { Some(a) } else { Some(2) };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn if_let_option_result() -> Result<(), ()> {
|
||||||
|
fn f(x: i32) -> Result<Option<i32>, ()> {
|
||||||
|
Ok(Some(x))
|
||||||
|
}
|
||||||
|
// Don't trigger
|
||||||
|
let _ = if let Some(v) = f(1)? { Some(v) } else { f(2)? };
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn if_let_result() {
|
fn if_let_result() {
|
||||||
|
|
|
@ -103,6 +103,18 @@ fn if_let_option() {
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Don't trigger
|
||||||
|
let _ = if let Some(a) = Some(1) { Some(a) } else { Some(2) };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn if_let_option_result() -> Result<(), ()> {
|
||||||
|
fn f(x: i32) -> Result<Option<i32>, ()> {
|
||||||
|
Ok(Some(x))
|
||||||
|
}
|
||||||
|
// Don't trigger
|
||||||
|
let _ = if let Some(v) = f(1)? { Some(v) } else { f(2)? };
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn if_let_result() {
|
fn if_let_result() {
|
||||||
|
|
|
@ -72,19 +72,19 @@ LL | let _ = if let Some(a) = Some(1) { Some(a) } else { None };
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `Some(1)`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `Some(1)`
|
||||||
|
|
||||||
error: this if-let expression is unnecessary
|
error: this if-let expression is unnecessary
|
||||||
--> $DIR/needless_match.rs:110:31
|
--> $DIR/needless_match.rs:122:31
|
||||||
|
|
|
|
||||||
LL | let _: Result<i32, i32> = if let Err(e) = x { Err(e) } else { x };
|
LL | let _: Result<i32, i32> = if let Err(e) = x { Err(e) } else { x };
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
|
||||||
|
|
||||||
error: this if-let expression is unnecessary
|
error: this if-let expression is unnecessary
|
||||||
--> $DIR/needless_match.rs:111:31
|
--> $DIR/needless_match.rs:123:31
|
||||||
|
|
|
|
||||||
LL | let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x };
|
LL | let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x };
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
|
||||||
|
|
||||||
error: this if-let expression is unnecessary
|
error: this if-let expression is unnecessary
|
||||||
--> $DIR/needless_match.rs:117:21
|
--> $DIR/needless_match.rs:129:21
|
||||||
|
|
|
|
||||||
LL | let _: Simple = if let Simple::A = x {
|
LL | let _: Simple = if let Simple::A = x {
|
||||||
| _____________________^
|
| _____________________^
|
||||||
|
@ -97,7 +97,7 @@ LL | | };
|
||||||
| |_____^ help: replace it with: `x`
|
| |_____^ help: replace it with: `x`
|
||||||
|
|
||||||
error: this match expression is unnecessary
|
error: this match expression is unnecessary
|
||||||
--> $DIR/needless_match.rs:156:26
|
--> $DIR/needless_match.rs:168:26
|
||||||
|
|
|
|
||||||
LL | let _: Complex = match ce {
|
LL | let _: Complex = match ce {
|
||||||
| __________________________^
|
| __________________________^
|
||||||
|
|
Loading…
Reference in a new issue