From 74cba639e9dc68b252f54db711be776a8bbe5c5e Mon Sep 17 00:00:00 2001 From: MarcusGrass Date: Sun, 3 Mar 2024 15:30:49 +0100 Subject: [PATCH 1/3] Dedup std_instead_of_core by using first segment span for uniqueness --- clippy_lints/src/std_instead_of_core.rs | 8 +++---- tests/ui/std_instead_of_core.fixed | 9 +++++++- tests/ui/std_instead_of_core.rs | 9 +++++++- tests/ui/std_instead_of_core.stderr | 30 +++++++++++++++---------- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/clippy_lints/src/std_instead_of_core.rs b/clippy_lints/src/std_instead_of_core.rs index 38fd54a0f1e0c..cf839941123d1 100644 --- a/clippy_lints/src/std_instead_of_core.rs +++ b/clippy_lints/src/std_instead_of_core.rs @@ -109,7 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports { sym::core => (STD_INSTEAD_OF_CORE, "std", "core"), sym::alloc => (STD_INSTEAD_OF_ALLOC, "std", "alloc"), _ => { - self.prev_span = path.span; + self.prev_span = first_segment.ident.span; return; }, }, @@ -117,13 +117,13 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports { if cx.tcx.crate_name(def_id.krate) == sym::core { (ALLOC_INSTEAD_OF_CORE, "alloc", "core") } else { - self.prev_span = path.span; + self.prev_span = first_segment.ident.span; return; } }, _ => return, }; - if path.span != self.prev_span { + if first_segment.ident.span != self.prev_span { span_lint_and_sugg( cx, lint, @@ -133,7 +133,7 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports { replace_with.to_string(), Applicability::MachineApplicable, ); - self.prev_span = path.span; + self.prev_span = first_segment.ident.span; } } } diff --git a/tests/ui/std_instead_of_core.fixed b/tests/ui/std_instead_of_core.fixed index 63b30620a5d62..e245452024064 100644 --- a/tests/ui/std_instead_of_core.fixed +++ b/tests/ui/std_instead_of_core.fixed @@ -1,5 +1,4 @@ //@aux-build:proc_macro_derive.rs -//@compile-flags: -Zdeduplicate-diagnostics=yes #![warn(clippy::std_instead_of_core)] #![allow(unused_imports)] @@ -24,6 +23,14 @@ fn std_instead_of_core() { use core::fmt::{Debug, Result}; //~^ ERROR: used import from `std` instead of `core` + // Multiple imports multiline + #[rustfmt::skip] + use core::{ + //~^ ERROR: used import from `std` instead of `core` + fmt::Write, + ptr::read_unaligned, + }; + // Function calls let ptr = core::ptr::null::(); //~^ ERROR: used import from `std` instead of `core` diff --git a/tests/ui/std_instead_of_core.rs b/tests/ui/std_instead_of_core.rs index 0696d3a18d84b..71bc5b7bb65bb 100644 --- a/tests/ui/std_instead_of_core.rs +++ b/tests/ui/std_instead_of_core.rs @@ -1,5 +1,4 @@ //@aux-build:proc_macro_derive.rs -//@compile-flags: -Zdeduplicate-diagnostics=yes #![warn(clippy::std_instead_of_core)] #![allow(unused_imports)] @@ -24,6 +23,14 @@ fn std_instead_of_core() { use std::fmt::{Debug, Result}; //~^ ERROR: used import from `std` instead of `core` + // Multiple imports multiline + #[rustfmt::skip] + use std::{ + //~^ ERROR: used import from `std` instead of `core` + fmt::Write, + ptr::read_unaligned, + }; + // Function calls let ptr = std::ptr::null::(); //~^ ERROR: used import from `std` instead of `core` diff --git a/tests/ui/std_instead_of_core.stderr b/tests/ui/std_instead_of_core.stderr index 97835908c72c8..8f920511cc5d0 100644 --- a/tests/ui/std_instead_of_core.stderr +++ b/tests/ui/std_instead_of_core.stderr @@ -1,5 +1,5 @@ error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:15:9 + --> tests/ui/std_instead_of_core.rs:14:9 | LL | use std::hash::Hasher; | ^^^ help: consider importing the item from `core`: `core` @@ -8,49 +8,55 @@ 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` - --> tests/ui/std_instead_of_core.rs:18:11 + --> tests/ui/std_instead_of_core.rs:17:11 | LL | use ::std::hash::Hash; | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:24:9 + --> tests/ui/std_instead_of_core.rs:23:9 | LL | use std::fmt::{Debug, Result}; | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:28:15 + --> tests/ui/std_instead_of_core.rs:28:9 + | +LL | use std::{ + | ^^^ help: consider importing the item from `core`: `core` + +error: used import from `std` instead of `core` + --> tests/ui/std_instead_of_core.rs:35:15 | LL | let ptr = std::ptr::null::(); | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:30:21 + --> tests/ui/std_instead_of_core.rs:37:21 | LL | let ptr_mut = ::std::ptr::null_mut::(); | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:34:16 + --> tests/ui/std_instead_of_core.rs:41: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` - --> tests/ui/std_instead_of_core.rs:36:27 + --> tests/ui/std_instead_of_core.rs:43: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` - --> tests/ui/std_instead_of_core.rs:45:9 + --> tests/ui/std_instead_of_core.rs:52:9 | LL | use std::iter::Iterator; | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `alloc` - --> tests/ui/std_instead_of_core.rs:52:9 + --> tests/ui/std_instead_of_core.rs:59:9 | LL | use std::vec; | ^^^ help: consider importing the item from `alloc`: `alloc` @@ -59,13 +65,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` - --> tests/ui/std_instead_of_core.rs:54:9 + --> tests/ui/std_instead_of_core.rs:61:9 | LL | use std::vec::Vec; | ^^^ help: consider importing the item from `alloc`: `alloc` error: used import from `alloc` instead of `core` - --> tests/ui/std_instead_of_core.rs:60:9 + --> tests/ui/std_instead_of_core.rs:67:9 | LL | use alloc::slice::from_ref; | ^^^^^ help: consider importing the item from `core`: `core` @@ -73,5 +79,5 @@ LL | use alloc::slice::from_ref; = note: `-D clippy::alloc-instead-of-core` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::alloc_instead_of_core)]` -error: aborting due to 11 previous errors +error: aborting due to 12 previous errors From a2e4ef294cdf48e07eb4e159534907fba7cb951a Mon Sep 17 00:00:00 2001 From: MarcusGrass Date: Sun, 3 Mar 2024 17:17:58 +0100 Subject: [PATCH 2/3] dump bugged fix --- clippy_lints/src/std_instead_of_core.rs | 2 -- tests/ui/std_instead_of_core.fixed | 8 +++++-- tests/ui/std_instead_of_core.rs | 6 ++++- tests/ui/std_instead_of_core.stderr | 30 +++++++++++++++++-------- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/std_instead_of_core.rs b/clippy_lints/src/std_instead_of_core.rs index cf839941123d1..c0e4f3c368a8d 100644 --- a/clippy_lints/src/std_instead_of_core.rs +++ b/clippy_lints/src/std_instead_of_core.rs @@ -109,7 +109,6 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports { sym::core => (STD_INSTEAD_OF_CORE, "std", "core"), sym::alloc => (STD_INSTEAD_OF_ALLOC, "std", "alloc"), _ => { - self.prev_span = first_segment.ident.span; return; }, }, @@ -117,7 +116,6 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports { if cx.tcx.crate_name(def_id.krate) == sym::core { (ALLOC_INSTEAD_OF_CORE, "alloc", "core") } else { - self.prev_span = first_segment.ident.span; return; } }, diff --git a/tests/ui/std_instead_of_core.fixed b/tests/ui/std_instead_of_core.fixed index e245452024064..db92f22c203b6 100644 --- a/tests/ui/std_instead_of_core.fixed +++ b/tests/ui/std_instead_of_core.fixed @@ -17,7 +17,7 @@ fn std_instead_of_core() { use ::core::hash::Hash; //~^ ERROR: used import from `std` instead of `core` // Don't lint on `env` macro - use std::env; + use core::env; // Multiple imports use core::fmt::{Debug, Result}; @@ -27,10 +27,14 @@ fn std_instead_of_core() { #[rustfmt::skip] use core::{ //~^ ERROR: used import from `std` instead of `core` - fmt::Write, + fmt::Write as _, ptr::read_unaligned, }; + // Multiline mixed imports + use core::{io::Write, fmt::Display}; + //~^ ERROR: used import from `std` instead of `core` + // Function calls let ptr = core::ptr::null::(); //~^ ERROR: used import from `std` instead of `core` diff --git a/tests/ui/std_instead_of_core.rs b/tests/ui/std_instead_of_core.rs index 71bc5b7bb65bb..d1281ee075813 100644 --- a/tests/ui/std_instead_of_core.rs +++ b/tests/ui/std_instead_of_core.rs @@ -27,10 +27,14 @@ fn std_instead_of_core() { #[rustfmt::skip] use std::{ //~^ ERROR: used import from `std` instead of `core` - fmt::Write, + fmt::Write as _, ptr::read_unaligned, }; + // Multiline mixed imports + use std::{io::Write, fmt::Display}; + //~^ ERROR: used import from `std` instead of `core` + // Function calls let ptr = std::ptr::null::(); //~^ ERROR: used import from `std` instead of `core` diff --git a/tests/ui/std_instead_of_core.stderr b/tests/ui/std_instead_of_core.stderr index 8f920511cc5d0..35bc6993b0155 100644 --- a/tests/ui/std_instead_of_core.stderr +++ b/tests/ui/std_instead_of_core.stderr @@ -13,6 +13,12 @@ error: used import from `std` instead of `core` LL | use ::std::hash::Hash; | ^^^ help: consider importing the item from `core`: `core` +error: used import from `std` instead of `core` + --> tests/ui/std_instead_of_core.rs:20:9 + | +LL | use std::env; + | ^^^ help: consider importing the item from `core`: `core` + error: used import from `std` instead of `core` --> tests/ui/std_instead_of_core.rs:23:9 | @@ -26,37 +32,43 @@ LL | use std::{ | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:35:15 + --> tests/ui/std_instead_of_core.rs:35:9 + | +LL | use std::{io::Write, fmt::Display}; + | ^^^ help: consider importing the item from `core`: `core` + +error: used import from `std` instead of `core` + --> tests/ui/std_instead_of_core.rs:39:15 | LL | let ptr = std::ptr::null::(); | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:37:21 + --> tests/ui/std_instead_of_core.rs:41:21 | LL | let ptr_mut = ::std::ptr::null_mut::(); | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:41:16 + --> tests/ui/std_instead_of_core.rs:45: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` - --> tests/ui/std_instead_of_core.rs:43:27 + --> tests/ui/std_instead_of_core.rs:47: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` - --> tests/ui/std_instead_of_core.rs:52:9 + --> tests/ui/std_instead_of_core.rs:56:9 | LL | use std::iter::Iterator; | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `alloc` - --> tests/ui/std_instead_of_core.rs:59:9 + --> tests/ui/std_instead_of_core.rs:63:9 | LL | use std::vec; | ^^^ help: consider importing the item from `alloc`: `alloc` @@ -65,13 +77,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` - --> tests/ui/std_instead_of_core.rs:61:9 + --> tests/ui/std_instead_of_core.rs:65:9 | LL | use std::vec::Vec; | ^^^ help: consider importing the item from `alloc`: `alloc` error: used import from `alloc` instead of `core` - --> tests/ui/std_instead_of_core.rs:67:9 + --> tests/ui/std_instead_of_core.rs:71:9 | LL | use alloc::slice::from_ref; | ^^^^^ help: consider importing the item from `core`: `core` @@ -79,5 +91,5 @@ LL | use alloc::slice::from_ref; = note: `-D clippy::alloc-instead-of-core` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::alloc_instead_of_core)]` -error: aborting due to 12 previous errors +error: aborting due to 14 previous errors From 3735bf93ff62eee7818852a1b31b7d75939c90db Mon Sep 17 00:00:00 2001 From: MarcusGrass Date: Sun, 3 Mar 2024 17:23:11 +0100 Subject: [PATCH 3/3] Working but not with mixed imports --- tests/ui/std_instead_of_core.fixed | 4 ---- tests/ui/std_instead_of_core.rs | 4 ---- tests/ui/std_instead_of_core.stderr | 24 +++++++++--------------- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/tests/ui/std_instead_of_core.fixed b/tests/ui/std_instead_of_core.fixed index db92f22c203b6..0a734a65d2976 100644 --- a/tests/ui/std_instead_of_core.fixed +++ b/tests/ui/std_instead_of_core.fixed @@ -31,10 +31,6 @@ fn std_instead_of_core() { ptr::read_unaligned, }; - // Multiline mixed imports - use core::{io::Write, fmt::Display}; - //~^ ERROR: used import from `std` instead of `core` - // Function calls let ptr = core::ptr::null::(); //~^ ERROR: used import from `std` instead of `core` diff --git a/tests/ui/std_instead_of_core.rs b/tests/ui/std_instead_of_core.rs index d1281ee075813..c12c459c7eb4b 100644 --- a/tests/ui/std_instead_of_core.rs +++ b/tests/ui/std_instead_of_core.rs @@ -31,10 +31,6 @@ fn std_instead_of_core() { ptr::read_unaligned, }; - // Multiline mixed imports - use std::{io::Write, fmt::Display}; - //~^ ERROR: used import from `std` instead of `core` - // Function calls let ptr = std::ptr::null::(); //~^ ERROR: used import from `std` instead of `core` diff --git a/tests/ui/std_instead_of_core.stderr b/tests/ui/std_instead_of_core.stderr index 35bc6993b0155..ee42b474a32e3 100644 --- a/tests/ui/std_instead_of_core.stderr +++ b/tests/ui/std_instead_of_core.stderr @@ -32,43 +32,37 @@ LL | use std::{ | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:35:9 - | -LL | use std::{io::Write, fmt::Display}; - | ^^^ help: consider importing the item from `core`: `core` - -error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:39:15 + --> tests/ui/std_instead_of_core.rs:35:15 | LL | let ptr = std::ptr::null::(); | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:41:21 + --> tests/ui/std_instead_of_core.rs:37:21 | LL | let ptr_mut = ::std::ptr::null_mut::(); | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `core` - --> tests/ui/std_instead_of_core.rs:45:16 + --> tests/ui/std_instead_of_core.rs:41: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` - --> tests/ui/std_instead_of_core.rs:47:27 + --> tests/ui/std_instead_of_core.rs:43: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` - --> tests/ui/std_instead_of_core.rs:56:9 + --> tests/ui/std_instead_of_core.rs:52:9 | LL | use std::iter::Iterator; | ^^^ help: consider importing the item from `core`: `core` error: used import from `std` instead of `alloc` - --> tests/ui/std_instead_of_core.rs:63:9 + --> tests/ui/std_instead_of_core.rs:59:9 | LL | use std::vec; | ^^^ help: consider importing the item from `alloc`: `alloc` @@ -77,13 +71,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` - --> tests/ui/std_instead_of_core.rs:65:9 + --> tests/ui/std_instead_of_core.rs:61:9 | LL | use std::vec::Vec; | ^^^ help: consider importing the item from `alloc`: `alloc` error: used import from `alloc` instead of `core` - --> tests/ui/std_instead_of_core.rs:71:9 + --> tests/ui/std_instead_of_core.rs:67:9 | LL | use alloc::slice::from_ref; | ^^^^^ help: consider importing the item from `core`: `core` @@ -91,5 +85,5 @@ LL | use alloc::slice::from_ref; = note: `-D clippy::alloc-instead-of-core` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::alloc_instead_of_core)]` -error: aborting due to 14 previous errors +error: aborting due to 13 previous errors