Skip to content

Commit 9f5e003

Browse files
committed
Added .fixed test file
1 parent b11827b commit 9f5e003

File tree

4 files changed

+119
-36
lines changed

4 files changed

+119
-36
lines changed

clippy_lints/src/semicolon_outside_block.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ declare_clippy_lint! {
3636

3737
declare_lint_pass!(SemicolonOutsideBlock => [SEMICOLON_OUTSIDE_BLOCK]);
3838

39+
/// Checks if an ExprKind is of an illegal variant (aka blocks that we don't want)
40+
/// to lint on as it's illegal or unnecessary to put a semicolon afterwards.
41+
/// This macro then inserts a `return` statement to return out of the check_block
42+
/// method.
43+
macro_rules! check_expr_return {
44+
($l:expr) => {
45+
if let ExprKind::If(..) | ExprKind::Loop(..) | ExprKind::DropTemps(..) | ExprKind::Match(..) = $l {
46+
return;
47+
}
48+
};
49+
}
50+
3951
impl LateLintPass<'_> for SemicolonOutsideBlock {
4052
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
4153
if_chain! {
@@ -46,47 +58,37 @@ impl LateLintPass<'_> for SemicolonOutsideBlock {
4658
let t_expr = cx.typeck_results().expr_ty(expr);
4759
if t_expr.is_unit();
4860
then {
49-
// make sure that the block does not belong to a function
61+
// make sure that the block does not belong to a function by iterating over the parents
5062
for (hir_id, _) in cx.tcx.hir().parent_iter(block.hir_id) {
5163
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(hir_id) {
64+
// if we're in a body, check if it's an fn or a closure
5265
if cx.tcx.hir().body_owner_kind(hir_id).is_fn_or_closure() {
5366
let item_body = cx.tcx.hir().body(body_id);
5467
if let ExprKind::Block(fn_block, _) = item_body.value.kind {
68+
// check for an illegal statement in the list of statements...
5569
for stmt in fn_block.stmts {
5670
if let StmtKind::Expr(pot_ille_expr) = stmt.kind {
57-
if let ExprKind::If(..) |
58-
ExprKind::Loop(..) |
59-
ExprKind::DropTemps(..) |
60-
ExprKind::Match(..) = pot_ille_expr.kind {
61-
return
62-
}
71+
check_expr_return!(pot_ille_expr.kind);
6372
}
6473
}
6574

75+
//.. the potential last statement ..
6676
if let Some(last_expr) = fn_block.expr {
67-
if let ExprKind::If(..) |
68-
ExprKind::Loop(..) |
69-
ExprKind::DropTemps(..) |
70-
ExprKind::Match(..) = last_expr.kind {
71-
return;
72-
}
77+
check_expr_return!(last_expr.kind);
7378
}
7479

75-
if fn_block.hir_id == block.hir_id && !matches!(cx.tcx.hir().body_owner_kind(hir_id), BodyOwnerKind::Closure) {
80+
// .. or if this is the body of an fn
81+
if fn_block.hir_id == block.hir_id &&
82+
!matches!(cx.tcx.hir().body_owner_kind(hir_id), BodyOwnerKind::Closure) {
7683
return
7784
}
7885
}
7986
}
8087
}
8188
}
8289

83-
8490
// filter out other blocks and the desugared for loop
85-
if let ExprKind::Block(..) |
86-
ExprKind::DropTemps(..) |
87-
ExprKind::If(..) |
88-
ExprKind::Loop(..) |
89-
ExprKind::Match(..) = expr.kind { return }
91+
check_expr_return!(expr.kind);
9092

9193
// make sure we're also having the semicolon at the end of the expression...
9294
let expr_w_sem = expand_span_to_semicolon(cx, expr.span);
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// run-rustfix
2+
#![warn(clippy::semicolon_outside_block)]
3+
#![allow(dead_code)]
4+
5+
unsafe fn f(_arg: u32) {}
6+
7+
#[rustfmt::skip]
8+
fn main() {
9+
let x = 32;
10+
11+
unsafe { f(x) };
12+
}
13+
14+
fn foo() {
15+
let x = 32;
16+
17+
unsafe {
18+
f(x)
19+
};
20+
}
21+
22+
fn bar() {
23+
let x = 32;
24+
25+
unsafe {
26+
let _this = 1;
27+
let _is = 2;
28+
let _a = 3;
29+
let _long = 4;
30+
let _list = 5;
31+
let _of = 6;
32+
let _variables = 7;
33+
f(x)
34+
};
35+
}
36+
37+
fn get_unit() {}
38+
39+
#[rustfmt::skip]
40+
fn closure_error() {
41+
let _d = || {
42+
get_unit()
43+
};
44+
}
45+
46+
fn my_own_block() {
47+
let x: i32;
48+
{
49+
let y = 42;
50+
x = y + 1;
51+
get_unit()
52+
};
53+
assert_eq!(x, 43)
54+
}
55+
56+
// This is all ok
57+
58+
fn just_get_unit() {
59+
get_unit();
60+
}
61+
62+
fn test_if() {
63+
if 1 > 2 {
64+
get_unit();
65+
} else {
66+
println!("everything alright!");
67+
}
68+
}
69+
70+
fn test_for() {
71+
for _project in &[
72+
"clippy_workspace_tests",
73+
"clippy_workspace_tests/src",
74+
"clippy_workspace_tests/subcrate",
75+
"clippy_workspace_tests/subcrate/src",
76+
"clippy_dev",
77+
"clippy_lints",
78+
"clippy_utils",
79+
"rustc_tools_util",
80+
] {
81+
get_unit();
82+
}
83+
84+
get_unit();
85+
}

tests/ui/semicolon_outside_block.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
// run-rustfix
12
#![warn(clippy::semicolon_outside_block)]
2-
#![allow(clippy::single_match)]
3+
#![allow(dead_code)]
34

4-
unsafe fn f(arg: u32) {}
5+
unsafe fn f(_arg: u32) {}
56

67
#[rustfmt::skip]
78
fn main() {
@@ -35,13 +36,6 @@ fn bar() {
3536

3637
fn get_unit() {}
3738

38-
fn moin() {
39-
{
40-
let _u = get_unit();
41-
println!("Hello");
42-
}
43-
}
44-
4539
#[rustfmt::skip]
4640
fn closure_error() {
4741
let _d = || {
@@ -56,9 +50,11 @@ fn my_own_block() {
5650
x = y + 1;
5751
get_unit();
5852
}
59-
assert_eq!(43, 43)
53+
assert_eq!(x, 43)
6054
}
6155

56+
// This is all ok
57+
6258
fn just_get_unit() {
6359
get_unit();
6460
}
@@ -72,7 +68,7 @@ fn test_if() {
7268
}
7369

7470
fn test_for() {
75-
for project in &[
71+
for _project in &[
7672
"clippy_workspace_tests",
7773
"clippy_workspace_tests/src",
7874
"clippy_workspace_tests/subcrate",

tests/ui/semicolon_outside_block.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: consider moving the `;` outside the block for consistent formatting
2-
--> $DIR/semicolon_outside_block.rs:10:5
2+
--> $DIR/semicolon_outside_block.rs:11:5
33
|
44
LL | unsafe { f(x); }
55
| ^^^^^^^^^^^^^^^^ help: put the `;` outside the block: `unsafe { f(x) };`
66
|
77
= note: `-D clippy::semicolon-outside-block` implied by `-D warnings`
88

99
error: consider moving the `;` outside the block for consistent formatting
10-
--> $DIR/semicolon_outside_block.rs:16:5
10+
--> $DIR/semicolon_outside_block.rs:17:5
1111
|
1212
LL | / unsafe {
1313
LL | | f(x);
@@ -22,7 +22,7 @@ LL | };
2222
|
2323

2424
error: consider moving the `;` outside the block for consistent formatting
25-
--> $DIR/semicolon_outside_block.rs:24:5
25+
--> $DIR/semicolon_outside_block.rs:25:5
2626
|
2727
LL | / unsafe {
2828
LL | | let _this = 1;
@@ -44,7 +44,7 @@ LL | let _list = 5;
4444
...
4545

4646
error: consider moving the `;` outside the block for consistent formatting
47-
--> $DIR/semicolon_outside_block.rs:47:17
47+
--> $DIR/semicolon_outside_block.rs:41:17
4848
|
4949
LL | let _d = || {
5050
| _________________^
@@ -60,7 +60,7 @@ LL | };
6060
|
6161

6262
error: consider moving the `;` outside the block for consistent formatting
63-
--> $DIR/semicolon_outside_block.rs:54:5
63+
--> $DIR/semicolon_outside_block.rs:48:5
6464
|
6565
LL | / {
6666
LL | | let y = 42;

0 commit comments

Comments
 (0)