Don't lint unstable moves in std_instead_of_core

Such as the currently unstable `core::error`
This commit is contained in:
Alex Macleod 2022-09-27 18:21:14 +00:00
parent 257fb4b458
commit 5b0f46a9d9
3 changed files with 40 additions and 4 deletions

View file

@ -1,6 +1,8 @@
use clippy_utils::diagnostics::span_lint_and_help;
use rustc_hir::def_id::DefId;
use rustc_hir::{def::Res, HirId, Path, PathSegment};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::DefIdTree;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{sym, symbol::kw, Span};
@ -94,6 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
fn check_path(&mut self, cx: &LateContext<'tcx>, path: &Path<'tcx>, _: HirId) {
if let Res::Def(_, def_id) = path.res
&& let Some(first_segment) = get_first_segment(path)
&& is_stable(cx, def_id)
{
let (lint, msg, help) = match first_segment.ident.name {
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>>
_ => None,
}
}
/// Checks if all ancestors of `def_id` are stable, to avoid linting
/// [unstable moves](https://github.com/rust-lang/rust/pull/95956)
fn is_stable(cx: &LateContext<'_>, mut def_id: DefId) -> bool {
loop {
if cx
.tcx
.lookup_stability(def_id)
.map_or(false, |stability| stability.is_unstable())
{
return false;
}
match cx.tcx.opt_parent(def_id) {
Some(parent) => def_id = parent,
None => return true,
}
}
}

View file

@ -24,6 +24,12 @@ fn std_instead_of_core() {
let cell_absolute = ::std::cell::Cell::new(8u32);
let _ = std::env!("PATH");
// do not lint until `error_in_core` is stable
use std::error::Error;
// lint items re-exported from private modules, `core::iter::traits::iterator::Iterator`
use std::iter::Iterator;
}
#[warn(clippy::std_instead_of_alloc)]

View file

@ -63,9 +63,17 @@ LL | let cell_absolute = ::std::cell::Cell::new(8u32);
|
= help: consider importing the item from `core`
error: used import from `std` instead of `alloc`
error: used import from `std` instead of `core`
--> $DIR/std_instead_of_core.rs:32:9
|
LL | use std::iter::Iterator;
| ^^^^^^^^^^^^^^^^^^^
|
= help: consider importing the item from `core`
error: used import from `std` instead of `alloc`
--> $DIR/std_instead_of_core.rs:38:9
|
LL | use std::vec;
| ^^^^^^^^
|
@ -73,7 +81,7 @@ LL | use std::vec;
= help: consider importing the item from `alloc`
error: used import from `std` instead of `alloc`
--> $DIR/std_instead_of_core.rs:33:9
--> $DIR/std_instead_of_core.rs:39:9
|
LL | use std::vec::Vec;
| ^^^^^^^^^^^^^
@ -81,7 +89,7 @@ LL | use std::vec::Vec;
= help: consider importing the item from `alloc`
error: used import from `alloc` instead of `core`
--> $DIR/std_instead_of_core.rs:38:9
--> $DIR/std_instead_of_core.rs:44:9
|
LL | use alloc::slice::from_ref;
| ^^^^^^^^^^^^^^^^^^^^^^
@ -89,5 +97,5 @@ LL | use alloc::slice::from_ref;
= note: `-D clippy::alloc-instead-of-core` implied by `-D warnings`
= help: consider importing the item from `core`
error: aborting due to 11 previous errors
error: aborting due to 12 previous errors