Skip to content

Commit 319ed5f

Browse files
Review ignore external/proc macros
1 parent 35d7ae9 commit 319ed5f

File tree

4 files changed

+129
-10
lines changed

4 files changed

+129
-10
lines changed

clippy_lints/src/anon_trait_import.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_config::Conf;
33
use clippy_utils::diagnostics::span_lint_and_sugg;
4+
use clippy_utils::is_from_proc_macro;
45
use clippy_utils::source::snippet_opt;
56
use rustc_errors::Applicability;
67
use rustc_hir::def::{DefKind, Res};
78
use rustc_hir::{Item, ItemKind, UseKind};
8-
use rustc_lint::{LateContext, LateLintPass};
9+
use rustc_lint::{LateContext, LateLintPass, LintContext as _};
10+
use rustc_middle::lint::in_external_macro;
911
use rustc_middle::ty::Visibility;
1012
use rustc_session::impl_lint_pass;
1113
use rustc_span::symbol::kw;
@@ -61,6 +63,7 @@ impl_lint_pass!(AnonTraitImport => [ANON_TRAIT_IMPORT]);
6163
impl<'tcx> LateLintPass<'tcx> for AnonTraitImport {
6264
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
6365
if self.msrv.meets(msrvs::UNDERSCORE_IMPORTS)
66+
&& !in_external_macro(cx.sess(), item.span)
6467
&& let ItemKind::Use(path, UseKind::Single) = item.kind
6568
// Ignore imports that already use Underscore
6669
&& item.ident.name != kw::Underscore
@@ -72,6 +75,7 @@ impl<'tcx> LateLintPass<'tcx> for AnonTraitImport {
7275
&& cx.tcx.visibility(item.owner_id.def_id) == Visibility::Restricted(module.to_def_id())
7376
&& let Some(last_segment) = path.segments.last()
7477
&& let Some(snip) = snippet_opt(cx, last_segment.ident.span)
78+
&& !is_from_proc_macro(cx, &last_segment.ident)
7579
{
7680
let complete_span = last_segment.ident.span.to(item.ident.span);
7781
span_lint_and_sugg(

tests/ui/anon_trait_import.fixed

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
//@aux-build:proc_macros.rs
2+
13
#![allow(unused)]
24
#![warn(clippy::anon_trait_import)]
5+
#![feature(decl_macro)]
6+
7+
extern crate proc_macros;
38

49
fn main() {}
510

@@ -186,3 +191,50 @@ fn msrv_1_33() {
186191
use simple_trait::{MyStruct, MyTrait as _};
187192
MyStruct.do_things();
188193
}
194+
195+
mod lint_inside_macro_expansion_bad {
196+
macro_rules! foo {
197+
() => {
198+
use std::any::Any as _;
199+
fn bar() {
200+
"bar".type_id();
201+
}
202+
};
203+
}
204+
205+
foo!();
206+
}
207+
208+
mod macro_and_trait_same_name {
209+
pub macro Foo() {}
210+
pub trait Foo {
211+
fn bar(&self);
212+
}
213+
impl Foo for () {
214+
fn bar(&self) {}
215+
}
216+
}
217+
218+
fn call_macro_and_trait_good() {
219+
// importing trait and macro but only using macro by path won't allow us to change this to
220+
// `use macro_and_trait_same_name::Foo as _;`
221+
use macro_and_trait_same_name::Foo;
222+
Foo!();
223+
().bar();
224+
}
225+
226+
proc_macros::external!(
227+
fn ignore_inside_external_proc_macro() {
228+
use std::any::Any;
229+
"foo".type_id();
230+
}
231+
);
232+
233+
proc_macros::with_span!(
234+
span
235+
236+
fn ignore_inside_with_span_proc_macro() {
237+
use std::any::Any;
238+
"foo".type_id();
239+
}
240+
);

tests/ui/anon_trait_import.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
//@aux-build:proc_macros.rs
2+
13
#![allow(unused)]
24
#![warn(clippy::anon_trait_import)]
5+
#![feature(decl_macro)]
6+
7+
extern crate proc_macros;
38

49
fn main() {}
510

@@ -186,3 +191,50 @@ fn msrv_1_33() {
186191
use simple_trait::{MyStruct, MyTrait};
187192
MyStruct.do_things();
188193
}
194+
195+
mod lint_inside_macro_expansion_bad {
196+
macro_rules! foo {
197+
() => {
198+
use std::any::Any;
199+
fn bar() {
200+
"bar".type_id();
201+
}
202+
};
203+
}
204+
205+
foo!();
206+
}
207+
208+
mod macro_and_trait_same_name {
209+
pub macro Foo() {}
210+
pub trait Foo {
211+
fn bar(&self);
212+
}
213+
impl Foo for () {
214+
fn bar(&self) {}
215+
}
216+
}
217+
218+
fn call_macro_and_trait_good() {
219+
// importing trait and macro but only using macro by path won't allow us to change this to
220+
// `use macro_and_trait_same_name::Foo as _;`
221+
use macro_and_trait_same_name::Foo;
222+
Foo!();
223+
().bar();
224+
}
225+
226+
proc_macros::external!(
227+
fn ignore_inside_external_proc_macro() {
228+
use std::any::Any;
229+
"foo".type_id();
230+
}
231+
);
232+
233+
proc_macros::with_span!(
234+
span
235+
236+
fn ignore_inside_with_span_proc_macro() {
237+
use std::any::Any;
238+
"foo".type_id();
239+
}
240+
);

tests/ui/anon_trait_import.stderr

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: importing trait that is only used anonymously
2-
--> tests/ui/anon_trait_import.rs:7:19
2+
--> tests/ui/anon_trait_import.rs:12:19
33
|
44
LL | use std::any::Any;
55
| ^^^ help: use: `Any as _`
@@ -8,46 +8,57 @@ LL | use std::any::Any;
88
= help: to override `-D warnings` add `#[allow(clippy::anon_trait_import)]`
99

1010
error: importing trait that is only used anonymously
11-
--> tests/ui/anon_trait_import.rs:26:26
11+
--> tests/ui/anon_trait_import.rs:31:26
1212
|
1313
LL | use std::any::{self, Any, TypeId};
1414
| ^^^ help: use: `Any as _`
1515

1616
error: importing trait that is only used anonymously
17-
--> tests/ui/anon_trait_import.rs:38:19
17+
--> tests/ui/anon_trait_import.rs:43:19
1818
|
1919
LL | use std::any::Any as MyAny;
2020
| ^^^^^^^^^^^^ help: use: `Any as _`
2121

2222
error: importing trait that is only used anonymously
23-
--> tests/ui/anon_trait_import.rs:44:20
23+
--> tests/ui/anon_trait_import.rs:49:20
2424
|
2525
LL | use std::any::{Any as MyAny, TypeId as MyTypeId};
2626
| ^^^^^^^^^^^^ help: use: `Any as _`
2727

2828
error: importing trait that is only used anonymously
29-
--> tests/ui/anon_trait_import.rs:67:23
29+
--> tests/ui/anon_trait_import.rs:72:23
3030
|
3131
LL | use std::any::Any;
3232
| ^^^ help: use: `Any as _`
3333

3434
error: importing trait that is only used anonymously
35-
--> tests/ui/anon_trait_import.rs:108:19
35+
--> tests/ui/anon_trait_import.rs:113:19
3636
|
3737
LL | use std::any::Any;
3838
| ^^^ help: use: `Any as _`
3939

4040
error: importing trait that is only used anonymously
41-
--> tests/ui/anon_trait_import.rs:127:19
41+
--> tests/ui/anon_trait_import.rs:132:19
4242
|
4343
LL | use std::any::Any;
4444
| ^^^ help: use: `Any as _`
4545

4646
error: importing trait that is only used anonymously
47-
--> tests/ui/anon_trait_import.rs:186:34
47+
--> tests/ui/anon_trait_import.rs:191:34
4848
|
4949
LL | use simple_trait::{MyStruct, MyTrait};
5050
| ^^^^^^^ help: use: `MyTrait as _`
5151

52-
error: aborting due to 8 previous errors
52+
error: importing trait that is only used anonymously
53+
--> tests/ui/anon_trait_import.rs:198:27
54+
|
55+
LL | use std::any::Any;
56+
| ^^^ help: use: `Any as _`
57+
...
58+
LL | foo!();
59+
| ------ in this macro invocation
60+
|
61+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
62+
63+
error: aborting due to 9 previous errors
5364

0 commit comments

Comments
 (0)