Skip to content

Commit

Permalink
fix suggestion error with attr macros
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed Jan 18, 2024
1 parent 9fe7c6a commit 80b306b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
9 changes: 7 additions & 2 deletions clippy_lints/src/semicolon_if_nothing_returned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,8 +38,8 @@ declare_lint_pass!(SemicolonIfNothingReturned => [SEMICOLON_IF_NOTHING_RETURNED]

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
if 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
Expand All @@ -63,3 +64,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, _))
}
13 changes: 10 additions & 3 deletions tests/ui/semicolon_if_nothing_returned.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
9 changes: 8 additions & 1 deletion tests/ui/semicolon_if_nothing_returned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ mod issue12123 {
mod this_triggers {
#[fake_main]
async fn main() {

}
}

Expand All @@ -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() {}
Expand Down
14 changes: 1 addition & 13 deletions tests/ui/semicolon_if_nothing_returned.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 80b306b

Please sign in to comment.