Skip to content

Commit 0a81f82

Browse files
committed
add semicolon_outside_block_if_singleline lint
1 parent 90cb0fa commit 0a81f82

6 files changed

+317
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4884,6 +4884,7 @@ Released 2018-09-13
48844884
[`semicolon_if_nothing_returned`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
48854885
[`semicolon_inside_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_inside_block
48864886
[`semicolon_outside_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_outside_block
4887+
[`semicolon_outside_block_if_singleline`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_outside_block_if_singleline
48874888
[`separated_literal_suffix`]: https://rust-lang.github.io/rust-clippy/master/index.html#separated_literal_suffix
48884889
[`serde_api_misuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#serde_api_misuse
48894890
[`shadow_reuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_reuse

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
546546
crate::self_named_constructors::SELF_NAMED_CONSTRUCTORS_INFO,
547547
crate::semicolon_block::SEMICOLON_INSIDE_BLOCK_INFO,
548548
crate::semicolon_block::SEMICOLON_OUTSIDE_BLOCK_INFO,
549+
crate::semicolon_block::SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE_INFO,
549550
crate::semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED_INFO,
550551
crate::serde_api::SERDE_API_MISUSE_INFO,
551552
crate::shadow::SHADOW_REUSE_INFO,

clippy_lints/src/semicolon_block.rs

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,48 @@ declare_clippy_lint! {
6464
restriction,
6565
"add a semicolon outside the block"
6666
}
67-
declare_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK]);
67+
declare_clippy_lint! {
68+
/// ### What it does
69+
///
70+
/// Suggests moving the semicolon from a block's final expression outside of
71+
/// the block if it's singleline, and inside the block if it's multiline.
72+
///
73+
/// ### Why is this bad?
74+
///
75+
/// Some may prefer if the semicolon is outside if a block is only one
76+
/// expression, as this allows rustfmt to make it singleline. In the case that
77+
/// it isn't, it should be inside.
78+
/// Take a look at both `semicolon_inside_block` and `semicolon_outside_block` for alternatives.
79+
///
80+
/// ### Example
81+
///
82+
/// ```rust
83+
/// # fn f(_: u32) {}
84+
/// # let x = 0;
85+
/// unsafe { f(x); }
86+
///
87+
/// unsafe {
88+
/// let x = 1;
89+
/// f(x)
90+
/// };
91+
/// ```
92+
/// Use instead:
93+
/// ```rust
94+
/// # fn f(_: u32) {}
95+
/// # let x = 0;
96+
/// unsafe { f(x) };
97+
///
98+
/// unsafe {
99+
/// let x = 1;
100+
/// f(x);
101+
/// }
102+
/// ```
103+
#[clippy::version = "1.68.0"]
104+
pub SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE,
105+
restriction,
106+
"add a semicolon inside the block if it's singleline, otherwise outside"
107+
}
108+
declare_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE]);
68109

69110
impl LateLintPass<'_> for SemicolonBlock {
70111
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
@@ -98,6 +139,8 @@ fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'
98139
let insert_span = tail.span.source_callsite().shrink_to_hi();
99140
let remove_span = semi_span.with_lo(block.span.hi());
100141

142+
check_semicolon_outside_block_if_singleline(cx, block, remove_span, insert_span, true, "inside");
143+
101144
span_lint_and_then(
102145
cx,
103146
SEMICOLON_INSIDE_BLOCK,
@@ -120,6 +163,8 @@ fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_ex
120163
let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span);
121164
let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi());
122165

