Skip to content

Commit dd02468

Browse files
committed
Auto merge of #7326 - 1c3t3a:1c3t3a-issue-7324, r=flip1995
Fix false positive on `semicolon_if_nothing_returned` Currently the [`semicolon_if_nothing_returned`](https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned) lint fires in unwanted situations where a block only spans one line. An example of this was given in #7324. This code: ```rust use std::mem::MaybeUninit; use std::ptr; fn main() { let mut s = MaybeUninit::<String>::uninit(); let _d = || unsafe { ptr::drop_in_place(s.as_mut_ptr()) }; } ``` yields the following clippy error: ``` error: consider adding a `;` to the last statement for consistent formatting --> src/main.rs:6:26 | 6 | let _d = || unsafe { ptr::drop_in_place(s.as_mut_ptr()) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `ptr::drop_in_place(s.as_mut_ptr());` | = note: `-D clippy::semicolon-if-nothing-returned` implied by `-D clippy::pedantic` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned ``` I updated the lint to check if the statement is inside an `unsafe` block, a closure or a normal block and if the block only spans one line, in that case the lint is not emitted. This closes #7324. changelog: enhanced semicolon if nothing returned according to #7324.
2 parents da0538e + 5ec80f3 commit dd02468

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

clippy_lints/src/semicolon_if_nothing_returned.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::rustc_lint::LintContext;
12
use clippy_utils::diagnostics::span_lint_and_sugg;
23
use clippy_utils::source::snippet_with_macro_callsite;
34
use clippy_utils::{in_macro, sugg};
@@ -45,6 +46,7 @@ impl LateLintPass<'_> for SemicolonIfNothingReturned {
4546
if t_expr.is_unit();
4647
if let snippet = snippet_with_macro_callsite(cx, expr.span, "}");
4748
if !snippet.ends_with('}');
49+
if cx.sess().source_map().is_multiline(block.span);
4850
then {
4951
// filter out the desugared `for` loop
5052
if let ExprKind::DropTemps(..) = &expr.kind {

tests/ui/semicolon_if_nothing_returned.rs

+44
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ fn basic101(x: i32) {
1717
y = x + 1
1818
}
1919

20+
#[rustfmt::skip]
21+
fn closure_error() {
22+
let _d = || {
23+
hello()
24+
};
25+
}
26+
27+
#[rustfmt::skip]
28+
fn unsafe_checks_error() {
29+
use std::mem::MaybeUninit;
30+
use std::ptr;
31+
32+
let mut s = MaybeUninit::<String>::uninit();
33+
let _d = || unsafe {
34+
ptr::drop_in_place(s.as_mut_ptr())
35+
};
36+
}
37+
2038
// this is fine
2139
fn print_sum(a: i32, b: i32) {
2240
println!("{}", a + b);
@@ -53,3 +71,29 @@ fn loop_test(x: i32) {
5371
println!("{}", ext);
5472
}
5573
}
74+
75+
fn closure() {
76+
let _d = || hello();
77+
}
78+
79+
#[rustfmt::skip]
80+
fn closure_block() {
81+
let _d = || { hello() };
82+
}
83+
84+
unsafe fn some_unsafe_op() {}
85+
unsafe fn some_other_unsafe_fn() {}
86+
87+
fn do_something() {
88+
unsafe { some_unsafe_op() };
89+
90+
unsafe { some_other_unsafe_fn() };
91+
}
92+
93+
fn unsafe_checks() {
94+
use std::mem::MaybeUninit;
95+
use std::ptr;
96+
97+
let mut s = MaybeUninit::<String>::uninit();
98+
let _d = || unsafe { ptr::drop_in_place(s.as_mut_ptr()) };
99+
}

tests/ui/semicolon_if_nothing_returned.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,17 @@ error: consider adding a `;` to the last statement for consistent formatting
1818
LL | y = x + 1
1919
| ^^^^^^^^^ help: add a `;` here: `y = x + 1;`
2020

21-
error: aborting due to 3 previous errors
21+
error: consider adding a `;` to the last statement for consistent formatting
22+
--> $DIR/semicolon_if_nothing_returned.rs:23:9
23+
|
24+
LL | hello()
25+
| ^^^^^^^ help: add a `;` here: `hello();`
26+
27+
error: consider adding a `;` to the last statement for consistent formatting
28+
--> $DIR/semicolon_if_nothing_returned.rs:34:9
29+
|
30+
LL | ptr::drop_in_place(s.as_mut_ptr())
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `ptr::drop_in_place(s.as_mut_ptr());`
32+
33+
error: aborting due to 5 previous errors
2234

0 commit comments

Comments
 (0)