From 0e961cd854c9b1752d00a2b53ec3b5b0ee668e34 Mon Sep 17 00:00:00 2001 From: J-ZhengLi Date: Thu, 18 Jan 2024 18:53:41 +0800 Subject: [PATCH] fix suggestion error with attr macros --- clippy_lints/src/semicolon_if_nothing_returned.rs | 6 ++++++ tests/ui/semicolon_if_nothing_returned.fixed | 13 ++++++++++--- tests/ui/semicolon_if_nothing_returned.rs | 9 ++++++++- tests/ui/semicolon_if_nothing_returned.stderr | 14 +------------- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/clippy_lints/src/semicolon_if_nothing_returned.rs b/clippy_lints/src/semicolon_if_nothing_returned.rs index 2cd3e57f885a..6540626f7d5a 100644 --- a/clippy_lints/src/semicolon_if_nothing_returned.rs +++ b/clippy_lints/src/semicolon_if_nothing_returned.rs @@ -5,6 +5,7 @@ use rustc_errors::Applicability; use rustc_hir::{Block, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; +use rustc_span::{ExpnKind, MacroKind, Span}; declare_clippy_lint! { /// ### What it does @@ -39,6 +40,7 @@ impl<'tcx> LateLintPass<'tcx> for SemicolonIfNothingReturned { fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) { if !block.span.from_expansion() && let Some(expr) = block.expr + && !from_attr_macro(expr.span) && let t_expr = cx.typeck_results().expr_ty(expr) && t_expr.is_unit() && let mut app = Applicability::MachineApplicable @@ -63,3 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for SemicolonIfNothingReturned { } } } + +fn from_attr_macro(span: Span) -> bool { + matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Attr, _)) +} diff --git a/tests/ui/semicolon_if_nothing_returned.fixed b/tests/ui/semicolon_if_nothing_returned.fixed index bc96b959fc54..cdfa5d9cc780 100644 --- a/tests/ui/semicolon_if_nothing_returned.fixed +++ b/tests/ui/semicolon_if_nothing_returned.fixed @@ -129,19 +129,26 @@ fn let_else_stmts() { mod issue12123 { #[rustfmt::skip] mod this_triggers { - #[fake_main]; + #[fake_main] async fn main() { - + } } mod and_this { - #[fake_main]; + #[fake_main] async fn main() { println!("hello"); } } + #[rustfmt::skip] + mod maybe_this { + /** */ #[fake_main] + async fn main() { + } + } + mod but_this_does_not { #[fake_main] async fn main() {} diff --git a/tests/ui/semicolon_if_nothing_returned.rs b/tests/ui/semicolon_if_nothing_returned.rs index e5713ce062bc..315b7e4f3839 100644 --- a/tests/ui/semicolon_if_nothing_returned.rs +++ b/tests/ui/semicolon_if_nothing_returned.rs @@ -131,7 +131,7 @@ mod issue12123 { mod this_triggers { #[fake_main] async fn main() { - + } } @@ -142,6 +142,13 @@ mod issue12123 { } } + #[rustfmt::skip] + mod maybe_this { + /** */ #[fake_main] + async fn main() { + } + } + mod but_this_does_not { #[fake_main] async fn main() {} diff --git a/tests/ui/semicolon_if_nothing_returned.stderr b/tests/ui/semicolon_if_nothing_returned.stderr index 0d5381ef6212..09c4d12f216c 100644 --- a/tests/ui/semicolon_if_nothing_returned.stderr +++ b/tests/ui/semicolon_if_nothing_returned.stderr @@ -31,17 +31,5 @@ error: consider adding a `;` to the last statement for consistent formatting LL | ptr::drop_in_place(s.as_mut_ptr()) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `ptr::drop_in_place(s.as_mut_ptr());` -error: consider adding a `;` to the last statement for consistent formatting - --> $DIR/semicolon_if_nothing_returned.rs:132:9 - | -LL | #[fake_main] - | ^^^^^^^^^^^^ help: add a `;` here: `#[fake_main];` - -error: consider adding a `;` to the last statement for consistent formatting - --> $DIR/semicolon_if_nothing_returned.rs:139:9 - | -LL | #[fake_main] - | ^^^^^^^^^^^^ help: add a `;` here: `#[fake_main];` - -error: aborting due to 7 previous errors +error: aborting due to 5 previous errors