Skip to content

Commit 81400e2

Browse files
committed
Auto merge of #11589 - koka831:fix/10198, r=giraffate
std_instead_of_core: avoid lint inside of proc-macro - fixes #10198 note: The lint for the reported `thiserror::Error` has been suppressed by [Don't lint unstable moves in std_instead_of_core](https://github.com/rust-lang/rust-clippy/pull/9545/files#diff-2cb8a24429cf9d9898de901450d640115503a10454d692dddc6a073a299fbb7eR29) because `thiserror::Error` internally implements `std::error::Error for (derived struct)`. changelog: [`std_intead_of_core`]: avoid linting inside proc-macro I confirmed this change fixes the problem: <details> <summary>test result without the change</summary> ```console error: used import from `std` instead of `core` --> tests/ui/std_instead_of_core.rs:65:14 | LL | #[derive(ImplStructWithStdDisplay)] | ^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in the derive macro `ImplStructWithStdDisplay` (in Nightly builds, run with -Z macro-backtrace for more info) ``` </details>
2 parents 08c429f + d5cc97e commit 81400e2

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

clippy_lints/src/std_instead_of_core.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_from_proc_macro;
23
use rustc_errors::Applicability;
34
use rustc_hir::def::Res;
45
use rustc_hir::def_id::DefId;
56
use rustc_hir::{HirId, Path, PathSegment};
6-
use rustc_lint::{LateContext, LateLintPass};
7+
use rustc_lint::{LateContext, LateLintPass, LintContext};
8+
use rustc_middle::lint::in_external_macro;
79
use rustc_session::{declare_tool_lint, impl_lint_pass};
810
use rustc_span::symbol::kw;
911
use rustc_span::{sym, Span};
@@ -99,6 +101,8 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
99101
if let Res::Def(_, def_id) = path.res
100102
&& let Some(first_segment) = get_first_segment(path)
101103
&& is_stable(cx, def_id)
104+
&& !in_external_macro(cx.sess(), path.span)
105+
&& !is_from_proc_macro(cx, &first_segment.ident)
102106
{
103107
let (lint, used_mod, replace_with) = match first_segment.ident.name {
104108
sym::std => match cx.tcx.crate_name(def_id.krate) {

tests/ui/auxiliary/proc_macro_derive.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ pub fn derive(_: TokenStream) -> TokenStream {
2121
output
2222
}
2323

24+
#[proc_macro_derive(ImplStructWithStdDisplay)]
25+
pub fn derive_std(_: TokenStream) -> TokenStream {
26+
quote! {
27+
struct A {}
28+
impl ::std::fmt::Display for A {
29+
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
30+
write!(f, "A")
31+
}
32+
}
33+
}
34+
}
35+
2436
#[proc_macro_derive(FieldReassignWithDefault)]
2537
pub fn derive_foo(_input: TokenStream) -> TokenStream {
2638
quote! {

tests/ui/std_instead_of_core.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
//@aux-build:proc_macro_derive.rs
12
#![warn(clippy::std_instead_of_core)]
23
#![allow(unused_imports)]
34

45
extern crate alloc;
56

7+
#[macro_use]
8+
extern crate proc_macro_derive;
9+
610
#[warn(clippy::std_instead_of_core)]
711
fn std_instead_of_core() {
812
// Regular import
@@ -55,6 +59,13 @@ fn alloc_instead_of_core() {
5559
//~^ ERROR: used import from `alloc` instead of `core`
5660
}
5761

62+
mod std_in_proc_macro_derive {
63+
#[warn(clippy::alloc_instead_of_core)]
64+
#[allow(unused)]
65+
#[derive(ImplStructWithStdDisplay)]
66+
struct B {}
67+
}
68+
5869
fn main() {
5970
std_instead_of_core();
6071
std_instead_of_alloc();

tests/ui/std_instead_of_core.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
//@aux-build:proc_macro_derive.rs
12
#![warn(clippy::std_instead_of_core)]
23
#![allow(unused_imports)]
34

45
extern crate alloc;
56

7+
#[macro_use]
8+
extern crate proc_macro_derive;
9+
610
#[warn(clippy::std_instead_of_core)]
711
fn std_instead_of_core() {
812
// Regular import
@@ -55,6 +59,13 @@ fn alloc_instead_of_core() {
5559
//~^ ERROR: used import from `alloc` instead of `core`
5660
}
5761

62+
mod std_in_proc_macro_derive {
63+
#[warn(clippy::alloc_instead_of_core)]
64+
#[allow(unused)]
65+
#[derive(ImplStructWithStdDisplay)]
66+
struct B {}
67+
}
68+
5869
fn main() {
5970
std_instead_of_core();
6071
std_instead_of_alloc();

tests/ui/std_instead_of_core.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: used import from `std` instead of `core`
2-
--> $DIR/std_instead_of_core.rs:9:9
2+
--> $DIR/std_instead_of_core.rs:13:9
33
|
44
LL | use std::hash::Hasher;
55
| ^^^ help: consider importing the item from `core`: `core`
@@ -8,49 +8,49 @@ LL | use std::hash::Hasher;
88
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_core)]`
99

1010
error: used import from `std` instead of `core`
11-
--> $DIR/std_instead_of_core.rs:12:11
11+
--> $DIR/std_instead_of_core.rs:16:11
1212
|
1313
LL | use ::std::hash::Hash;
1414
| ^^^ help: consider importing the item from `core`: `core`
1515

1616
error: used import from `std` instead of `core`
17-
--> $DIR/std_instead_of_core.rs:18:9
17+
--> $DIR/std_instead_of_core.rs:22:9
1818
|
1919
LL | use std::fmt::{Debug, Result};
2020
| ^^^ help: consider importing the item from `core`: `core`
2121

2222
error: used import from `std` instead of `core`
23-
--> $DIR/std_instead_of_core.rs:22:15
23+
--> $DIR/std_instead_of_core.rs:26:15
2424
|
2525
LL | let ptr = std::ptr::null::<u32>();
2626
| ^^^ help: consider importing the item from `core`: `core`
2727

2828
error: used import from `std` instead of `core`
29-
--> $DIR/std_instead_of_core.rs:24:21
29+
--> $DIR/std_instead_of_core.rs:28:21
3030
|
3131
LL | let ptr_mut = ::std::ptr::null_mut::<usize>();
3232
| ^^^ help: consider importing the item from `core`: `core`
3333

3434
error: used import from `std` instead of `core`
35-
--> $DIR/std_instead_of_core.rs:28:16
35+
--> $DIR/std_instead_of_core.rs:32:16
3636
|
3737
LL | let cell = std::cell::Cell::new(8u32);
3838
| ^^^ help: consider importing the item from `core`: `core`
3939

4040
error: used import from `std` instead of `core`
41-
--> $DIR/std_instead_of_core.rs:30:27
41+
--> $DIR/std_instead_of_core.rs:34:27
4242
|
4343
LL | let cell_absolute = ::std::cell::Cell::new(8u32);
4444
| ^^^ help: consider importing the item from `core`: `core`
4545

4646
error: used import from `std` instead of `core`
47-
--> $DIR/std_instead_of_core.rs:39:9
47+
--> $DIR/std_instead_of_core.rs:43:9
4848
|
4949
LL | use std::iter::Iterator;
5050
| ^^^ help: consider importing the item from `core`: `core`
5151

5252
error: used import from `std` instead of `alloc`
53-
--> $DIR/std_instead_of_core.rs:46:9
53+
--> $DIR/std_instead_of_core.rs:50:9
5454
|
5555
LL | use std::vec;
5656
| ^^^ help: consider importing the item from `alloc`: `alloc`
@@ -59,13 +59,13 @@ LL | use std::vec;
5959
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_alloc)]`
6060

6161
error: used import from `std` instead of `alloc`
62-
--> $DIR/std_instead_of_core.rs:48:9
62+
--> $DIR/std_instead_of_core.rs:52:9
6363
|
6464
LL | use std::vec::Vec;
6565
| ^^^ help: consider importing the item from `alloc`: `alloc`
6666

6767
error: used import from `alloc` instead of `core`
68-
--> $DIR/std_instead_of_core.rs:54:9
68+
--> $DIR/std_instead_of_core.rs:58:9
6969
|
7070
LL | use alloc::slice::from_ref;
7171
| ^^^^^ help: consider importing the item from `core`: `core`

0 commit comments

Comments
 (0)