Skip to content

Commit 4b3a9c0

Browse files
committed
Auto merge of rust-lang#12167 - J-ZhengLi:issue12133, r=Alexendoo
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
2 parents b08ffee + 0e961cd commit 4b3a9c0

5 files changed

+111
-7
lines changed

clippy_lints/src/semicolon_if_nothing_returned.rs

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_errors::Applicability;
55
use rustc_hir::{Block, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::declare_lint_pass;
8+
use rustc_span::{ExpnKind, MacroKind, Span};
89

910
declare_clippy_lint! {
1011
/// ### What it does
@@ -39,6 +40,7 @@ impl<'tcx> LateLintPass<'tcx> for SemicolonIfNothingReturned {
3940
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
4041
if !block.span.from_expansion()
4142
&& let Some(expr) = block.expr
43+
&& !from_attr_macro(expr.span)
4244
&& let t_expr = cx.typeck_results().expr_ty(expr)
4345
&& t_expr.is_unit()
4446
&& let mut app = Applicability::MachineApplicable
@@ -63,3 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for SemicolonIfNothingReturned {
6365
}
6466
}
6567
}
68+
69+
fn from_attr_macro(span: Span) -> bool {
70+
matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Attr, _))
71+
}

tests/ui/auxiliary/proc_macro_attr.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use quote::{quote, quote_spanned};
1111
use syn::spanned::Spanned;
1212
use syn::token::Star;
1313
use syn::{
14-
parse_macro_input, parse_quote, FnArg, ImplItem, ItemImpl, ItemTrait, Lifetime, Pat, PatIdent, PatType, Signature,
15-
TraitItem, Type,
14+
parse_macro_input, parse_quote, FnArg, ImplItem, ItemFn, ItemImpl, ItemTrait, Lifetime, Pat, PatIdent, PatType,
15+
Signature, TraitItem, Type,
1616
};
1717

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

9696
TokenStream::from(quote!(#item))
9797
}
98+
99+
#[proc_macro_attribute]
100+
pub fn fake_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
101+
let mut item = parse_macro_input!(item as ItemFn);
102+
let span = item.block.brace_token.span;
103+
104+
if item.sig.asyncness.is_some() {
105+
item.sig.asyncness = None;
106+
}
107+
108+
let crate_name = quote! { fake_crate };
109+
let block = item.block;
110+
item.block = syn::parse_quote_spanned! {
111+
span =>
112+
{
113+
#crate_name::block_on(async {
114+
#block
115+
})
116+
}
117+
};
118+
119+
quote! {
120+
mod #crate_name {
121+
pub fn block_on<F: ::std::future::Future>(_fut: F) {}
122+
}
123+
124+
#item
125+
}
126+
.into()
127+
}

tests/ui/semicolon_if_nothing_returned.fixed

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
//@aux-build:proc_macro_attr.rs
2+
13
#![warn(clippy::semicolon_if_nothing_returned)]
24
#![allow(clippy::redundant_closure, clippy::uninlined_format_args, clippy::needless_late_init)]
35

6+
#[macro_use]
7+
extern crate proc_macro_attr;
8+
49
fn get_unit() {}
510

611
// the functions below trigger the lint
@@ -120,3 +125,32 @@ fn let_else_stmts() {
120125
return;
121126
};
122127
}
128+
129+
mod issue12123 {
130+
#[rustfmt::skip]
131+
mod this_triggers {
132+
#[fake_main]
133+
async fn main() {
134+
135+
}
136+
}
137+
138+
mod and_this {
139+
#[fake_main]
140+
async fn main() {
141+
println!("hello");
142+
}
143+
}
144+
145+
#[rustfmt::skip]
146+
mod maybe_this {
147+
/** */ #[fake_main]
148+
async fn main() {
149+
}
150+
}
151+
152+
mod but_this_does_not {
153+
#[fake_main]
154+
async fn main() {}
155+
}
156+
}

tests/ui/semicolon_if_nothing_returned.rs

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
//@aux-build:proc_macro_attr.rs
2+
13
#![warn(clippy::semicolon_if_nothing_returned)]
24
#![allow(clippy::redundant_closure, clippy::uninlined_format_args, clippy::needless_late_init)]
35

6+
#[macro_use]
7+
extern crate proc_macro_attr;
8+
49
fn get_unit() {}
510

611
// the functions below trigger the lint
@@ -120,3 +125,32 @@ fn let_else_stmts() {
120125
return;
121126
};
122127
}
128+
129+
mod issue12123 {
130+
#[rustfmt::skip]
131+
mod this_triggers {
132+
#[fake_main]
133+
async fn main() {
134+
135+
}
136+
}
137+
138+
mod and_this {
139+
#[fake_main]
140+
async fn main() {
141+
println!("hello");
142+
}
143+
}
144+
145+
#[rustfmt::skip]
146+
mod maybe_this {
147+
/** */ #[fake_main]
148+
async fn main() {
149+
}
150+
}
151+
152+
mod but_this_does_not {
153+
#[fake_main]
154+
async fn main() {}
155+
}
156+
}

tests/ui/semicolon_if_nothing_returned.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: consider adding a `;` to the last statement for consistent formatting
2-
--> $DIR/semicolon_if_nothing_returned.rs:8:5
2+
--> $DIR/semicolon_if_nothing_returned.rs:13:5
33
|
44
LL | println!("Hello")
55
| ^^^^^^^^^^^^^^^^^ help: add a `;` here: `println!("Hello");`
@@ -8,25 +8,25 @@ LL | println!("Hello")
88
= help: to override `-D warnings` add `#[allow(clippy::semicolon_if_nothing_returned)]`
99

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

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

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

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

0 commit comments

Comments
 (0)