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

Make std_instead_of_core somewhat MSRV aware #13168

Merged
merged 1 commit into from
Jul 27, 2024
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
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_late_pass(move |_| Box::new(manual_retain::ManualRetain::new(conf)));
store.register_late_pass(move |_| Box::new(manual_rotate::ManualRotate));
store.register_late_pass(move |_| Box::new(operators::Operators::new(conf)));
store.register_late_pass(|_| Box::<std_instead_of_core::StdReexports>::default());
store.register_late_pass(move |_| Box::new(std_instead_of_core::StdReexports::new(conf)));
store.register_late_pass(move |_| Box::new(instant_subtraction::InstantSubtraction::new(conf)));
store.register_late_pass(|_| Box::new(partialeq_to_none::PartialeqToNone));
store.register_late_pass(move |_| Box::new(manual_clamp::ManualClamp::new(conf)));
Expand Down
53 changes: 43 additions & 10 deletions clippy_lints/src/std_instead_of_core.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use clippy_config::msrvs::Msrv;
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_from_proc_macro;
use rustc_attr::{StabilityLevel, StableSince};
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::def_id::DefId;
use rustc_hir::{HirId, Path, PathSegment};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_semver::RustcVersion;
use rustc_session::impl_lint_pass;
use rustc_span::symbol::kw;
use rustc_span::{sym, Span};
Expand Down Expand Up @@ -66,6 +70,10 @@ declare_clippy_lint! {
/// imported from core to ensure disabling `alloc` does not cause the crate to fail to compile. This lint
/// is also useful for crates migrating to become `no_std` compatible.
///
/// ### Known problems
/// The lint is only partially aware of the required MSRV for items that were originally in `std` but moved
/// to `core`.
///
/// ### Example
/// ```no_run
/// # extern crate alloc;
Expand All @@ -81,20 +89,30 @@ declare_clippy_lint! {
"type is imported from alloc when available in core"
}

#[derive(Default)]
pub struct StdReexports {
// Paths which can be either a module or a macro (e.g. `std::env`) will cause this check to happen
// twice. First for the mod, second for the macro. This is used to avoid the lint reporting for the macro
// when the path could be also be used to access the module.
prev_span: Span,
msrv: Msrv,
}

impl StdReexports {
pub fn new(conf: &'static Conf) -> Self {
Self {
prev_span: Span::default(),
msrv: conf.msrv.clone(),
}
}
}

impl_lint_pass!(StdReexports => [STD_INSTEAD_OF_CORE, STD_INSTEAD_OF_ALLOC, ALLOC_INSTEAD_OF_CORE]);

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)
&& is_stable(cx, def_id, &self.msrv)
&& !in_external_macro(cx.sess(), path.span)
&& !is_from_proc_macro(cx, &first_segment.ident)
{
Expand Down Expand Up @@ -131,6 +149,8 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
}
}
}

extract_msrv_attr!(LateContext);
}

/// Returns the first named segment of a [`Path`].
Expand All @@ -146,16 +166,29 @@ fn get_first_segment<'tcx>(path: &Path<'tcx>) -> Option<&'tcx PathSegment<'tcx>>
}
}

/// 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 {
/// Checks if all ancestors of `def_id` meet `msrv` to avoid linting [unstable moves](https://github.com/rust-lang/rust/pull/95956)
/// or now stable moves that were once unstable.
///
/// Does not catch individually moved items
fn is_stable(cx: &LateContext<'_>, mut def_id: DefId, msrv: &Msrv) -> bool {
loop {
if cx
.tcx
.lookup_stability(def_id)
.map_or(false, |stability| stability.is_unstable())
if let Some(stability) = cx.tcx.lookup_stability(def_id)
&& let StabilityLevel::Stable {
since,
allowed_through_unstable_modules: false,
} = stability.level
{
return false;
let stable = match since {
StableSince::Version(v) => {
msrv.meets(RustcVersion::new(v.major.into(), v.minor.into(), v.patch.into()))
},
StableSince::Current => msrv.current().is_none(),
StableSince::Err => false,
};

if !stable {
return false;
}
}

match cx.tcx.opt_parent(def_id) {
Expand Down
17 changes: 13 additions & 4 deletions tests/ui/std_instead_of_core.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,17 @@ mod std_in_proc_macro_derive {
struct B {}
}

fn main() {
std_instead_of_core();
std_instead_of_alloc();
alloc_instead_of_core();
// Some intrinsics are usable on stable but live in an unstable module, but should still suggest
// replacing std -> core
fn intrinsic(a: *mut u8, b: *mut u8) {
unsafe {
core::intrinsics::copy(a, b, 1);
//~^ std_instead_of_core
}
}

#[clippy::msrv = "1.76"]
fn msrv_1_76(_: std::net::IpAddr) {}

#[clippy::msrv = "1.77"]
fn msrv_1_77(_: core::net::IpAddr) {}
17 changes: 13 additions & 4 deletions tests/ui/std_instead_of_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,17 @@ mod std_in_proc_macro_derive {
struct B {}
}

fn main() {
std_instead_of_core();
std_instead_of_alloc();
alloc_instead_of_core();
// Some intrinsics are usable on stable but live in an unstable module, but should still suggest
// replacing std -> core
fn intrinsic(a: *mut u8, b: *mut u8) {
unsafe {
std::intrinsics::copy(a, b, 1);
//~^ std_instead_of_core
}
}

#[clippy::msrv = "1.76"]
fn msrv_1_76(_: std::net::IpAddr) {}

#[clippy::msrv = "1.77"]
fn msrv_1_77(_: std::net::IpAddr) {}
14 changes: 13 additions & 1 deletion tests/ui/std_instead_of_core.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,17 @@ LL | use alloc::slice::from_ref;
= note: `-D clippy::alloc-instead-of-core` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::alloc_instead_of_core)]`

error: aborting due to 13 previous errors
error: used import from `std` instead of `core`
--> tests/ui/std_instead_of_core.rs:82:9
|
LL | std::intrinsics::copy(a, b, 1);
| ^^^ help: consider importing the item from `core`: `core`

error: used import from `std` instead of `core`
--> tests/ui/std_instead_of_core.rs:91:17
|
LL | fn msrv_1_77(_: std::net::IpAddr) {}
| ^^^ help: consider importing the item from `core`: `core`

error: aborting due to 15 previous errors