Skip to content

Fix std_instead_of_core for ListStem use #12474

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

Closed
Closed
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
336 changes: 316 additions & 20 deletions clippy_lints/src/std_instead_of_core.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
use clippy_utils::is_from_proc_macro;
use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::def_id::DefId;
use rustc_hir::{HirId, Path, PathSegment};
use rustc_hir::intravisit::FnKind;
use rustc_hir::{
Arm, Block, Body, Expr, FieldDef, FnDecl, ForeignItem, GenericParam, Generics, HirId, ImplItem, Item, ItemKind,
LetStmt, Mod, Pat, Path, PathSegment, PolyTraitRef, Stmt, TraitItem, Ty, UseKind, Variant, VariantData,
};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::impl_lint_pass;
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::kw;
use rustc_span::{sym, Span};
use std::hash::{Hash, Hasher};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -93,50 +100,339 @@ pub struct StdReexports {
// 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,
// When inside of a UseKind::ListStem the previous check
// isn't enough to ensure correct parsing
open_use: Option<OpenUseSpan>,
}

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

/// Save all members of a `UseKind::ListStem` `use std::fmt::{Debug, Result};`
/// Lint when the `ListStem` closes. However, since there's no look-ahead (that I know of).
/// Close whenever something is encountered that's outside of the container `Span`.
#[derive(Debug)]
struct OpenUseSpan {
container: Span,
// Preserve insert order on iteration, so that lints come out in 'source order'
members: FxIndexSet<UseSpanMember>,
}

#[derive(Debug, Copy, Clone)]
struct UseSpanMember {
inner: Span,
hir_id: HirId,
first_seg_ident_span: Span,
lint_data: LintData,
}

impl PartialEq for UseSpanMember {
fn eq(&self, other: &Self) -> bool {
self.inner.eq(&other.inner)
}
}

impl Eq for UseSpanMember {}

impl Hash for UseSpanMember {
fn hash<H: Hasher>(&self, state: &mut H) {
self.inner.hash(state);
}
}

#[derive(Debug, Copy, Clone)]
enum LintData {
CanReplace(ReplaceLintData),
NoReplace,
}

#[derive(Debug, Copy, Clone)]
struct ReplaceLintData {
lint: &'static crate::Lint,
used_mod: &'static str,
replace_with: &'static str,
}

impl StdReexports {
fn suggest_for_open_use_item_if_after(&mut self, cx: &LateContext<'_>, span: Span) {
if let Some(collected_use) = self.open_use.take() {
// Still contains other span, throw it back
if collected_use.container.contains(span) {
self.open_use = Some(collected_use);
return;
}
// Short circuit
if collected_use.members.is_empty() {
return;
}
let mut place_holder_unique_check: Option<(Span, ReplaceLintData)> = None;
// If true after checking all members, the lint is 'fixable'.
// Otherwise, just warn
let mut all_same_valid_lint = true;
for member in &collected_use.members {
match &member.lint_data {
LintData::CanReplace(lint_data) => {
if let Some((_span, prev_lint_data)) = place_holder_unique_check.take() {
if prev_lint_data.lint.name == lint_data.lint.name
&& prev_lint_data.used_mod == lint_data.used_mod
&& prev_lint_data.replace_with == lint_data.replace_with
{
place_holder_unique_check = Some((member.first_seg_ident_span, *lint_data));
} else {
// Will have to warn for individual entries
all_same_valid_lint = false;
break;
}
} else {
place_holder_unique_check = Some((member.first_seg_ident_span, *lint_data));
}
},
LintData::NoReplace => {
// Will have to warn for individual entries
all_same_valid_lint = false;
break;
},
}
}
// If they can all be replaced with the same thing, just lint and suggest, then
// clippy-fix works as well
if all_same_valid_lint {
if let Some((
first_segment_ident_span,
ReplaceLintData {
lint,
used_mod,
replace_with,
},
)) = place_holder_unique_check
{
span_lint_and_sugg(
cx,
lint,
first_segment_ident_span,
format!("used import from `{used_mod}` instead of `{replace_with}`"),
format!("consider importing the item from `{replace_with}`"),
replace_with.to_string(),
Applicability::MachineApplicable,
);
}
} else {
for member in collected_use.members {
if let LintData::CanReplace(ReplaceLintData {
lint,
used_mod,
replace_with,
}) = member.lint_data
{
// Just lint, don't suggest a change
span_lint_hir_and_then(
cx,
lint,
member.hir_id,
member.inner,
format!("used import from `{used_mod}` instead of `{replace_with}`"),
|diag| {
diag.help(format!("consider importing the item from `{replace_with}`"));
},
);
}
}
}
}
}
}

