fix suggestion error with attr macros

This commit is contained in:
J-ZhengLi
2024-01-18 18:53:41 +08:00
parent 9fe7c6a7ec
commit 0e961cd854
4 changed files with 25 additions and 17 deletions
@@ -5,6 +5,7 @@
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 @@ fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
}
}
}
fn from_attr_macro(span: Span) -> bool {
matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Attr, _))
}
+10 -3
View File
@@ -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() {}
+8 -1
View File
@@ -131,7 +131,7 @@ mod issue12123 {
mod this_triggers {
#[fake_main]
async fn main() {
}
}
@@ -142,6 +142,13 @@ async fn main() {
}
}
#[rustfmt::skip]
mod maybe_this {
/** */ #[fake_main]
async fn main() {
}
}
mod but_this_does_not {
#[fake_main]
async fn main() {}
+1 -13
View File
@@ -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