diff options
| author | J-ZhengLi <lizheng135@huawei.com> | 2024-01-18 18:53:41 +0800 |
|---|---|---|
| committer | J-ZhengLi <lizheng135@huawei.com> | 2024-01-18 18:53:41 +0800 |
| commit | 0e961cd854c9b1752d00a2b53ec3b5b0ee668e34 (patch) | |
| tree | e7ad3a7f5f0d9ec1c30aa2241e3d8dff78839904 | |
| parent | 9fe7c6a7ec4ff03300c69046e2055b6619956864 (diff) | |
| download | rust-0e961cd854c9b1752d00a2b53ec3b5b0ee668e34.tar.gz rust-0e961cd854c9b1752d00a2b53ec3b5b0ee668e34.zip | |
fix suggestion error with attr macros
| -rw-r--r-- | clippy_lints/src/semicolon_if_nothing_returned.rs | 6 | ||||
| -rw-r--r-- | tests/ui/semicolon_if_nothing_returned.fixed | 13 | ||||
| -rw-r--r-- | tests/ui/semicolon_if_nothing_returned.rs | 9 | ||||
| -rw-r--r-- | 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 2cd3e57f885..6540626f7d5 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 bc96b959fc5..cdfa5d9cc78 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 e5713ce062b..315b7e4f383 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 0d5381ef621..09c4d12f216 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 |
