Skip to content

Commit 85c2b1e

Browse files
committed
Switched to snippet_with_macro_callsite
1 parent cd67487 commit 85c2b1e

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

clippy_lints/src/semicolon_if_nothing_returned.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::utils::{in_macro, span_lint_and_then, sugg};
1+
use crate::utils::{in_macro, span_lint_and_sugg, sugg, snippet_with_macro_callsite};
22
use if_chain::if_chain;
33
use rustc_errors::Applicability;
4-
use rustc_hir::*;
4+
use rustc_hir::{Block, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
77

@@ -11,7 +11,6 @@ declare_clippy_lint! {
1111
///
1212
/// **Why is this bad?** The semicolon might be optional but when
1313
/// extending the block with new code, it doesn't require a change in previous last line.
14-
/// It's also more idiomatic.
1514
///
1615
/// **Known problems:** None.
1716
///
@@ -29,7 +28,7 @@ declare_clippy_lint! {
2928
/// }
3029
/// ```
3130
pub SEMICOLON_IF_NOTHING_RETURNED,
32-
pedantic,
31+
restriction,
3332
"add a semicolon if nothing is returned"
3433
}
3534

@@ -42,31 +41,25 @@ impl LateLintPass<'_> for SemicolonIfNothingReturned {
4241
if let Some(expr) = block.expr;
4342
let t_expr = cx.typeck_results().expr_ty(expr);
4443
if t_expr.is_unit();
44+
if let snippet = snippet_with_macro_callsite(cx, expr.span, "}");
45+
if !snippet.ends_with('}');
4546
then {
46-
match expr.kind {
47-
ExprKind::Loop(..) |
48-
ExprKind::Match(..) |
49-
ExprKind::Block(..) |
50-
ExprKind::If(..) if !in_macro(expr.span) => return,
51-
_ => (),
47+
// filter out the desugared `for` loop
48+
if let ExprKind::DropTemps(..) = &expr.kind {
49+
return;
5250
}
5351

5452
let sugg = sugg::Sugg::hir(cx, &expr, "..");
5553
let suggestion = format!("{0};", sugg);
56-
span_lint_and_then(
54+
span_lint_and_sugg(
5755
cx,
5856
SEMICOLON_IF_NOTHING_RETURNED,
5957
expr.span,
60-
"add `;` to terminate block",
61-
| diag | {
62-
diag.span_suggestion(
63-
expr.span,
64-
"add `;`",
65-
suggestion,
66-
Applicability::MaybeIncorrect,
67-
);
68-
}
69-
)
58+
"consider adding a `;` to the last statement for consistent formatting",
59+
"add a `;` here",
60+
suggestion,
61+
Applicability::MaybeIncorrect,
62+
);
7063
}
7164
}
7265
}

tests/ui/semicolon_if_nothing_returned.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,10 @@ fn foobar(x: i32) {
4646
y = x + 1;
4747
}
4848
}
49+
50+
fn loop_test(x: i32) {
51+
let y: i32;
52+
for &ext in &["stdout", "stderr", "fixed"] {
53+
println!("{}", ext);
54+
}
55+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: add `;` to terminate block
1+
error: consider adding a `;` to the last statement for consistent formatting
22
--> $DIR/semicolon_if_nothing_returned.rs:8:5
33
|
44
LL | println!("Hello")
@@ -7,17 +7,17 @@ LL | println!("Hello")
77
= note: `-D clippy::semicolon-if-nothing-returned` implied by `-D warnings`
88
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
99

10-
error: add `;` to terminate block
10+
error: consider adding a `;` to the last statement for consistent formatting
1111
--> $DIR/semicolon_if_nothing_returned.rs:12:5
1212
|
1313
LL | get_unit()
14-
| ^^^^^^^^^^ help: add `;`: `get_unit();`
14+
| ^^^^^^^^^^ help: add a `;` here: `get_unit();`
1515

16-
error: add `;` to terminate block
16+
error: consider adding a `;` to the last statement for consistent formatting
1717
--> $DIR/semicolon_if_nothing_returned.rs:17:5
1818
|
1919
LL | y = x + 1
20-
| ^^^^^^^^^ help: add `;`: `y = x + 1;`
20+
| ^^^^^^^^^ help: add a `;` here: `y = x + 1;`
2121

2222
error: aborting due to 3 previous errors
2323

0 commit comments

Comments
 (0)