Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions clippy_lints/src/std_instead_of_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,21 @@ fn get_first_segment<'tcx>(path: &Path<'tcx>) -> Option<&'tcx PathSegment<'tcx>>
/// Does not catch individually moved items
fn is_stable(cx: &LateContext<'_>, mut def_id: DefId, msrv: Msrv) -> bool {
loop {
if let Some(stability) = cx.tcx.lookup_stability(def_id)
&& let StabilityLevel::Stable {
since,
allowed_through_unstable_modules: None,
} = stability.level
{
let stable = match since {
StableSince::Version(v) => msrv.meets(cx, v),
StableSince::Current => msrv.current(cx).is_none(),
StableSince::Err(_) => false,
};

if !stable {
return false;
if let Some(stability) = cx.tcx.lookup_stability(def_id) {
match stability.level {
// Workaround for items from `core::intrinsics` with a stable export in a different module.
// Not that we ignore the `since` field as we are already accessing the item in question.
StabilityLevel::Stable {
allowed_through_unstable_modules: Some(_),
..
} => return true,
StabilityLevel::Stable { since, .. } => match since {
StableSince::Version(v) if !msrv.meets(cx, v) => return false,
StableSince::Current if msrv.current(cx).is_none() => return false,
StableSince::Err(_) => return false,
StableSince::Version(_) | StableSince::Current => {},
},
StabilityLevel::Unstable { .. } => return false,
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/ui/std_instead_of_core.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,23 @@ fn issue15579() {

let layout = alloc::Layout::new::<u8>();
}

#[warn(clippy::std_instead_of_core)]
fn issue13158_core_io() {
// items moved from std::io into core::io are stable in an unstable module.
use std::io::ErrorKind;
}

#[clippy::msrv = "1.40"]
fn issue13158_msrv_1_40(_: &dyn std::panic::UnwindSafe) {}

#[clippy::msrv = "1.41"]
fn issue13158_msrv_1_41(_: &dyn core::panic::UnwindSafe) {}
//~^ std_instead_of_core

#[clippy::msrv = "1.80"]
fn issue13158_msrv_1_80(_: &dyn std::error::Error) {}

#[clippy::msrv = "1.81"]
fn issue13158_msrv_1_81(_: &dyn core::error::Error) {}
//~^ std_instead_of_core
20 changes: 20 additions & 0 deletions tests/ui/std_instead_of_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,23 @@ fn issue15579() {

let layout = alloc::Layout::new::<u8>();
}

#[warn(clippy::std_instead_of_core)]
fn issue13158_core_io() {
// items moved from std::io into core::io are stable in an unstable module.
use std::io::ErrorKind;
}

#[clippy::msrv = "1.40"]
fn issue13158_msrv_1_40(_: &dyn std::panic::UnwindSafe) {}

#[clippy::msrv = "1.41"]
fn issue13158_msrv_1_41(_: &dyn std::panic::UnwindSafe) {}
//~^ std_instead_of_core

#[clippy::msrv = "1.80"]
fn issue13158_msrv_1_80(_: &dyn std::error::Error) {}

#[clippy::msrv = "1.81"]
fn issue13158_msrv_1_81(_: &dyn std::error::Error) {}
//~^ std_instead_of_core
14 changes: 13 additions & 1 deletion tests/ui/std_instead_of_core.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,17 @@ error: used import from `std` instead of `core`
LL | fn msrv_1_77(_: std::net::IpAddr) {}
| ^^^ help: consider importing the item from `core`: `core`

error: aborting due to 15 previous errors
error: used import from `std` instead of `core`
--> tests/ui/std_instead_of_core.rs:110:33
|
LL | fn issue13158_msrv_1_41(_: &dyn std::panic::UnwindSafe) {}
| ^^^ help: consider importing the item from `core`: `core`

error: used import from `std` instead of `core`
--> tests/ui/std_instead_of_core.rs:117:33
|
LL | fn issue13158_msrv_1_81(_: &dyn std::error::Error) {}
| ^^^ help: consider importing the item from `core`: `core`

error: aborting due to 17 previous errors

11 changes: 11 additions & 0 deletions tests/ui/std_instead_of_core_unfixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ fn issue15143() {
//~^ std_instead_of_core
//~| std_instead_of_alloc
}

#[rustfmt::skip]
fn pr16964() {
use std::{
borrow::Cow,
//~^ std_instead_of_alloc
collections::BTreeSet,
//~^ std_instead_of_alloc
ffi::OsString,
};
}
18 changes: 17 additions & 1 deletion tests/ui/std_instead_of_core_unfixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,21 @@ LL | use std::{error::Error, vec::Vec, fs::File};
= note: `-D clippy::std-instead-of-alloc` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_alloc)]`

error: aborting due to 3 previous errors
error: used import from `std` instead of `alloc`
--> tests/ui/std_instead_of_core_unfixable.rs:21:17
|
LL | borrow::Cow,
| ^^^
|
= help: consider importing the item from `alloc`

error: used import from `std` instead of `alloc`
--> tests/ui/std_instead_of_core_unfixable.rs:23:22
|
LL | collections::BTreeSet,
| ^^^^^^^^
|
= help: consider importing the item from `alloc`

error: aborting due to 5 previous errors

Loading