Skip to content

Commit bc2d729

Browse files
committed
fix(lint): std_instead_of_core false positive for core::io
1 parent 6310134 commit bc2d729

4 files changed

Lines changed: 73 additions & 14 deletions

File tree

clippy_lints/src/std_instead_of_core.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,21 +237,33 @@ fn get_first_segment<'tcx>(path: &Path<'tcx>) -> Option<&'tcx PathSegment<'tcx>>
237237
///
238238
/// Does not catch individually moved items
239239
fn is_stable(cx: &LateContext<'_>, mut def_id: DefId, msrv: Msrv) -> bool {
240+
let mut allowed_through_unstable_modules_seen = false;
240241
loop {
241-
if let Some(stability) = cx.tcx.lookup_stability(def_id)
242-
&& let StabilityLevel::Stable {
243-
since,
244-
allowed_through_unstable_modules: None,
245-
} = stability.level
246-
{
247-
let stable = match since {
248-
StableSince::Version(v) => msrv.meets(cx, v),
249-
StableSince::Current => msrv.current(cx).is_none(),
250-
StableSince::Err(_) => false,
251-
};
242+
if let Some(stability) = cx.tcx.lookup_stability(def_id) {
243+
match stability.level {
244+
StabilityLevel::Unstable { .. } => {
245+
if !allowed_through_unstable_modules_seen {
246+
return false;
247+
}
248+
},
249+
StabilityLevel::Stable {
250+
since,
251+
allowed_through_unstable_modules,
252+
} => {
253+
let stable = match since {
254+
StableSince::Version(v) => msrv.meets(cx, v),
255+
StableSince::Current => msrv.current(cx).is_none(),
256+
StableSince::Err(_) => false,
257+
};
252258

253-
if !stable {
254-
return false;
259+
if allowed_through_unstable_modules.is_some() {
260+
allowed_through_unstable_modules_seen = true;
261+
}
262+
263+
if !stable && allowed_through_unstable_modules.is_none() {
264+
return false;
265+
}
266+
},
255267
}
256268
}
257269

tests/ui/std_instead_of_core.fixed

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,19 @@ fn issue15579() {
9696

9797
let layout = alloc::Layout::new::<u8>();
9898
}
99+
100+
#[warn(clippy::std_instead_of_core)]
101+
#[warn(clippy::std_instead_of_alloc)]
102+
fn issue13158_core_io() {
103+
// items moved from std::io into core::io are stable in an unstable module.
104+
use std::io::ErrorKind;
105+
106+
#[rustfmt::skip]
107+
use std::sync::{
108+
Arc,
109+
//~^ std_instead_of_alloc
110+
Mutex,
111+
Weak,
112+
//~^ std_instead_of_alloc
113+
};
114+
}

tests/ui/std_instead_of_core.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,19 @@ fn issue15579() {
9696

9797
let layout = alloc::Layout::new::<u8>();
9898
}
99+
100+
#[warn(clippy::std_instead_of_core)]
101+
#[warn(clippy::std_instead_of_alloc)]
102+
fn issue13158_core_io() {
103+
// items moved from std::io into core::io are stable in an unstable module.
104+
use std::io::ErrorKind;
105+
106+
#[rustfmt::skip]
107+
use std::sync::{
108+
Arc,
109+
//~^ std_instead_of_alloc
110+
Mutex,
111+
Weak,
112+
//~^ std_instead_of_alloc
113+
};
114+
}

tests/ui/std_instead_of_core.stderr

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,20 @@ error: used import from `std` instead of `core`
9797
LL | fn msrv_1_77(_: std::net::IpAddr) {}
9898
| ^^^ help: consider importing the item from `core`: `core`
9999

100-
error: aborting due to 15 previous errors
100+
error: used import from `std` instead of `alloc`
101+
--> tests/ui/std_instead_of_core.rs:108:9
102+
|
103+
LL | Arc,
104+
| ^^^
105+
|
106+
= help: consider importing the item from `alloc`
107+
108+
error: used import from `std` instead of `alloc`
109+
--> tests/ui/std_instead_of_core.rs:111:9
110+
|
111+
LL | Weak,
112+
| ^^^^
113+
|
114+
= help: consider importing the item from `alloc`
101115

116+
error: aborting due to 17 previous errors

0 commit comments

Comments
 (0)