Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 74cba63

Browse files
committed
Dedup std_instead_of_core by using first segment span for uniqueness
1 parent 5e0afee commit 74cba63

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

clippy_lints/src/std_instead_of_core.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,21 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
109109
sym::core => (STD_INSTEAD_OF_CORE, "std", "core"),
110110
sym::alloc => (STD_INSTEAD_OF_ALLOC, "std", "alloc"),
111111
_ => {
112-
self.prev_span = path.span;
112+
self.prev_span = first_segment.ident.span;
113113
return;
114114
},
115115
},
116116
sym::alloc => {
117117
if cx.tcx.crate_name(def_id.krate) == sym::core {
118118
(ALLOC_INSTEAD_OF_CORE, "alloc", "core")
119119
} else {
120-
self.prev_span = path.span;
120+
self.prev_span = first_segment.ident.span;
121121
return;
122122
}
123123
},
124124
_ => return,
125125
};
126-
if path.span != self.prev_span {
126+
if first_segment.ident.span != self.prev_span {
127127
span_lint_and_sugg(
128128
cx,
129129
lint,
@@ -133,7 +133,7 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
133133
replace_with.to_string(),
134134
Applicability::MachineApplicable,
135135
);
136-
self.prev_span = path.span;
136+
self.prev_span = first_segment.ident.span;
137137
}
138138
}
139139
}

tests/ui/std_instead_of_core.fixed

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@aux-build:proc_macro_derive.rs
2-
//@compile-flags: -Zdeduplicate-diagnostics=yes
32

43
#![warn(clippy::std_instead_of_core)]
54
#![allow(unused_imports)]
@@ -24,6 +23,14 @@ fn std_instead_of_core() {
2423
use core::fmt::{Debug, Result};
2524
//~^ ERROR: used import from `std` instead of `core`
2625

26+
// Multiple imports multiline
27+
#[rustfmt::skip]
28+
use core::{
29+
//~^ ERROR: used import from `std` instead of `core`
30+
fmt::Write,
31+
ptr::read_unaligned,
32+
};
33+
2734
// Function calls
2835
let ptr = core::ptr::null::<u32>();
2936
//~^ ERROR: used import from `std` instead of `core`

tests/ui/std_instead_of_core.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@aux-build:proc_macro_derive.rs
2-
//@compile-flags: -Zdeduplicate-diagnostics=yes
32

43
#![warn(clippy::std_instead_of_core)]
54
#![allow(unused_imports)]
@@ -24,6 +23,14 @@ fn std_instead_of_core() {
2423
use std::fmt::{Debug, Result};
2524
//~^ ERROR: used import from `std` instead of `core`
2625

26+
// Multiple imports multiline
27+
#[rustfmt::skip]
28+
use std::{
29+
//~^ ERROR: used import from `std` instead of `core`
30+
fmt::Write,
31+
ptr::read_unaligned,
32+
};
33+
2734
// Function calls
2835
let ptr = std::ptr::null::<u32>();
2936
//~^ ERROR: used import from `std` instead of `core`

tests/ui/std_instead_of_core.stderr

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: used import from `std` instead of `core`
2-
--> tests/ui/std_instead_of_core.rs:15:9
2+
--> tests/ui/std_instead_of_core.rs:14:9
33
|
44
LL | use std::hash::Hasher;
55
| ^^^ help: consider importing the item from `core`: `core`
@@ -8,49 +8,55 @@ 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-
--> tests/ui/std_instead_of_core.rs:18:11
11+
--> tests/ui/std_instead_of_core.rs:17: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-
--> tests/ui/std_instead_of_core.rs:24:9
17+
--> tests/ui/std_instead_of_core.rs:23: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-
--> tests/ui/std_instead_of_core.rs:28:15
23+
--> tests/ui/std_instead_of_core.rs:28:9
24+
|
25+
LL | use std::{
26+
| ^^^ help: consider importing the item from `core`: `core`
27+
28+
error: used import from `std` instead of `core`
29+
--> tests/ui/std_instead_of_core.rs:35:15
2430
|
2531
LL | let ptr = std::ptr::null::<u32>();
2632
| ^^^ help: consider importing the item from `core`: `core`
2733

2834
error: used import from `std` instead of `core`
29-
--> tests/ui/std_instead_of_core.rs:30:21
35+
--> tests/ui/std_instead_of_core.rs:37:21
3036
|
3137
LL | let ptr_mut = ::std::ptr::null_mut::<usize>();
3238
| ^^^ help: consider importing the item from `core`: `core`
3339

3440
error: used import from `std` instead of `core`
35-
--> tests/ui/std_instead_of_core.rs:34:16
41+
--> tests/ui/std_instead_of_core.rs:41:16
3642
|
3743
LL | let cell = std::cell::Cell::new(8u32);
3844
| ^^^ help: consider importing the item from `core`: `core`
3945

4046
error: used import from `std` instead of `core`
41-
--> tests/ui/std_instead_of_core.rs:36:27
47+
--> tests/ui/std_instead_of_core.rs:43:27
4248
|
4349
LL | let cell_absolute = ::std::cell::Cell::new(8u32);
4450
| ^^^ help: consider importing the item from `core`: `core`
4551

4652
error: used import from `std` instead of `core`
47-
--> tests/ui/std_instead_of_core.rs:45:9
53+
--> tests/ui/std_instead_of_core.rs:52:9
4854
|
4955
LL | use std::iter::Iterator;
5056
| ^^^ help: consider importing the item from `core`: `core`
5157

5258
error: used import from `std` instead of `alloc`
53-
--> tests/ui/std_instead_of_core.rs:52:9
59+
--> tests/ui/std_instead_of_core.rs:59:9
5460
|
5561
LL | use std::vec;
5662
| ^^^ help: consider importing the item from `alloc`: `alloc`
@@ -59,19 +65,19 @@ LL | use std::vec;
5965
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_alloc)]`
6066

6167
error: used import from `std` instead of `alloc`
62-
--> tests/ui/std_instead_of_core.rs:54:9
68+
--> tests/ui/std_instead_of_core.rs:61:9
6369
|
6470
LL | use std::vec::Vec;
6571
| ^^^ help: consider importing the item from `alloc`: `alloc`
6672

6773
error: used import from `alloc` instead of `core`
68-
--> tests/ui/std_instead_of_core.rs:60:9
74+
--> tests/ui/std_instead_of_core.rs:67:9
6975
|
7076
LL | use alloc::slice::from_ref;
7177
| ^^^^^ help: consider importing the item from `core`: `core`
7278
|
7379
= note: `-D clippy::alloc-instead-of-core` implied by `-D warnings`
7480
= help: to override `-D warnings` add `#[allow(clippy::alloc_instead_of_core)]`
7581

76-
error: aborting due to 11 previous errors
82+
error: aborting due to 12 previous errors
7783

0 commit comments

Comments
 (0)