Skip to content

Commit efb5560

Browse files
committed
fix: [std_instead_of_core] false positive for core::io
Using suggestion proposed in: #16964 (comment)
1 parent 6310134 commit efb5560

6 files changed

Lines changed: 96 additions & 16 deletions

clippy_lints/src/std_instead_of_core.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,21 @@ fn get_first_segment<'tcx>(path: &Path<'tcx>) -> Option<&'tcx PathSegment<'tcx>>
238238
/// Does not catch individually moved items
239239
fn is_stable(cx: &LateContext<'_>, mut def_id: DefId, msrv: Msrv) -> bool {
240240
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-
};
252-
253-
if !stable {
254-
return false;
241+
if let Some(stability) = cx.tcx.lookup_stability(def_id) {
242+
match stability.level {
243+
// Workaround for items from `core::intrinsics` with a stable export in a different module.
244+
// Not that we ignore the `since` field as we are already accessing the item in question.
245+
StabilityLevel::Stable {
246+
allowed_through_unstable_modules: Some(_),
247+
..
248+
} => return true,
249+
StabilityLevel::Stable { since, .. } => match since {
250+
StableSince::Version(v) if !msrv.meets(cx, v) => return false,
251+
StableSince::Current if msrv.current(cx).is_none() => return false,
252+
StableSince::Err(_) => return false,
253+
StableSince::Version(_) | StableSince::Current => {},
254+
},
255+
StabilityLevel::Unstable { .. } => return false,
255256
}
256257
}
257258

tests/ui/std_instead_of_core.fixed

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

9797
let layout = alloc::Layout::new::<u8>();
9898
}
99+
100+
#[warn(clippy::std_instead_of_core)]
101+
fn issue13158_core_io() {
102+
// items moved from std::io into core::io are stable in an unstable module.
103+
use std::io::ErrorKind;
104+
}
105+
106+
#[clippy::msrv = "1.40"]
107+
fn issue13158_msrv_1_40(_: &dyn std::panic::UnwindSafe) {}
108+
109+
#[clippy::msrv = "1.41"]
110+
fn issue13158_msrv_1_41(_: &dyn core::panic::UnwindSafe) {}
111+
//~^ std_instead_of_core
112+
113+
#[clippy::msrv = "1.80"]
114+
fn issue13158_msrv_1_80(_: &dyn std::error::Error) {}
115+
116+
#[clippy::msrv = "1.81"]
117+
fn issue13158_msrv_1_81(_: &dyn core::error::Error) {}
118+
//~^ std_instead_of_core

tests/ui/std_instead_of_core.rs

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

9797
let layout = alloc::Layout::new::<u8>();
9898
}
99+
100+
#[warn(clippy::std_instead_of_core)]
101+
fn issue13158_core_io() {
102+
// items moved from std::io into core::io are stable in an unstable module.
103+
use std::io::ErrorKind;
104+
}
105+
106+
#[clippy::msrv = "1.40"]
107+
fn issue13158_msrv_1_40(_: &dyn std::panic::UnwindSafe) {}
108+
109+
#[clippy::msrv = "1.41"]
110+
fn issue13158_msrv_1_41(_: &dyn std::panic::UnwindSafe) {}
111+
//~^ std_instead_of_core
112+
113+
#[clippy::msrv = "1.80"]
114+
fn issue13158_msrv_1_80(_: &dyn std::error::Error) {}
115+
116+
#[clippy::msrv = "1.81"]
117+
fn issue13158_msrv_1_81(_: &dyn std::error::Error) {}
118+
//~^ std_instead_of_core

tests/ui/std_instead_of_core.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,17 @@ 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 `core`
101+
--> tests/ui/std_instead_of_core.rs:110:33
102+
|
103+
LL | fn issue13158_msrv_1_41(_: &dyn std::panic::UnwindSafe) {}
104+
| ^^^ help: consider importing the item from `core`: `core`
105+
106+
error: used import from `std` instead of `core`
107+
--> tests/ui/std_instead_of_core.rs:117:33
108+
|
109+
LL | fn issue13158_msrv_1_81(_: &dyn std::error::Error) {}
110+
| ^^^ help: consider importing the item from `core`: `core`
111+
112+
error: aborting due to 17 previous errors
101113

tests/ui/std_instead_of_core_unfixable.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@ fn issue15143() {
1414
//~^ std_instead_of_core
1515
//~| std_instead_of_alloc
1616
}
17+
18+
#[rustfmt::skip]
19+
fn pr16964() {
20+
use std::{
21+
borrow::Cow,
22+
//~^ std_instead_of_alloc
23+
collections::BTreeSet,
24+
//~^ std_instead_of_alloc
25+
ffi::OsString,
26+
};
27+
}

tests/ui/std_instead_of_core_unfixable.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,21 @@ LL | use std::{error::Error, vec::Vec, fs::File};
2626
= note: `-D clippy::std-instead-of-alloc` implied by `-D warnings`
2727
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_alloc)]`
2828

29-
error: aborting due to 3 previous errors
29+
error: used import from `std` instead of `alloc`
30+
--> tests/ui/std_instead_of_core_unfixable.rs:21:17
31+
|
32+
LL | borrow::Cow,
33+
| ^^^
34+
|
35+
= help: consider importing the item from `alloc`
36+
37+
error: used import from `std` instead of `alloc`
38+
--> tests/ui/std_instead_of_core_unfixable.rs:23:22
39+
|
40+
LL | collections::BTreeSet,
41+
| ^^^^^^^^
42+
|
43+
= help: consider importing the item from `alloc`
44+
45+
error: aborting due to 5 previous errors
3046

0 commit comments

Comments
 (0)