Skip to content

Commit 0e961cd

Browse files
committed
fix suggestion error with attr macros
1 parent 9fe7c6a commit 0e961cd

4 files changed

+25
-17
lines changed

clippy_lints/src/semicolon_if_nothing_returned.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_errors::Applicability;
55
use rustc_hir::{Block, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::declare_lint_pass;
8+
use rustc_span::{ExpnKind, MacroKind, Span};
89

910
declare_clippy_lint! {
1011
/// ### What it does
@@ -39,6 +40,7 @@ impl<'tcx> LateLintPass<'tcx> for SemicolonIfNothingReturned {
3940
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
4041
if !block.span.from_expansion()
4142
&& let Some(expr) = block.expr
43+
&& !from_attr_macro(expr.span)
4244
&& let t_expr = cx.typeck_results().expr_ty(expr)
4345
&& t_expr.is_unit()
4446
&& let mut app = Applicability::MachineApplicable
@@ -63,3 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for SemicolonIfNothingReturned {
6365
}
6466
}
6567
}
68+
69+
fn from_attr_macro(span: Span) -> bool {
70+
matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Attr, _))
71+
}

tests/ui/semicolon_if_nothing_returned.fixed

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,26 @@ fn let_else_stmts() {
129129
mod issue12123 {
130130
#[rustfmt::skip]
131131
mod this_triggers {
132-
#[fake_main];
132+
#[fake_main]
133133
async fn main() {
134-
134+
135135
}
136136
}
137137

138138
mod and_this {
139-
#[fake_main];
139+
#[fake_main]
140140
async fn main() {
141141
println!("hello");
142142
}
143143
}
144144

145+
#[rustfmt::skip]
146+
mod maybe_this {
147+
/** */ #[fake_main]
148+
async fn main() {
149+
}
150+
}
151+
145152
mod but_this_does_not {
146153
#[fake_main]
147154
async fn main() {}

tests/ui/semicolon_if_nothing_returned.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ mod issue12123 {
131131
mod this_triggers {
132132
#[fake_main]
133133
async fn main() {
134-
134+
135135
}
136136
}
137137

@@ -142,6 +142,13 @@ mod issue12123 {
142142
}
143143
}
144144

145+
#[rustfmt::skip]
146+
mod maybe_this {
147+
/** */ #[fake_main]
148+
async fn main() {
149+
}
150+
}
151+
145152
mod but_this_does_not {
146153
#[fake_main]
147154
async fn main() {}

tests/ui/semicolon_if_nothing_returned.stderr

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,5 @@ error: consider adding a `;` to the last statement for consistent formatting
3131
LL | ptr::drop_in_place(s.as_mut_ptr())
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `ptr::drop_in_place(s.as_mut_ptr());`
3333

34-
error: consider adding a `;` to the last statement for consistent formatting
35-
--> $DIR/semicolon_if_nothing_returned.rs:132:9
36-
|
37-
LL | #[fake_main]
38-
| ^^^^^^^^^^^^ help: add a `;` here: `#[fake_main];`
39-
40-
error: consider adding a `;` to the last statement for consistent formatting
41-
--> $DIR/semicolon_if_nothing_returned.rs:139:9
42-
|
43-
LL | #[fake_main]
44-
| ^^^^^^^^^^^^ help: add a `;` here: `#[fake_main];`
45-
46-
error: aborting due to 7 previous errors
34+
error: aborting due to 5 previous errors
4735

0 commit comments

Comments
 (0)