Skip to content

Commit bd52c01

Browse files
committed
needless_return: look inside else if parts as well
`if` expressions don't necessarily contain a block in the `else` part in the presence of an `else if`. The `else` part, if present, must be handled as a regular expression, not necessarily as a block expression.
1 parent 1a864f3 commit bd52c01

File tree

5 files changed

+166
-3
lines changed

5 files changed

+166
-3
lines changed

clippy_lints/src/panic_unimplemented.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
152152
expr.span,
153153
"`panic_any` should not be present in production code",
154154
);
155-
return;
156155
}
157156
}
158157
}

clippy_lints/src/returns.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,9 @@ fn check_final_expr<'tcx>(
428428
ExprKind::If(_, then, else_clause_opt) => {
429429
check_block_return(cx, &then.kind, peeled_drop_expr.span, semi_spans.clone());
430430
if let Some(else_clause) = else_clause_opt {
431-
check_block_return(cx, &else_clause.kind, peeled_drop_expr.span, semi_spans);
431+
// The `RetReplacement` won't be used there as `else_clause` will be either a block or
432+
// a `if` expression.
433+
check_final_expr(cx, else_clause, semi_spans, RetReplacement::Empty, match_ty_opt);
432434
}
433435
},
434436
// a match expr, check all arms

tests/ui/needless_return.fixed

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,42 @@ pub unsafe fn issue_12157() -> *const i32 {
452452
(unsafe { todo() } as *const i32)
453453
//~^ needless_return
454454
}
455+
456+
mod else_ifs {
457+
fn test1(a: i32) -> u32 {
458+
if a == 0 {
459+
1
460+
//~^ needless_return
461+
} else if a < 10 {
462+
2
463+
//~^ needless_return
464+
} else {
465+
3
466+
//~^ needless_return
467+
}
468+
}
469+
470+
fn test2(a: i32) -> u32 {
471+
if a == 0 {
472+
1
473+
//~^ needless_return
474+
} else if a < 10 {
475+
2
476+
} else {
477+
3
478+
//~^ needless_return
479+
}
480+
}
481+
482+
fn test3(a: i32) -> u32 {
483+
if a == 0 {
484+
1
485+
//~^ needless_return
486+
} else if a < 10 {
487+
2
488+
} else {
489+
3
490+
//~^ needless_return
491+
}
492+
}
493+
}

tests/ui/needless_return.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,42 @@ pub unsafe fn issue_12157() -> *const i32 {
461461
return unsafe { todo() } as *const i32;
462462
//~^ needless_return
463463
}
464+
465+
mod else_ifs {
466+
fn test1(a: i32) -> u32 {
467+
if a == 0 {
468+
return 1;
469+
//~^ needless_return
470+
} else if a < 10 {
471+
return 2;
472+
//~^ needless_return
473+
} else {
474+
return 3;
475+
//~^ needless_return
476+
}
477+
}
478+
479+
fn test2(a: i32) -> u32 {
480+
if a == 0 {
481+
return 1;
482+
//~^ needless_return
483+
} else if a < 10 {
484+
2
485+
} else {
486+
return 3;
487+
//~^ needless_return
488+
}
489+
}
490+
491+
fn test3(a: i32) -> u32 {
492+
if a == 0 {
493+
return 1;
494+
//~^ needless_return
495+
} else if a < 10 {
496+
2
497+
} else {
498+
return 3;
499+
//~^ needless_return
500+
}
501+
}
502+
}

tests/ui/needless_return.stderr

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,5 +703,89 @@ LL - return unsafe { todo() } as *const i32;
703703
LL + (unsafe { todo() } as *const i32)
704704
|
705705

706-
error: aborting due to 55 previous errors
706+
error: unneeded `return` statement
707+
--> tests/ui/needless_return.rs:468:13
708+
|
709+
LL | return 1;
710+
| ^^^^^^^^
711+
|
712+
help: remove `return`
713+
|
714+
LL - return 1;
715+
LL + 1
716+
|
717+
718+
error: unneeded `return` statement
719+
--> tests/ui/needless_return.rs:471:13
720+
|
721+
LL | return 2;
722+
| ^^^^^^^^
723+
|
724+
help: remove `return`
725+
|
726+
LL - return 2;
727+
LL + 2
728+
|
729+
730+
error: unneeded `return` statement
731+
--> tests/ui/needless_return.rs:474:13
732+
|
733+
LL | return 3;
734+
| ^^^^^^^^
735+
|
736+
help: remove `return`
737+
|
738+
LL - return 3;
739+
LL + 3
740+
|
741+
742+
error: unneeded `return` statement
743+
--> tests/ui/needless_return.rs:481:13
744+
|
745+
LL | return 1;
746+
| ^^^^^^^^
747+
|
748+
help: remove `return`
749+
|
750+
LL - return 1;
751+
LL + 1
752+
|
753+
754+
error: unneeded `return` statement
755+
--> tests/ui/needless_return.rs:486:13
756+
|
757+
LL | return 3;
758+
| ^^^^^^^^
759+
|
760+
help: remove `return`
761+
|
762+
LL - return 3;
763+
LL + 3
764+
|
765+
766+
error: unneeded `return` statement
767+
--> tests/ui/needless_return.rs:493:13
768+
|
769+
LL | return 1;
770+
| ^^^^^^^^
771+
|
772+
help: remove `return`
773+
|
774+
LL - return 1;
775+
LL + 1
776+
|
777+
778+
error: unneeded `return` statement
779+
--> tests/ui/needless_return.rs:498:13
780+
|
781+
LL | return 3;
782+
| ^^^^^^^^
783+
|
784+
help: remove `return`
785+
|
786+
LL - return 3;
787+
LL + 3
788+
|
789+
790+
error: aborting due to 62 previous errors
707791

0 commit comments

Comments
 (0)