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

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
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
39 changes: 39 additions & 0 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,42 @@ 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
}
}
}
39 changes: 39 additions & 0 deletions tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,42 @@ 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
}
}
}
86 changes: 85 additions & 1 deletion tests/ui/needless_return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -703,5 +703,89 @@ 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: aborting due to 62 previous errors