Skip to content

needless_match: do not pretend that return is not significant in an expression #14757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions clippy_lints/src/matches/needless_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn check_all_arms(cx: &LateContext<'_>, match_expr: &Expr<'_>, arms: &[Arm<'_>])
}

if let PatKind::Wild = arm.pat.kind {
if !eq_expr_value(cx, match_expr, strip_return(arm_expr)) {
if !eq_expr_value(cx, match_expr, arm_expr) {
return false;
}
} else if !pat_same_as_expr(arm.pat, arm_expr) {
Expand Down Expand Up @@ -103,27 +103,18 @@ fn check_if_let_inner(cx: &LateContext<'_>, if_let: &higher::IfLet<'_>) -> bool
if matches!(else_expr.kind, ExprKind::Block(..)) {
return false;
}
let ret = strip_return(else_expr);
let let_expr_ty = cx.typeck_results().expr_ty(if_let.let_expr);
if is_type_diagnostic_item(cx, let_expr_ty, sym::Option) {
return is_res_lang_ctor(cx, path_res(cx, ret), OptionNone) || eq_expr_value(cx, if_let.let_expr, ret);
return is_res_lang_ctor(cx, path_res(cx, else_expr), OptionNone)
|| eq_expr_value(cx, if_let.let_expr, else_expr);
}
return eq_expr_value(cx, if_let.let_expr, ret);
return eq_expr_value(cx, if_let.let_expr, else_expr);
}
}

false
}

/// Strip `return` keyword if the expression type is `ExprKind::Ret`.
fn strip_return<'hir>(expr: &'hir Expr<'hir>) -> &'hir Expr<'hir> {
if let ExprKind::Ret(Some(ret)) = expr.kind {
ret
} else {
expr
}
}

/// Manually check for coercion casting by checking if the type of the match operand or let expr
/// differs with the assigned local variable or the function return type.
fn expr_ty_matches_p_ty(cx: &LateContext<'_>, expr: &Expr<'_>, p_expr: &Expr<'_>) -> bool {
Expand Down Expand Up @@ -161,7 +152,6 @@ fn expr_ty_matches_p_ty(cx: &LateContext<'_>, expr: &Expr<'_>, p_expr: &Expr<'_>
}

fn pat_same_as_expr(pat: &Pat<'_>, expr: &Expr<'_>) -> bool {
let expr = strip_return(expr);
match (&pat.kind, &expr.kind) {
// Example: `Some(val) => Some(val)`
(PatKind::TupleStruct(QPath::Resolved(_, path), tuple_params, _), ExprKind::Call(call_expr, call_params)) => {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/needless_match.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,16 @@ pub fn issue13574() -> Option<()> {
None
}

fn issue14754(t: Result<i32, &'static str>) -> Result<i32, &'static str> {
let _ = match t {
Ok(v) => Ok::<_, &'static str>(v),
err @ Err(_) => return err,
};
println!("Still here");
let x = t;
//~^^^^ needless_match
println!("Still here");
x
}

fn main() {}
15 changes: 15 additions & 0 deletions tests/ui/needless_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,19 @@ pub fn issue13574() -> Option<()> {
None
}

fn issue14754(t: Result<i32, &'static str>) -> Result<i32, &'static str> {
let _ = match t {
Ok(v) => Ok::<_, &'static str>(v),
err @ Err(_) => return err,
};
println!("Still here");
let x = match t {
Ok(v) => Ok::<_, &'static str>(v),
err @ Err(_) => err,
};
//~^^^^ needless_match
println!("Still here");
x
}

fn main() {}
12 changes: 11 additions & 1 deletion tests/ui/needless_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,15 @@ LL | | None
LL | | }
| |_________^ help: replace it with: `A`

error: aborting due to 14 previous errors
error: this match expression is unnecessary
--> tests/ui/needless_match.rs:373:13
|
LL | let x = match t {
| _____________^
LL | | Ok(v) => Ok::<_, &'static str>(v),
LL | | err @ Err(_) => err,
LL | | };
| |_____^ help: replace it with: `t`

error: aborting due to 15 previous errors