Skip to content

Commit 3fe4236

Browse files
authored
Rollup merge of #79812 - Aaron1011:lint-item-trailing-semi, r=oli-obk
Lint on redundant trailing semicolon after item We now lint on code like this: ```rust fn main() { fn foo() {}; struct Bar {}; } ``` Previously, this caused warnings in Cargo, so it was disabled.
2 parents f3eead1 + 6bef37c commit 3fe4236

File tree

8 files changed

+32
-30
lines changed

8 files changed

+32
-30
lines changed

compiler/rustc_lint/src/redundant_semicolon.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,26 @@ declare_lint_pass!(RedundantSemicolons => [REDUNDANT_SEMICOLONS]);
2828

2929
impl EarlyLintPass for RedundantSemicolons {
3030
fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
31-
let mut after_item_stmt = false;
3231
let mut seq = None;
3332
for stmt in block.stmts.iter() {
3433
match (&stmt.kind, &mut seq) {
3534
(StmtKind::Empty, None) => seq = Some((stmt.span, false)),
3635
(StmtKind::Empty, Some(seq)) => *seq = (seq.0.to(stmt.span), true),
37-
(_, seq) => {
38-
maybe_lint_redundant_semis(cx, seq, after_item_stmt);
39-
after_item_stmt = matches!(stmt.kind, StmtKind::Item(_));
40-
}
36+
(_, seq) => maybe_lint_redundant_semis(cx, seq),
4137
}
4238
}
43-
maybe_lint_redundant_semis(cx, &mut seq, after_item_stmt);
39+
maybe_lint_redundant_semis(cx, &mut seq);
4440
}
4541
}
4642

47-
fn maybe_lint_redundant_semis(
48-
cx: &EarlyContext<'_>,
49-
seq: &mut Option<(Span, bool)>,
50-
after_item_stmt: bool,
51-
) {
43+
fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) {
5244
if let Some((span, multiple)) = seq.take() {
5345
// FIXME: Find a better way of ignoring the trailing
5446
// semicolon from macro expansion
5547
if span == rustc_span::DUMMY_SP {
5648
return;
5749
}
5850

59-
// FIXME: Lint on semicolons after item statements
60-
// once doing so doesn't break bootstrapping
61-
if after_item_stmt {
62-
return;
63-
}
64-
6551
cx.struct_span_lint(REDUNDANT_SEMICOLONS, span, |lint| {
6652
let (msg, rem) = if multiple {
6753
("unnecessary trailing semicolons", "remove these semicolons")

src/librustdoc/doctest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,12 @@ crate fn make_test(
558558
"fn main() {{ {}fn {}() -> Result<(), impl core::fmt::Debug> {{\n",
559559
inner_attr, inner_fn_name
560560
),
561-
format!("\n}}; {}().unwrap() }}", inner_fn_name),
561+
format!("\n}} {}().unwrap() }}", inner_fn_name),
562562
)
563563
} else if test_id.is_some() {
564564
(
565565
format!("fn main() {{ {}fn {}() {{\n", inner_attr, inner_fn_name),
566-
format!("\n}}; {}() }}", inner_fn_name),
566+
format!("\n}} {}() }}", inner_fn_name),
567567
)
568568
} else {
569569
("fn main() {\n".into(), "\n}".into())

src/librustdoc/doctest/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ use std::io;
292292
let mut input = String::new();
293293
io::stdin().read_line(&mut input)?;
294294
Ok::<(), io:Error>(())
295-
}; _inner().unwrap() }"
295+
} _inner().unwrap() }"
296296
.to_string();
297297
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
298298
assert_eq!((output, len), (expected, 2));
@@ -306,7 +306,7 @@ fn make_test_named_wrapper() {
306306
let expected = "#![allow(unused)]
307307
fn main() { #[allow(non_snake_case)] fn _doctest_main__some_unique_name() {
308308
assert_eq!(2+2, 4);
309-
}; _doctest_main__some_unique_name() }"
309+
} _doctest_main__some_unique_name() }"
310310
.to_string();
311311
let (output, len, _) =
312312
make_test(input, None, false, &opts, DEFAULT_EDITION, Some("_some_unique_name"));

src/librustdoc/html/markdown.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ fn markdown_summary_with_limit(md: &str, length_limit: usize) -> (String, bool)
10271027
fn push(s: &mut String, text_length: &mut usize, text: &str) {
10281028
s.push_str(text);
10291029
*text_length += text.len();
1030-
};
1030+
}
10311031

10321032
'outer: for event in Parser::new_ext(md, summary_opts()) {
10331033
match &event {
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
// check-pass
2-
// This test should stop compiling
3-
// we decide to enable this lint for item statements.
4-
51
#![deny(redundant_semicolons)]
62

73
fn main() {
8-
fn inner() {};
9-
struct Bar {};
4+
fn inner() {}; //~ ERROR unnecessary
5+
struct Bar {}; //~ ERROR unnecessary
106
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: unnecessary trailing semicolon
2+
--> $DIR/item-stmt-semi.rs:4:18
3+
|
4+
LL | fn inner() {};
5+
| ^ help: remove this semicolon
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/item-stmt-semi.rs:1:9
9+
|
10+
LL | #![deny(redundant_semicolons)]
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
13+
error: unnecessary trailing semicolon
14+
--> $DIR/item-stmt-semi.rs:5:18
15+
|
16+
LL | struct Bar {};
17+
| ^ help: remove this semicolon
18+
19+
error: aborting due to 2 previous errors
20+

src/tools/clippy/tests/ui/match_expr_like_matches_macro.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn main() {
3939
B(i32),
4040
C,
4141
D,
42-
};
42+
}
4343
let x = E::A(2);
4444
{
4545
// lint

src/tools/clippy/tests/ui/match_expr_like_matches_macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn main() {
5151
B(i32),
5252
C,
5353
D,
54-
};
54+
}
5555
let x = E::A(2);
5656
{
5757
// lint

0 commit comments

Comments
 (0)