impl<'tcx> LateLintPass<'tcx> for StdReexports {
fn check_path(&mut self, cx: &LateContext<'tcx>, path: &Path<'tcx>, _: HirId) {
fn check_path(&mut self, cx: &LateContext<'tcx>, path: &Path<'tcx>, hir_id: HirId) {
self.suggest_for_open_use_item_if_after(cx, path.span);
if let Res::Def(_, def_id) = path.res
&& let Some(first_segment) = get_first_segment(path)
&& is_stable(cx, def_id)
&& !in_external_macro(cx.sess(), path.span)
&& !is_from_proc_macro(cx, &first_segment.ident)
{
let (lint, used_mod, replace_with) = match first_segment.ident.name {
let lint_data = match first_segment.ident.name {
sym::std => match cx.tcx.crate_name(def_id.krate) {
sym::core => (STD_INSTEAD_OF_CORE, "std", "core"),
sym::alloc => (STD_INSTEAD_OF_ALLOC, "std", "alloc"),
sym::core => LintData::CanReplace(ReplaceLintData {
lint: STD_INSTEAD_OF_CORE,
used_mod: "std",
replace_with: "core",
}),
sym::alloc => LintData::CanReplace(ReplaceLintData {
lint: STD_INSTEAD_OF_ALLOC,
used_mod: "std",
replace_with: "alloc",
}),
_ => {
self.prev_span = first_segment.ident.span;
return;
LintData::NoReplace
},
},
sym::alloc => {
if cx.tcx.crate_name(def_id.krate) == sym::core {
(ALLOC_INSTEAD_OF_CORE, "alloc", "core")
LintData::CanReplace(ReplaceLintData {
lint: ALLOC_INSTEAD_OF_CORE,
used_mod: "alloc",
replace_with: "core",
})
} else {
self.prev_span = first_segment.ident.span;
return;
LintData::NoReplace
}
},
_ => return,
};
if first_segment.ident.span != self.prev_span {
span_lint_and_sugg(
cx,
lint,
first_segment.ident.span,
format!("used import from `{used_mod}` instead of `{replace_with}`"),
format!("consider importing the item from `{replace_with}`"),
replace_with.to_string(),
Applicability::MachineApplicable,
);
self.prev_span = first_segment.ident.span;
if let Some(in_use) = self.open_use.as_mut() {
in_use.members.insert(UseSpanMember {
inner: path.span,
hir_id,
first_seg_ident_span: first_segment.ident.span,
lint_data,
});
return;
}
if let LintData::CanReplace(ReplaceLintData {
lint,
used_mod,
replace_with,
}) = lint_data
{
if first_segment.ident.span != self.prev_span {
span_lint_and_sugg(
cx,
lint,
first_segment.ident.span,
format!("used import from `{used_mod}` instead of `{replace_with}`"),
format!("consider importing the item from `{replace_with}`"),
replace_with.to_string(),
Applicability::MachineApplicable,
);
self.prev_span = first_segment.ident.span;
}
}
}
}

fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, item.span);
if matches!(item.kind, ItemKind::Use(_, UseKind::ListStem)) {
self.open_use = Some(OpenUseSpan {
container: item.span,
members: FxIndexSet::default(),
});
}
}

// Essentially, check every other parsable thing's start (except for attributes),
// If it starts, wrap up the last path lint. Lookahead would be nice but I don't
// know if that's possible.

#[inline]
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, body.value.span);
}

#[inline]
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, Span::default());
}

#[inline]
fn check_mod(&mut self, cx: &LateContext<'tcx>, _: &'tcx Mod<'tcx>, _: HirId) {
self.suggest_for_open_use_item_if_after(cx, Span::default());
}

#[inline]
fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, f_item: &'tcx ForeignItem<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, f_item.span);
}

#[inline]
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx LetStmt<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, local.span);
}

#[inline]
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, block.span);
}

#[inline]
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, stmt.span);
}

#[inline]
fn check_arm(&mut self, cx: &LateContext<'tcx>, arm: &'tcx Arm<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, arm.span);
}

#[inline]
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, pat.span);
}

#[inline]
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, expr.span);
}

#[inline]
fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, ty.span);
}

#[inline]
fn check_generic_param(&mut self, cx: &LateContext<'tcx>, gp: &'tcx GenericParam<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, gp.span);
}

#[inline]
fn check_generics(&mut self, cx: &LateContext<'tcx>, g: &'tcx Generics<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, g.span);
}

#[inline]
fn check_poly_trait_ref(&mut self, cx: &LateContext<'tcx>, ptr: &'tcx PolyTraitRef<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, ptr.span);
}

#[inline]
fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
_: FnKind<'tcx>,
_: &'tcx FnDecl<'tcx>,
_: &'tcx Body<'tcx>,
s: Span,
_: LocalDefId,
) {
self.suggest_for_open_use_item_if_after(cx, s);
}

#[inline]
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, ti: &'tcx TraitItem<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, ti.span);
}

#[inline]
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, imp: &'tcx ImplItem<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, imp.span);
}

#[inline]
fn check_struct_def(&mut self, cx: &LateContext<'tcx>, _: &'tcx VariantData<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, Span::default());
}

#[inline]
fn check_field_def(&mut self, cx: &LateContext<'tcx>, fd: &'tcx FieldDef<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, fd.span);
}

#[inline]
fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx Variant<'tcx>) {
self.suggest_for_open_use_item_if_after(cx, v.span);
}
}

/// Returns the first named segment of a [`Path`].
Expand Down
Loading