Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std_instead_of_core: avoid lint inside of proc-macro #11589

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion clippy_lints/src/std_instead_of_core.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_from_proc_macro;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::def_id::DefId;
use rustc_hir::{HirId, Path, PathSegment};
use rustc_lint::{LateContext, LateLintPass};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::symbol::kw;
use rustc_span::{sym, Span};
Expand Down Expand Up @@ -99,6 +101,8 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
if let Res::Def(_, def_id) = path.res
&& let Some(first_segment) = get_first_segment(path)
&& is_stable(cx, def_id)
&& !in_external_macro(cx.sess(), path.span)
&& !is_from_proc_macro(cx, &first_segment.ident)
{
let (lint, used_mod, replace_with) = match first_segment.ident.name {
sym::std => match cx.tcx.crate_name(def_id.krate) {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/auxiliary/proc_macro_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ pub fn derive(_: TokenStream) -> TokenStream {
output
}

#[proc_macro_derive(ImplStructWithStdDisplay)]
pub fn derive_std(_: TokenStream) -> TokenStream {
quote! {
struct A {}
impl ::std::fmt::Display for A {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "A")
}
}
}
}

#[proc_macro_derive(FieldReassignWithDefault)]
pub fn derive_foo(_input: TokenStream) -> TokenStream {
quote! {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/std_instead_of_core.fixed
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//@aux-build:proc_macro_derive.rs
#![warn(clippy::std_instead_of_core)]
#![allow(unused_imports)]

extern crate alloc;

#[macro_use]
extern crate proc_macro_derive;

#[warn(clippy::std_instead_of_core)]
fn std_instead_of_core() {
// Regular import
Expand Down Expand Up @@ -55,6 +59,13 @@ fn alloc_instead_of_core() {
//~^ ERROR: used import from `alloc` instead of `core`
}

mod std_in_proc_macro_derive {
#[warn(clippy::alloc_instead_of_core)]
#[allow(unused)]
#[derive(ImplStructWithStdDisplay)]
struct B {}
}

fn main() {
std_instead_of_core();
std_instead_of_alloc();
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/std_instead_of_core.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//@aux-build:proc_macro_derive.rs
#![warn(clippy::std_instead_of_core)]
#![allow(unused_imports)]

extern crate alloc;

#[macro_use]
extern crate proc_macro_derive;

#[warn(clippy::std_instead_of_core)]
fn std_instead_of_core() {
// Regular import
Expand Down Expand Up @@ -55,6 +59,13 @@ fn alloc_instead_of_core() {
//~^ ERROR: used import from `alloc` instead of `core`
}

mod std_in_proc_macro_derive {
#[warn(clippy::alloc_instead_of_core)]
#[allow(unused)]
#[derive(ImplStructWithStdDisplay)]
struct B {}
}

fn main() {
std_instead_of_core();
std_instead_of_alloc();
Expand Down
22 changes: 11 additions & 11 deletions tests/ui/std_instead_of_core.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: used import from `std` instead of `core`
--> $DIR/std_instead_of_core.rs:9:9
--> $DIR/std_instead_of_core.rs:13:9
|
LL | use std::hash::Hasher;
| ^^^ help: consider importing the item from `core`: `core`
Expand All @@ -8,49 +8,49 @@ LL | use std::hash::Hasher;
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_core)]`

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

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

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

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

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

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

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

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

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

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