about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-09-27 20:11:34 +0000
committerbors <bors@rust-lang.org>2022-09-27 20:11:34 +0000
commit35b7ce5ca91add134c22c9e5ca37f2053baeb471 (patch)
treecf187d3747254f461c12b765c13408d5bce56175
parent672fb8e42e64a6b684df47d8589e0af69ef2466a (diff)
parent5b0f46a9d90ed2c7d26b2760dddee7399b379612 (diff)
downloadrust-35b7ce5ca91add134c22c9e5ca37f2053baeb471.tar.gz
rust-35b7ce5ca91add134c22c9e5ca37f2053baeb471.zip
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`
-rw-r--r--clippy_lints/src/std_instead_of_core.rs22
-rw-r--r--tests/ui/std_instead_of_core.rs6
-rw-r--r--tests/ui/std_instead_of_core.stderr16
3 files changed, 40 insertions, 4 deletions
diff --git a/clippy_lints/src/std_instead_of_core.rs b/clippy_lints/src/std_instead_of_core.rs
index ffd63cc687a..d6b336bef94 100644
--- a/clippy_lints/src/std_instead_of_core.rs
+++ b/clippy_lints/src/std_instead_of_core.rs
@@ -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,
+        }
+    }
+}
diff --git a/tests/ui/std_instead_of_core.rs b/tests/ui/std_instead_of_core.rs
index 6b27475de4c..75b114ba0ae 100644
--- a/tests/ui/std_instead_of_core.rs
+++ b/tests/ui/std_instead_of_core.rs
@@ -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)]
diff --git a/tests/ui/std_instead_of_core.stderr b/tests/ui/std_instead_of_core.stderr
index bc49dabf586..e9ead4db5f9 100644
--- a/tests/ui/std_instead_of_core.stderr
+++ b/tests/ui/std_instead_of_core.stderr
@@ -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