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.
This commit is contained in:
commit
3fe423663b
8 changed files with 32 additions and 30 deletions
|
@ -28,27 +28,19 @@ declare_lint_pass!(RedundantSemicolons => [REDUNDANT_SEMICOLONS]);
|
||||||
|
|
||||||
impl EarlyLintPass for RedundantSemicolons {
|
impl EarlyLintPass for RedundantSemicolons {
|
||||||
fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
|
fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
|
||||||
let mut after_item_stmt = false;
|
|
||||||
let mut seq = None;
|
let mut seq = None;
|
||||||
for stmt in block.stmts.iter() {
|
for stmt in block.stmts.iter() {
|
||||||
match (&stmt.kind, &mut seq) {
|
match (&stmt.kind, &mut seq) {
|
||||||
(StmtKind::Empty, None) => seq = Some((stmt.span, false)),
|
(StmtKind::Empty, None) => seq = Some((stmt.span, false)),
|
||||||
(StmtKind::Empty, Some(seq)) => *seq = (seq.0.to(stmt.span), true),
|
(StmtKind::Empty, Some(seq)) => *seq = (seq.0.to(stmt.span), true),
|
||||||
(_, seq) => {
|
(_, seq) => maybe_lint_redundant_semis(cx, seq),
|
||||||
maybe_lint_redundant_semis(cx, seq, after_item_stmt);
|
|
||||||
after_item_stmt = matches!(stmt.kind, StmtKind::Item(_));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
maybe_lint_redundant_semis(cx, &mut seq);
|
||||||
maybe_lint_redundant_semis(cx, &mut seq, after_item_stmt);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn maybe_lint_redundant_semis(
|
fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) {
|
||||||
cx: &EarlyContext<'_>,
|
|
||||||
seq: &mut Option<(Span, bool)>,
|
|
||||||
after_item_stmt: bool,
|
|
||||||
) {
|
|
||||||
if let Some((span, multiple)) = seq.take() {
|
if let Some((span, multiple)) = seq.take() {
|
||||||
// FIXME: Find a better way of ignoring the trailing
|
// FIXME: Find a better way of ignoring the trailing
|
||||||
// semicolon from macro expansion
|
// semicolon from macro expansion
|
||||||
|
@ -56,12 +48,6 @@ fn maybe_lint_redundant_semis(
|
||||||
return;
|
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| {
|
cx.struct_span_lint(REDUNDANT_SEMICOLONS, span, |lint| {
|
||||||
let (msg, rem) = if multiple {
|
let (msg, rem) = if multiple {
|
||||||
("unnecessary trailing semicolons", "remove these semicolons")
|
("unnecessary trailing semicolons", "remove these semicolons")
|
||||||
|
|
|
@ -558,12 +558,12 @@ crate fn make_test(
|
||||||
"fn main() {{ {}fn {}() -> Result<(), impl core::fmt::Debug> {{\n",
|
"fn main() {{ {}fn {}() -> Result<(), impl core::fmt::Debug> {{\n",
|
||||||
inner_attr, inner_fn_name
|
inner_attr, inner_fn_name
|
||||||
),
|
),
|
||||||
format!("\n}}; {}().unwrap() }}", inner_fn_name),
|
format!("\n}} {}().unwrap() }}", inner_fn_name),
|
||||||
)
|
)
|
||||||
} else if test_id.is_some() {
|
} else if test_id.is_some() {
|
||||||
(
|
(
|
||||||
format!("fn main() {{ {}fn {}() {{\n", inner_attr, inner_fn_name),
|
format!("fn main() {{ {}fn {}() {{\n", inner_attr, inner_fn_name),
|
||||||
format!("\n}}; {}() }}", inner_fn_name),
|
format!("\n}} {}() }}", inner_fn_name),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
("fn main() {\n".into(), "\n}".into())
|
("fn main() {\n".into(), "\n}".into())
|
||||||
|
|
|
@ -292,7 +292,7 @@ use std::io;
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
io::stdin().read_line(&mut input)?;
|
io::stdin().read_line(&mut input)?;
|
||||||
Ok::<(), io:Error>(())
|
Ok::<(), io:Error>(())
|
||||||
}; _inner().unwrap() }"
|
} _inner().unwrap() }"
|
||||||
.to_string();
|
.to_string();
|
||||||
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
|
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
|
||||||
assert_eq!((output, len), (expected, 2));
|
assert_eq!((output, len), (expected, 2));
|
||||||
|
@ -306,7 +306,7 @@ fn make_test_named_wrapper() {
|
||||||
let expected = "#![allow(unused)]
|
let expected = "#![allow(unused)]
|
||||||
fn main() { #[allow(non_snake_case)] fn _doctest_main__some_unique_name() {
|
fn main() { #[allow(non_snake_case)] fn _doctest_main__some_unique_name() {
|
||||||
assert_eq!(2+2, 4);
|
assert_eq!(2+2, 4);
|
||||||
}; _doctest_main__some_unique_name() }"
|
} _doctest_main__some_unique_name() }"
|
||||||
.to_string();
|
.to_string();
|
||||||
let (output, len, _) =
|
let (output, len, _) =
|
||||||
make_test(input, None, false, &opts, DEFAULT_EDITION, Some("_some_unique_name"));
|
make_test(input, None, false, &opts, DEFAULT_EDITION, Some("_some_unique_name"));
|
||||||
|
|
|
@ -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) {
|
fn push(s: &mut String, text_length: &mut usize, text: &str) {
|
||||||
s.push_str(text);
|
s.push_str(text);
|
||||||
*text_length += text.len();
|
*text_length += text.len();
|
||||||
};
|
}
|
||||||
|
|
||||||
'outer: for event in Parser::new_ext(md, summary_opts()) {
|
'outer: for event in Parser::new_ext(md, summary_opts()) {
|
||||||
match &event {
|
match &event {
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
// check-pass
|
|
||||||
// This test should stop compiling
|
|
||||||
// we decide to enable this lint for item statements.
|
|
||||||
|
|
||||||
#![deny(redundant_semicolons)]
|
#![deny(redundant_semicolons)]
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
fn inner() {};
|
fn inner() {}; //~ ERROR unnecessary
|
||||||
struct Bar {};
|
struct Bar {}; //~ ERROR unnecessary
|
||||||
}
|
}
|
||||||
|
|
20
src/test/ui/lint/redundant-semicolon/item-stmt-semi.stderr
Normal file
20
src/test/ui/lint/redundant-semicolon/item-stmt-semi.stderr
Normal file
|
@ -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
|
||||||
|
|
|
@ -39,7 +39,7 @@ fn main() {
|
||||||
B(i32),
|
B(i32),
|
||||||
C,
|
C,
|
||||||
D,
|
D,
|
||||||
};
|
}
|
||||||
let x = E::A(2);
|
let x = E::A(2);
|
||||||
{
|
{
|
||||||
// lint
|
// lint
|
||||||
|
|
|
@ -51,7 +51,7 @@ fn main() {
|
||||||
B(i32),
|
B(i32),
|
||||||
C,
|
C,
|
||||||
D,
|
D,
|
||||||
};
|
}
|
||||||
let x = E::A(2);
|
let x = E::A(2);
|
||||||
{
|
{
|
||||||
// lint
|
// lint
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue