Skip to content

Commit 751c8ff

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

3 files changed

Lines changed: 40 additions & 21 deletions

File tree

clippy_lints/src/std_instead_of_core.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -237,27 +237,34 @@ 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-
loop {
241-
if let Some(stability) = cx.tcx.lookup_stability(def_id)
242-
&& let StabilityLevel::Stable {
240+
core::iter::once(def_id)
241+
.chain(core::iter::from_fn(|| {
242+
def_id = cx.tcx.opt_parent(def_id)?;
243+
Some(def_id)
244+
}))
245+
.find_map(|def_id| match cx.tcx.lookup_stability(def_id)?.level {
246+
StabilityLevel::Unstable { .. } => Some(false),
247+
StabilityLevel::Stable {
243248
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;
255-
}
256-
}
249+
allowed_through_unstable_modules,
250+
} => {
251+
let stable = match since {
252+
StableSince::Version(v) => msrv.meets(cx, v),
253+
StableSince::Current => msrv.current(cx).is_none(),
254+
StableSince::Err(_) => false,
255+
};
257256

258-
match cx.tcx.opt_parent(def_id) {
259-
Some(parent) => def_id = parent,
260-
None => return true,
261-
}
262-
}
257+
if !stable {
258+
Some(false)
259+
} else if allowed_through_unstable_modules.is_some() {
260+
// Items marked with #[rustc_allowed_through_unstable_modules = "..."]
261+
// can be short-circuited, since no instability of a parent module
262+
// can override this attribute.
263+
Some(true)
264+
} else {
265+
None
266+
}
267+
},
268+
})
269+
.unwrap_or(true)
263270
}

tests/ui/std_instead_of_core.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@ 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+
type ErrorKindA = std::io::ErrorKind;
104+
}

tests/ui/std_instead_of_core.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@ 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+
type ErrorKindA = std::io::ErrorKind;
104+
}

0 commit comments

Comments
 (0)