Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't lint unstable moves in std_instead_of_core #9545

Merged
merged 1 commit into from
Sep 27, 2022
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
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