Skip to content

Commit

Permalink
Auto merge of rust-lang#9545 - Alexendoo:std-instead-of-core-unstable…
Browse files Browse the repository at this point in the history
…, r=Manishearth

Don't lint unstable moves in `std_instead_of_core`

Fixes rust-lang#9515

changelog: [`std_instead_of_core`]: No longer suggests unstable modules such as `core::error`
  • Loading branch information
bors committed Sep 27, 2022
2 parents 672fb8e + 5b0f46a commit 35b7ce5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
22 changes: 22 additions & 0 deletions clippy_lints/src/std_instead_of_core.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
}
}
}
6 changes: 6 additions & 0 deletions tests/ui/std_instead_of_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
16 changes: 12 additions & 4 deletions tests/ui/std_instead_of_core.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,39 @@ 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;
| ^^^^^^^^
|
= note: `-D clippy::std-instead-of-alloc` implied by `-D warnings`
= 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;
| ^^^^^^^^^^^^^
|
= 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;
| ^^^^^^^^^^^^^^^^^^^^^^
|
= 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

0 comments on commit 35b7ce5

Please sign in to comment.