Skip to content

Commit 3064211

Browse files
committed
Auto merge of rust-lang#12406 - MarcusGrass:fix-duplicate-std-instead-of-core, r=Alexendoo
Dedup std_instead_of_core by using first segment span for uniqueness Relates to rust-lang#12379. Instead of checking that the paths have an identical span, it checks that the relevant `std` part of the path segment's span is identical. Added a multiline test, because my first implementation was worse and failed that, then I realized that you could grab the span off the first_segment `Ident`. I did find another bug that isn't addressed by this, and that exists on master as well. The path: ```Rust use std::{io::Write, fmt::Display}; ``` Will get fixed into: ```Rust use core::{io::Write, fmt::Display}; ``` Which doesn't compile since `io::Write` isn't in `core`, if any of those paths are present in `core` it'll do the replace and cause a miscompilation. Do you think I should file a separate bug for that? Since `rustfmt` default splits those up it isn't that big of a deal. Rustfmt: ```Rust // Pre use std::{io::Write, fmt::Display}; // Post use std::fmt::Display; use std::io::Write; ``` --- changelog: [`std_instead_of_core`]: Fix duplicated output on multiple imports
2 parents 28e11b3 + 3735bf9 commit 3064211

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

clippy_lints/src/std_instead_of_core.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,19 @@ 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;
113112
return;
114113
},
115114
},
116115
sym::alloc => {
117116
if cx.tcx.crate_name(def_id.krate) == sym::core {
118117
(ALLOC_INSTEAD_OF_CORE, "alloc", "core")
119118
} else {
120-
self.prev_span = path.span;
121119
return;
122120
}
123121
},
124122
_ => return,
125123
};
126-
if path.span != self.prev_span {
124+
if first_segment.ident.span != self.prev_span {
127125
span_lint_and_sugg(
128126
cx,
129127
lint,
@@ -133,7 +131,7 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
133131
replace_with.to_string(),
134132
Applicability::MachineApplicable,
135133
);
136-
self.prev_span = path.span;
134+
self.prev_span = first_segment.ident.span;
137135
}
138136
}
139137
}

tests/ui/std_instead_of_core.fixed

+9-2
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)]
@@ -18,12 +17,20 @@ fn std_instead_of_core() {
1817
use ::core::hash::Hash;
1918
//~^ ERROR: used import from `std` instead of `core`
2019
// Don't lint on `env` macro
21-
use std::env;
20+
use core::env;
2221

2322
// Multiple imports
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 as _,
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

+8-1
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 as _,
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

+24-12
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,61 @@ 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:20:9
18+
|
19+
LL | use std::env;
20+
| ^^^ help: consider importing the item from `core`: `core`
21+
22+
error: used import from `std` instead of `core`
23+
--> tests/ui/std_instead_of_core.rs:23:9
1824
|
1925
LL | use std::fmt::{Debug, Result};
2026
| ^^^ help: consider importing the item from `core`: `core`
2127

2228
error: used import from `std` instead of `core`
23-
--> tests/ui/std_instead_of_core.rs:28:15
29+
--> tests/ui/std_instead_of_core.rs:28:9
30+
|
31+
LL | use std::{
32+
| ^^^ help: consider importing the item from `core`: `core`
33+
34+
error: used import from `std` instead of `core`
35+
--> tests/ui/std_instead_of_core.rs:35:15
2436
|
2537
LL | let ptr = std::ptr::null::<u32>();
2638
| ^^^ help: consider importing the item from `core`: `core`
2739

2840
error: used import from `std` instead of `core`
29-
--> tests/ui/std_instead_of_core.rs:30:21
41+
--> tests/ui/std_instead_of_core.rs:37:21
3042
|
3143
LL | let ptr_mut = ::std::ptr::null_mut::<usize>();
3244
| ^^^ help: consider importing the item from `core`: `core`
3345

3446
error: used import from `std` instead of `core`
35-
--> tests/ui/std_instead_of_core.rs:34:16
47+
--> tests/ui/std_instead_of_core.rs:41:16
3648
|
3749
LL | let cell = std::cell::Cell::new(8u32);
3850
| ^^^ help: consider importing the item from `core`: `core`
3951

4052
error: used import from `std` instead of `core`
41-
--> tests/ui/std_instead_of_core.rs:36:27
53+
--> tests/ui/std_instead_of_core.rs:43:27
4254
|
4355
LL | let cell_absolute = ::std::cell::Cell::new(8u32);
4456
| ^^^ help: consider importing the item from `core`: `core`
4557

4658
error: used import from `std` instead of `core`
47-
--> tests/ui/std_instead_of_core.rs:45:9
59+
--> tests/ui/std_instead_of_core.rs:52:9
4860
|
4961
LL | use std::iter::Iterator;
5062
| ^^^ help: consider importing the item from `core`: `core`
5163

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

6173
error: used import from `std` instead of `alloc`
62-
--> tests/ui/std_instead_of_core.rs:54:9
74+
--> tests/ui/std_instead_of_core.rs:61:9
6375
|
6476
LL | use std::vec::Vec;
6577
| ^^^ help: consider importing the item from `alloc`: `alloc`
6678

6779
error: used import from `alloc` instead of `core`
68-
--> tests/ui/std_instead_of_core.rs:60:9
80+
--> tests/ui/std_instead_of_core.rs:67:9
6981
|
7082
LL | use alloc::slice::from_ref;
7183
| ^^^^^ help: consider importing the item from `core`: `core`
7284
|
7385
= note: `-D clippy::alloc-instead-of-core` implied by `-D warnings`
7486
= help: to override `-D warnings` add `#[allow(clippy::alloc_instead_of_core)]`
7587

76-
error: aborting due to 11 previous errors
88+
error: aborting due to 13 previous errors
7789

0 commit comments

Comments
 (0)