Skip to content

Commit 35b7ce5

Browse files
committed
Auto merge of #9545 - Alexendoo:std-instead-of-core-unstable, r=Manishearth
Don't lint unstable moves in `std_instead_of_core` Fixes #9515 changelog: [`std_instead_of_core`]: No longer suggests unstable modules such as `core::error`
2 parents 672fb8e + 5b0f46a commit 35b7ce5

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

clippy_lints/src/std_instead_of_core.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_hir::def_id::DefId;
23
use rustc_hir::{def::Res, HirId, Path, PathSegment};
34
use rustc_lint::{LateContext, LateLintPass};
5+
use rustc_middle::ty::DefIdTree;
46
use rustc_session::{declare_tool_lint, impl_lint_pass};
57
use rustc_span::{sym, symbol::kw, Span};
68

@@ -94,6 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
9496
fn check_path(&mut self, cx: &LateContext<'tcx>, path: &Path<'tcx>, _: HirId) {
9597
if let Res::Def(_, def_id) = path.res
9698
&& let Some(first_segment) = get_first_segment(path)
99+
&& is_stable(cx, def_id)
97100
{
98101
let (lint, msg, help) = match first_segment.ident.name {
99102
sym::std => match cx.tcx.crate_name(def_id.krate) {
@@ -146,3 +149,22 @@ fn get_first_segment<'tcx>(path: &Path<'tcx>) -> Option<&'tcx PathSegment<'tcx>>
146149
_ => None,
147150
}
148151
}
152+
153+
/// Checks if all ancestors of `def_id` are stable, to avoid linting
154+
/// [unstable moves](https://github.com/rust-lang/rust/pull/95956)
155+
fn is_stable(cx: &LateContext<'_>, mut def_id: DefId) -> bool {
156+
loop {
157+
if cx
158+
.tcx
159+
.lookup_stability(def_id)
160+
.map_or(false, |stability| stability.is_unstable())
161+
{
162+
return false;
163+
}
164+
165+
match cx.tcx.opt_parent(def_id) {
166+
Some(parent) => def_id = parent,
167+
None => return true,
168+
}
169+
}
170+
}

tests/ui/std_instead_of_core.rs

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ fn std_instead_of_core() {
2424
let cell_absolute = ::std::cell::Cell::new(8u32);
2525

2626
let _ = std::env!("PATH");
27+
28+
// do not lint until `error_in_core` is stable
29+
use std::error::Error;
30+
31+
// lint items re-exported from private modules, `core::iter::traits::iterator::Iterator`
32+
use std::iter::Iterator;
2733
}
2834

2935
#[warn(clippy::std_instead_of_alloc)]

tests/ui/std_instead_of_core.stderr

+12-4
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,39 @@ LL | let cell_absolute = ::std::cell::Cell::new(8u32);
6363
|
6464
= help: consider importing the item from `core`
6565

66-
error: used import from `std` instead of `alloc`
66+
error: used import from `std` instead of `core`
6767
--> $DIR/std_instead_of_core.rs:32:9
6868
|
69+
LL | use std::iter::Iterator;
70+
| ^^^^^^^^^^^^^^^^^^^
71+
|
72+
= help: consider importing the item from `core`
73+
74+
error: used import from `std` instead of `alloc`
75+
--> $DIR/std_instead_of_core.rs:38:9
76+
|
6977
LL | use std::vec;
7078
| ^^^^^^^^
7179
|
7280
= note: `-D clippy::std-instead-of-alloc` implied by `-D warnings`
7381
= help: consider importing the item from `alloc`
7482

7583
error: used import from `std` instead of `alloc`
76-
--> $DIR/std_instead_of_core.rs:33:9
84+
--> $DIR/std_instead_of_core.rs:39:9
7785
|
7886
LL | use std::vec::Vec;
7987
| ^^^^^^^^^^^^^
8088
|
8189
= help: consider importing the item from `alloc`
8290

8391
error: used import from `alloc` instead of `core`
84-
--> $DIR/std_instead_of_core.rs:38:9
92+
--> $DIR/std_instead_of_core.rs:44:9
8593
|
8694
LL | use alloc::slice::from_ref;
8795
| ^^^^^^^^^^^^^^^^^^^^^^
8896
|
8997
= note: `-D clippy::alloc-instead-of-core` implied by `-D warnings`
9098
= help: consider importing the item from `core`
9199

92-
error: aborting due to 11 previous errors
100+
error: aborting due to 12 previous errors
93101

0 commit comments

Comments
 (0)