Skip to content

Commit 568ccf3

Browse files
committed
Auto merge of #11083 - sylvestre:autofix, r=dswij
[`semicolon_if_nothing_returned`]: add an autofix changelog: [`semicolon_if_nothing_returned`]: add an autofix
2 parents 9109533 + 8ebdd62 commit 568ccf3

4 files changed

+131
-7
lines changed

clippy_lints/src/semicolon_if_nothing_returned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'tcx> LateLintPass<'tcx> for SemicolonIfNothingReturned {
4343
if let Some(expr) = block.expr;
4444
let t_expr = cx.typeck_results().expr_ty(expr);
4545
if t_expr.is_unit();
46-
let mut app = Applicability::MaybeIncorrect;
46+
let mut app = Applicability::MachineApplicable;
4747
if let snippet = snippet_with_context(cx, expr.span, block.span.ctxt(), "}", &mut app).0;
4848
if !snippet.ends_with('}') && !snippet.ends_with(';');
4949
if cx.sess().source_map().is_multiline(block.span);
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//@run-rustfix
2+
#![warn(clippy::semicolon_if_nothing_returned)]
3+
#![allow(clippy::redundant_closure, clippy::uninlined_format_args, clippy::needless_late_init)]
4+
5+
fn get_unit() {}
6+
7+
// the functions below trigger the lint
8+
fn main() {
9+
println!("Hello");
10+
}
11+
12+
fn hello() {
13+
get_unit();
14+
}
15+
16+
fn basic101(x: i32) {
17+
let y: i32;
18+
y = x + 1;
19+
}
20+
21+
#[rustfmt::skip]
22+
fn closure_error() {
23+
let _d = || {
24+
hello();
25+
};
26+
}
27+
28+
#[rustfmt::skip]
29+
fn unsafe_checks_error() {
30+
use std::mem::MaybeUninit;
31+
use std::ptr;
32+
33+
let mut s = MaybeUninit::<String>::uninit();
34+
let _d = || unsafe {
35+
ptr::drop_in_place(s.as_mut_ptr());
36+
};
37+
}
38+
39+
// this is fine
40+
fn print_sum(a: i32, b: i32) {
41+
println!("{}", a + b);
42+
assert_eq!(true, false);
43+
}
44+
45+
fn foo(x: i32) {
46+
let y: i32;
47+
if x < 1 {
48+
y = 4;
49+
} else {
50+
y = 5;
51+
}
52+
}
53+
54+
fn bar(x: i32) {
55+
let y: i32;
56+
match x {
57+
1 => y = 4,
58+
_ => y = 32,
59+
}
60+
}
61+
62+
fn foobar(x: i32) {
63+
let y: i32;
64+
'label: {
65+
y = x + 1;
66+
}
67+
}
68+
69+
fn loop_test(x: i32) {
70+
let y: i32;
71+
for &ext in &["stdout", "stderr", "fixed"] {
72+
println!("{}", ext);
73+
}
74+
}
75+
76+
fn closure() {
77+
let _d = || hello();
78+
}
79+
80+
#[rustfmt::skip]
81+
fn closure_block() {
82+
let _d = || { hello() };
83+
}
84+
85+
unsafe fn some_unsafe_op() {}
86+
unsafe fn some_other_unsafe_fn() {}
87+
88+
fn do_something() {
89+
unsafe { some_unsafe_op() };
90+
91+
unsafe { some_other_unsafe_fn() };
92+
}
93+
94+
fn unsafe_checks() {
95+
use std::mem::MaybeUninit;
96+
use std::ptr;
97+
98+
let mut s = MaybeUninit::<String>::uninit();
99+
let _d = || unsafe { ptr::drop_in_place(s.as_mut_ptr()) };
100+
}
101+
102+
// Issue #7768
103+
#[rustfmt::skip]
104+
fn macro_with_semicolon() {
105+
macro_rules! repro {
106+
() => {
107+
while false {
108+
}
109+
};
110+
}
111+
repro!();
112+
}
113+
114+
fn function_returning_option() -> Option<i32> {
115+
Some(1)
116+
}
117+
118+
// No warning
119+
fn let_else_stmts() {
120+
let Some(x) = function_returning_option() else {
121+
return;
122+
};
123+
}

tests/ui/semicolon_if_nothing_returned.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
//@run-rustfix
12
#![warn(clippy::semicolon_if_nothing_returned)]
2-
#![allow(clippy::redundant_closure, clippy::uninlined_format_args)]
3+
#![allow(clippy::redundant_closure, clippy::uninlined_format_args, clippy::needless_late_init)]
34

45
fn get_unit() {}
56

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:8:5
2+
--> $DIR/semicolon_if_nothing_returned.rs:9: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:12:5
10+
--> $DIR/semicolon_if_nothing_returned.rs:13: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:17:5
16+
--> $DIR/semicolon_if_nothing_returned.rs:18: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:23:9
22+
--> $DIR/semicolon_if_nothing_returned.rs:24: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:34:9
28+
--> $DIR/semicolon_if_nothing_returned.rs:35: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)