Skip to content

Commit

Permalink
fix collision between needless_match
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed Jun 13, 2022
1 parent 9ae6281 commit c35130a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
5 changes: 5 additions & 0 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ fn check_if_let_some_or_err_and_early_return(cx: &LateContext<'_>, expr: &Expr<'
if (is_early_return(sym::Option, cx, &if_block) && path_to_local_id(peel_blocks(if_then), bind_id))
|| is_early_return(sym::Result, cx, &if_block);
then {
if let Some(else_expr) = if_else {
if eq_expr_value(cx, let_expr, peel_blocks(else_expr)) {
return;
}
}
let mut applicability = Applicability::MachineApplicable;
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
let by_ref = matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut);
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/needless_match.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ fn result_match() {
fn if_let_option() {
let _ = Some(1);

fn do_something() {}
fn if_let_result(x: Result<(), i32>) {
let _: Result<(), i32> = x;
let _: Result<(), i32> = x;
// Input type mismatch, don't trigger
#[allow(clippy::question_mark)]
let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
}

// Don't trigger
let _ = if let Some(a) = Some(1) {
Expand Down
28 changes: 23 additions & 5 deletions tests/ui/needless_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,29 @@ fn result_match() {
Err(err) => Err(err),
Ok(a) => Ok(a),
};
// as ref, don't trigger
let res = &func_ret_err(0_i32);
let _: Result<&i32, &i32> = match *res {
Ok(ref x) => Ok(x),
Err(ref x) => Err(x),
}

fn if_let_option() -> Option<i32> {
if let Some(a) = Some(1) { Some(a) } else { None }
}

fn if_let_result(x: Result<(), i32>) {
let _: Result<(), i32> = if let Err(e) = x { Err(e) } else { x };
let _: Result<(), i32> = if let Ok(val) = x { Ok(val) } else { x };
// Input type mismatch, don't trigger
#[allow(clippy::question_mark)]
let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
}

fn if_let_custom_enum(x: Choice) {
let _: Choice = if let Choice::A = x {
Choice::A
} else if let Choice::B = x {
Choice::B
} else if let Choice::C = x {
Choice::C
} else {
x
};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/needless_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ LL | let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`

error: this if-let expression is unnecessary
--> $DIR/needless_match.rs:129:21
--> $DIR/needless_match.rs:104:21
|
LL | let _: Simple = if let Simple::A = x {
| _____________________^
Expand Down

0 comments on commit c35130a

Please sign in to comment.