Skip to content

needless_return: look inside else if parts as well #14798

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

Merged
merged 1 commit into from
May 28, 2025
Merged
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
1 change: 0 additions & 1 deletion clippy_lints/src/panic_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
expr.span,
"`panic_any` should not be present in production code",
);
return;
}
}
}
4 changes: 3 additions & 1 deletion clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,9 @@ fn check_final_expr<'tcx>(
ExprKind::If(_, then, else_clause_opt) => {
check_block_return(cx, &then.kind, peeled_drop_expr.span, semi_spans.clone());
if let Some(else_clause) = else_clause_opt {
check_block_return(cx, &else_clause.kind, peeled_drop_expr.span, semi_spans);
// The `RetReplacement` won't be used there as `else_clause` will be either a block or
// a `if` expression.
check_final_expr(cx, else_clause, semi_spans, RetReplacement::Empty, match_ty_opt);
}
},
// a match expr, check all arms
Expand Down
65 changes: 65 additions & 0 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,68 @@ pub unsafe fn issue_12157() -> *const i32 {
(unsafe { todo() } as *const i32)
//~^ needless_return
}

mod else_ifs {
fn test1(a: i32) -> u32 {
if a == 0 {
1
//~^ needless_return
} else if a < 10 {
2
//~^ needless_return
} else {
3
//~^ needless_return
}
}

fn test2(a: i32) -> u32 {
if a == 0 {
1
//~^ needless_return
} else if a < 10 {
2
} else {
3
//~^ needless_return
}
}

fn test3(a: i32) -> u32 {
if a == 0 {
1
//~^ needless_return
} else if a < 10 {
2
} else {
3
//~^ needless_return
}
}

#[allow(clippy::match_single_binding, clippy::redundant_pattern)]
fn test4(a: i32) -> u32 {
if a == 0 {
1
//~^ needless_return
} else if if if a > 0x1_1 {
return 2;
} else {
return 5;
} {
true
} else {
true
} {
0xDEADC0DE
} else if match a {
b @ _ => {
return 1;
},
} {
0xDEADBEEF
} else {
1
}
}
}
65 changes: 65 additions & 0 deletions tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,68 @@ pub unsafe fn issue_12157() -> *const i32 {
return unsafe { todo() } as *const i32;
//~^ needless_return
}

mod else_ifs {
fn test1(a: i32) -> u32 {
if a == 0 {
return 1;
//~^ needless_return
} else if a < 10 {
return 2;
//~^ needless_return
} else {
return 3;
//~^ needless_return
}
}

fn test2(a: i32) -> u32 {
if a == 0 {
return 1;
//~^ needless_return
} else if a < 10 {
2
} else {
return 3;
//~^ needless_return
}
}

fn test3(a: i32) -> u32 {
if a == 0 {
return 1;
//~^ needless_return
} else if a < 10 {
2
} else {
return 3;
//~^ needless_return
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some weirded expressions, for future proofing (and checking that it works correctly right now)

Suggested change
}
fn test4(a: i32) -> u32 {
if a == 0 {
return 1;
} else if if if a > 0x1_1 {
return 2;
} else {
return 5;
} {true} else { true } { 0xDEADC0DE }
else if match a { b @ _ => { return 1; } }
{ 0xDEADBEEF }
else { 1 }
}
}


#[allow(clippy::match_single_binding, clippy::redundant_pattern)]
fn test4(a: i32) -> u32 {
if a == 0 {
return 1;
//~^ needless_return
} else if if if a > 0x1_1 {
return 2;
} else {
return 5;
} {
true
} else {
true
} {
0xDEADC0DE
} else if match a {
b @ _ => {
return 1;
},
} {
0xDEADBEEF
} else {
1
}
}
}
98 changes: 97 additions & 1 deletion tests/ui/needless_return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -703,5 +703,101 @@ LL - return unsafe { todo() } as *const i32;
LL + (unsafe { todo() } as *const i32)
|

error: aborting due to 55 previous errors
error: unneeded `return` statement
--> tests/ui/needless_return.rs:468:13
|
LL | return 1;
| ^^^^^^^^
|
help: remove `return`
|
LL - return 1;
LL + 1
|

error: unneeded `return` statement
--> tests/ui/needless_return.rs:471:13
|
LL | return 2;
| ^^^^^^^^
|
help: remove `return`
|
LL - return 2;
LL + 2
|

error: unneeded `return` statement
--> tests/ui/needless_return.rs:474:13
|
LL | return 3;
| ^^^^^^^^
|
help: remove `return`
|
LL - return 3;
LL + 3
|

error: unneeded `return` statement
--> tests/ui/needless_return.rs:481:13
|
LL | return 1;
| ^^^^^^^^
|
help: remove `return`
|
LL - return 1;
LL + 1
|

error: unneeded `return` statement
--> tests/ui/needless_return.rs:486:13
|
LL | return 3;
| ^^^^^^^^
|
help: remove `return`
|
LL - return 3;
LL + 3
|

error: unneeded `return` statement
--> tests/ui/needless_return.rs:493:13
|
LL | return 1;
| ^^^^^^^^
|
help: remove `return`
|
LL - return 1;
LL + 1
|

error: unneeded `return` statement
--> tests/ui/needless_return.rs:498:13
|
LL | return 3;
| ^^^^^^^^
|
help: remove `return`
|
LL - return 3;
LL + 3
|

error: unneeded `return` statement
--> tests/ui/needless_return.rs:506:13
|
LL | return 1;
| ^^^^^^^^
|
help: remove `return`
|
LL - return 1;
LL + 1
|

error: aborting due to 63 previous errors