Skip to content

Commit

Permalink
Rollup merge of rust-lang#79812 - Aaron1011:lint-item-trailing-semi, …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
Dylan-DPC committed Dec 28, 2020
2 parents 58aefbd + fdc336c commit 761400f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
20 changes: 3 additions & 17 deletions compiler/rustc_lint/src/redundant_semicolon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,26 @@ declare_lint_pass!(RedundantSemicolons => [REDUNDANT_SEMICOLONS]);

impl EarlyLintPass for RedundantSemicolons {
fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
let mut after_item_stmt = false;
let mut seq = None;
for stmt in block.stmts.iter() {
match (&stmt.kind, &mut seq) {
(StmtKind::Empty, None) => seq = Some((stmt.span, false)),
(StmtKind::Empty, Some(seq)) => *seq = (seq.0.to(stmt.span), true),
(_, seq) => {
maybe_lint_redundant_semis(cx, seq, after_item_stmt);
after_item_stmt = matches!(stmt.kind, StmtKind::Item(_));
}
(_, seq) => maybe_lint_redundant_semis(cx, seq),
}
}
maybe_lint_redundant_semis(cx, &mut seq, after_item_stmt);
maybe_lint_redundant_semis(cx, &mut seq);
}
}

fn maybe_lint_redundant_semis(
cx: &EarlyContext<'_>,
seq: &mut Option<(Span, bool)>,
after_item_stmt: bool,
) {
fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) {
if let Some((span, multiple)) = seq.take() {
// FIXME: Find a better way of ignoring the trailing
// semicolon from macro expansion
if span == rustc_span::DUMMY_SP {
return;
}

// FIXME: Lint on semicolons after item statements
// once doing so doesn't break bootstrapping
if after_item_stmt {
return;
}

cx.struct_span_lint(REDUNDANT_SEMICOLONS, span, |lint| {
let (msg, rem) = if multiple {
("unnecessary trailing semicolons", "remove these semicolons")
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ fn markdown_summary_with_limit(md: &str, length_limit: usize) -> (String, bool)
fn push(s: &mut String, text_length: &mut usize, text: &str) {
s.push_str(text);
*text_length += text.len();
};
}

'outer: for event in Parser::new_ext(md, summary_opts()) {
match &event {
Expand Down
8 changes: 2 additions & 6 deletions src/test/ui/lint/redundant-semicolon/item-stmt-semi.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// check-pass
// This test should stop compiling
// we decide to enable this lint for item statements.

#![deny(redundant_semicolons)]

fn main() {
fn inner() {};
struct Bar {};
fn inner() {}; //~ ERROR unnecessary
struct Bar {}; //~ ERROR unnecessary
}
20 changes: 20 additions & 0 deletions src/test/ui/lint/redundant-semicolon/item-stmt-semi.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: unnecessary trailing semicolon
--> $DIR/item-stmt-semi.rs:4:18
|
LL | fn inner() {};
| ^ help: remove this semicolon
|
note: the lint level is defined here
--> $DIR/item-stmt-semi.rs:1:9
|
LL | #![deny(redundant_semicolons)]
| ^^^^^^^^^^^^^^^^^^^^

error: unnecessary trailing semicolon
--> $DIR/item-stmt-semi.rs:5:18
|
LL | struct Bar {};
| ^ help: remove this semicolon

error: aborting due to 2 previous errors

0 comments on commit 761400f

Please sign in to comment.