Skip to content

Commit

Permalink
Auto merge of rust-lang#12167 - J-ZhengLi:issue12133, r=Alexendoo
Browse files Browse the repository at this point in the history
fix FP on [`semicolon_if_nothing_returned`]

fixes: rust-lang#12123

---

changelog: fix FP on [`semicolon_if_nothing_returned`] which suggesting adding semicolon after attr macro
  • Loading branch information
bors committed Jan 18, 2024
2 parents b08ffee + 0e961cd commit 4b3a9c0
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 7 deletions.
6 changes: 6 additions & 0 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 @@ -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
Expand All @@ -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, _))
}
34 changes: 32 additions & 2 deletions tests/ui/auxiliary/proc_macro_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use quote::{quote, quote_spanned};
use syn::spanned::Spanned;
use syn::token::Star;
use syn::{
parse_macro_input, parse_quote, FnArg, ImplItem, ItemImpl, ItemTrait, Lifetime, Pat, PatIdent, PatType, Signature,
TraitItem, Type,
parse_macro_input, parse_quote, FnArg, ImplItem, ItemFn, ItemImpl, ItemTrait, Lifetime, Pat, PatIdent, PatType,
Signature, TraitItem, Type,
};

#[proc_macro_attribute]
Expand Down Expand Up @@ -95,3 +95,33 @@ pub fn rename_my_lifetimes(_args: TokenStream, input: TokenStream) -> TokenStrea

TokenStream::from(quote!(#item))
}

#[proc_macro_attribute]
pub fn fake_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
let mut item = parse_macro_input!(item as ItemFn);
let span = item.block.brace_token.span;

if item.sig.asyncness.is_some() {
item.sig.asyncness = None;
}

let crate_name = quote! { fake_crate };
let block = item.block;
item.block = syn::parse_quote_spanned! {
span =>
{
#crate_name::block_on(async {
#block
})
}
};

quote! {
mod #crate_name {
pub fn block_on<F: ::std::future::Future>(_fut: F) {}
}

#item
}
.into()
}
34 changes: 34 additions & 0 deletions tests/ui/semicolon_if_nothing_returned.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//@aux-build:proc_macro_attr.rs

#![warn(clippy::semicolon_if_nothing_returned)]
#![allow(clippy::redundant_closure, clippy::uninlined_format_args, clippy::needless_late_init)]

#[macro_use]
extern crate proc_macro_attr;

fn get_unit() {}

// the functions below trigger the lint
Expand Down Expand Up @@ -120,3 +125,32 @@ fn let_else_stmts() {
return;
};
}

mod issue12123 {
#[rustfmt::skip]
mod this_triggers {
#[fake_main]
async fn main() {

}
}

mod and_this {
#[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() {}
}
}
34 changes: 34 additions & 0 deletions tests/ui/semicolon_if_nothing_returned.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//@aux-build:proc_macro_attr.rs

#![warn(clippy::semicolon_if_nothing_returned)]
#![allow(clippy::redundant_closure, clippy::uninlined_format_args, clippy::needless_late_init)]

#[macro_use]
extern crate proc_macro_attr;

fn get_unit() {}

// the functions below trigger the lint
Expand Down Expand Up @@ -120,3 +125,32 @@ fn let_else_stmts() {
return;
};
}

mod issue12123 {
#[rustfmt::skip]
mod this_triggers {
#[fake_main]
async fn main() {

}
}

mod and_this {
#[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() {}
}
}
10 changes: 5 additions & 5 deletions tests/ui/semicolon_if_nothing_returned.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: consider adding a `;` to the last statement for consistent formatting
--> $DIR/semicolon_if_nothing_returned.rs:8:5
--> $DIR/semicolon_if_nothing_returned.rs:13:5
|
LL | println!("Hello")
| ^^^^^^^^^^^^^^^^^ help: add a `;` here: `println!("Hello");`
Expand All @@ -8,25 +8,25 @@ LL | println!("Hello")
= help: to override `-D warnings` add `#[allow(clippy::semicolon_if_nothing_returned)]`

error: consider adding a `;` to the last statement for consistent formatting
--> $DIR/semicolon_if_nothing_returned.rs:12:5
--> $DIR/semicolon_if_nothing_returned.rs:17:5
|
LL | get_unit()
| ^^^^^^^^^^ help: add a `;` here: `get_unit();`

error: consider adding a `;` to the last statement for consistent formatting
--> $DIR/semicolon_if_nothing_returned.rs:17:5
--> $DIR/semicolon_if_nothing_returned.rs:22:5
|
LL | y = x + 1
| ^^^^^^^^^ help: add a `;` here: `y = x + 1;`

error: consider adding a `;` to the last statement for consistent formatting
--> $DIR/semicolon_if_nothing_returned.rs:23:9
--> $DIR/semicolon_if_nothing_returned.rs:28:9
|
LL | hello()
| ^^^^^^^ help: add a `;` here: `hello();`

error: consider adding a `;` to the last statement for consistent formatting
--> $DIR/semicolon_if_nothing_returned.rs:34:9
--> $DIR/semicolon_if_nothing_returned.rs:39:9
|
LL | ptr::drop_in_place(s.as_mut_ptr())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `ptr::drop_in_place(s.as_mut_ptr());`
Expand Down

0 comments on commit 4b3a9c0

Please sign in to comment.