Skip to content

Commit 2e17035

Browse files
committed
Auto merge of #7955 - dswij:let-else-early-return, r=giraffate
Fix `semicolon_if_nothing_returned` FP on `let-else` stmts closes #7912 `semicolon_if_nothing_returned` now additionally checks if the statements ends in `;` , this will also prevent `let-else` statements to be linted. changelog: fix [`semicolon_if_nothing_returned`] FP firing on `let-else`
2 parents 93f13d5 + a003ca6 commit 2e17035

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

clippy_lints/src/semicolon_if_nothing_returned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl LateLintPass<'_> for SemicolonIfNothingReturned {
4444
let t_expr = cx.typeck_results().expr_ty(expr);
4545
if t_expr.is_unit();
4646
if let snippet = snippet_with_macro_callsite(cx, expr.span, "}");
47-
if !snippet.ends_with('}');
47+
if !snippet.ends_with('}') && !snippet.ends_with(';');
4848
if cx.sess().source_map().is_multiline(block.span);
4949
then {
5050
// filter out the desugared `for` loop

tests/ui/semicolon_if_nothing_returned.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![warn(clippy::semicolon_if_nothing_returned)]
22
#![allow(clippy::redundant_closure)]
33
#![feature(label_break_value)]
4+
#![feature(let_else)]
45

56
fn get_unit() {}
67

@@ -110,3 +111,12 @@ fn macro_with_semicolon() {
110111
}
111112
repro!();
112113
}
114+
115+
fn function_returning_option() -> Option<i32> {
116+
Some(1)
117+
}
118+
119+
// No warning
120+
fn let_else_stmts() {
121+
let Some(x) = function_returning_option() else { return; };
122+
}

tests/ui/semicolon_if_nothing_returned.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: consider adding a `;` to the last statement for consistent formatting
2-
--> $DIR/semicolon_if_nothing_returned.rs:9:5
2+
--> $DIR/semicolon_if_nothing_returned.rs:10:5
33
|
44
LL | println!("Hello")
55
| ^^^^^^^^^^^^^^^^^ help: add a `;` here: `println!("Hello");`
66
|
77
= note: `-D clippy::semicolon-if-nothing-returned` implied by `-D warnings`
88

99
error: consider adding a `;` to the last statement for consistent formatting
10-
--> $DIR/semicolon_if_nothing_returned.rs:13:5
10+
--> $DIR/semicolon_if_nothing_returned.rs:14:5
1111
|
1212
LL | get_unit()
1313
| ^^^^^^^^^^ help: add a `;` here: `get_unit();`
1414

1515
error: consider adding a `;` to the last statement for consistent formatting
16-
--> $DIR/semicolon_if_nothing_returned.rs:18:5
16+
--> $DIR/semicolon_if_nothing_returned.rs:19:5
1717
|
1818
LL | y = x + 1
1919
| ^^^^^^^^^ help: add a `;` here: `y = x + 1;`
2020

2121
error: consider adding a `;` to the last statement for consistent formatting
22-
--> $DIR/semicolon_if_nothing_returned.rs:24:9
22+
--> $DIR/semicolon_if_nothing_returned.rs:25:9
2323
|
2424
LL | hello()
2525
| ^^^^^^^ help: add a `;` here: `hello();`
2626

2727
error: consider adding a `;` to the last statement for consistent formatting
28-
--> $DIR/semicolon_if_nothing_returned.rs:35:9
28+
--> $DIR/semicolon_if_nothing_returned.rs:36:9
2929
|
3030
LL | ptr::drop_in_place(s.as_mut_ptr())
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `ptr::drop_in_place(s.as_mut_ptr());`

0 commit comments

Comments
 (0)