166+
check_semicolon_outside_block_if_singleline(cx, block, remove_span, insert_span, false, "outside");
167+
123168
span_lint_and_then(
124169
cx,
125170
SEMICOLON_OUTSIDE_BLOCK,
@@ -135,3 +180,48 @@ fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_ex
135180
},
136181
);
137182
}
183+
184+
fn check_semicolon_outside_block_if_singleline(
185+
cx: &LateContext<'_>,
186+
block: &Block<'_>,
187+
remove_span: Span,
188+
insert_span: Span,
189+
inequality: bool,
190+
ty: &str,
191+
) {
192+
let remove_line = cx
193+
.sess()
194+
.source_map()
195+
.lookup_line(remove_span.lo())
196+
.expect("failed to get `remove_span`'s line")
197+
.line;
198+
let insert_line = cx
199+
.sess()
200+
.source_map()
201+
.lookup_line(insert_span.lo())
202+
.expect("failed to get `insert_span`'s line")
203+
.line;
204+
205+
let eq = if inequality {
206+
remove_line != insert_line
207+
} else {
208+
remove_line == insert_line
209+
};
210+
211+
if eq {
212+
span_lint_and_then(
213+
cx,
214+
SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE,
215+
block.span,
216+
&format!("consider moving the `;` {ty} the block for consistent formatting"),
217+
|diag| {
218+
multispan_sugg_with_applicability(
219+
diag,
220+
"put the `;` here",
221+
Applicability::MachineApplicable,
222+
[(remove_span, String::new()), (insert_span, ";".to_owned())],
223+
);
224+
},
225+
);
226+
}
227+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// run-rustfix
2+
#![allow(
3+
unused,
4+
clippy::unused_unit,
5+
clippy::unnecessary_operation,
6+
clippy::no_effect,
7+
clippy::single_element_loop
8+
)]
9+
#![warn(clippy::semicolon_outside_block_if_singleline)]
10+
11+
macro_rules! m {
12+
(()) => {
13+
()
14+
};
15+
(0) => {{
16+
0
17+
};};
18+
(1) => {{
19+
1;
20+
}};
21+
(2) => {{
22+
2;
23+
}};
24+
}
25+
26+
fn unit_fn_block() {
27+
()
28+
}
29+
30+
#[rustfmt::skip]
31+
fn main() {
32+
{ unit_fn_block() }
33+
unsafe { unit_fn_block() }
34+
35+
{
36+
unit_fn_block()
37+
}
38+
39+
{ unit_fn_block() };
40+
unsafe { unit_fn_block() };
41+
42+
{ unit_fn_block() };
43+
unsafe { unit_fn_block() };
44+
45+
{ unit_fn_block(); };
46+
unsafe { unit_fn_block(); };
47+
48+
{
49+
unit_fn_block();
50+
unit_fn_block();
51+
}
52+
{
53+
unit_fn_block();
54+
unit_fn_block();
55+
}
56+
{
57+
unit_fn_block();
58+
unit_fn_block();
59+
};
60+
61+
{ m!(()) };
62+
{ m!(()) };
63+
{ m!(()); };
64+
m!(0);
65+
m!(1);
66+
m!(2);
67+
68+
for _ in [()] {
69+
unit_fn_block();
70+
}
71+
for _ in [()] {
72+
unit_fn_block()
73+
}
74+
75+
let _d = || {
76+
unit_fn_block();
77+
};
78+
let _d = || {
79+
unit_fn_block()
80+
};
81+
82+
{ unit_fn_block(); };
83+
84+
unit_fn_block()
85+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// run-rustfix
2+
#![allow(
3+
unused,
4+
clippy::unused_unit,
5+
clippy::unnecessary_operation,
6+
clippy::no_effect,
7+
clippy::single_element_loop
8+
)]
9+
#![warn(clippy::semicolon_outside_block_if_singleline)]
10+
11+
macro_rules! m {
12+
(()) => {
13+
()
14+
};
15+
(0) => {{
16+
0
17+
};};
18+
(1) => {{
19+
1;
20+
}};
21+
(2) => {{
22+
2;
23+
}};
24+
}
25+
26+
fn unit_fn_block() {
27+
()
28+
}
29+
30+
#[rustfmt::skip]
31+
fn main() {
32+
{ unit_fn_block() }
33+
unsafe { unit_fn_block() }
34+
35+
{
36+
unit_fn_block()
37+
}
38+
39+
{ unit_fn_block() };
40+
unsafe { unit_fn_block() };
41+
42+
{ unit_fn_block(); }
43+
unsafe { unit_fn_block(); }
44+
45+
{ unit_fn_block(); };
46+
unsafe { unit_fn_block(); };
47+
48+
{
49+
unit_fn_block();
50+
unit_fn_block()
51+
};
52+
{
53+
unit_fn_block();
54+
unit_fn_block();
55+
}
56+
{
57+
unit_fn_block();
58+
unit_fn_block();
59+
};
60+
61+
{ m!(()) };
62+
{ m!(()); }
63+
{ m!(()); };
64+
m!(0);
65+
m!(1);
66+
m!(2);
67+
68+
for _ in [()] {
69+
unit_fn_block();
70+
}
71+
for _ in [()] {
72+
unit_fn_block()
73+
}
74+
75+
let _d = || {
76+
unit_fn_block();
77+
};
78+
let _d = || {
79+
unit_fn_block()
80+
};
81+
82+
{ unit_fn_block(); };
83+
84+
unit_fn_block()
85+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error: consider moving the `;` outside the block for consistent formatting
2+
--> $DIR/semicolon_outside_block_if_singleline.rs:42:5
3+
|
4+
LL | { unit_fn_block(); }
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::semicolon-outside-block-if-singleline` implied by `-D warnings`
8+
help: put the `;` here
9+
|
10+
LL - { unit_fn_block(); }
11+
LL + { unit_fn_block() };
12+
|
13+
14+
error: consider moving the `;` outside the block for consistent formatting
15+
--> $DIR/semicolon_outside_block_if_singleline.rs:43:5
16+
|
17+
LL | unsafe { unit_fn_block(); }
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
help: put the `;` here
21+
|
22+
LL - unsafe { unit_fn_block(); }
23+
LL + unsafe { unit_fn_block() };
24+
|
25+
26+
error: consider moving the `;` inside the block for consistent formatting
27+
--> $DIR/semicolon_outside_block_if_singleline.rs:48:5
28+
|
29+
LL | / {
30+
LL | | unit_fn_block();
31+
LL | | unit_fn_block()
32+
LL | | };
33+
| |_____^
34+
|
35+
help: put the `;` here
36+
|
37+
LL ~ unit_fn_block();
38+
LL ~ }
39+
|
40+
41+
error: consider moving the `;` outside the block for consistent formatting
42+
--> $DIR/semicolon_outside_block_if_singleline.rs:62:5
43+
|
44+
LL | { m!(()); }
45+
| ^^^^^^^^^^^
46+
|
47+
help: put the `;` here
48+
|
49+
LL - { m!(()); }
50+
LL + { m!(()) };
51+
|
52+
53+
error: aborting due to 4 previous errors
54+

0 commit comments

Comments
 (0)