Auto merge of #12149 - GuillaumeGomez:core-std-suggestions, r=llogiq
Correctly suggest std or core path depending if this is a `no_std` crate A few lints emit suggestions using `std` paths whether or not this is a `no_std` crate, which is an issue when running `rustfix` afterwards. So in case this is an item that is defined in both `std` and `core`, we need to check if the crate is `no_std` to emit the right path. r? `@llogiq` changelog: Correctly suggest std or core path depending if this is a `no_std` crate
This commit is contained in:
commit
ecb0311fcc
29 changed files with 650 additions and 95 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::last_path_segment;
|
||||
use clippy_utils::source::snippet_with_context;
|
||||
use clippy_utils::{last_path_segment, std_or_core};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{def, Expr, ExprKind, GenericArg, QPath, TyKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
|
|
@ -42,12 +42,14 @@ impl<'tcx> LateLintPass<'tcx> for DefaultIterEmpty {
|
|||
&& ty.span.ctxt() == ctxt
|
||||
{
|
||||
let mut applicability = Applicability::MachineApplicable;
|
||||
let sugg = make_sugg(cx, ty_path, ctxt, &mut applicability);
|
||||
let Some(path) = std_or_core(cx) else { return };
|
||||
let path = format!("{path}::iter::empty");
|
||||
let sugg = make_sugg(cx, ty_path, ctxt, &mut applicability, &path);
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
DEFAULT_INSTEAD_OF_ITER_EMPTY,
|
||||
expr.span,
|
||||
"`std::iter::empty()` is the more idiomatic way",
|
||||
&format!("`{path}()` is the more idiomatic way"),
|
||||
"try",
|
||||
sugg,
|
||||
applicability,
|
||||
|
|
@ -61,6 +63,7 @@ fn make_sugg(
|
|||
ty_path: &rustc_hir::QPath<'_>,
|
||||
ctxt: SyntaxContext,
|
||||
applicability: &mut Applicability,
|
||||
path: &str,
|
||||
) -> String {
|
||||
if let Some(last) = last_path_segment(ty_path).args
|
||||
&& let Some(iter_ty) = last.args.iter().find_map(|arg| match arg {
|
||||
|
|
@ -69,10 +72,10 @@ fn make_sugg(
|
|||
})
|
||||
{
|
||||
format!(
|
||||
"std::iter::empty::<{}>()",
|
||||
"{path}::<{}>()",
|
||||
snippet_with_context(cx, iter_ty.span, ctxt, "..", applicability).0
|
||||
)
|
||||
} else {
|
||||
"std::iter::empty()".to_owned()
|
||||
format!("{path}()")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lin
|
|||
use clippy_utils::source::{snippet, snippet_with_applicability};
|
||||
use clippy_utils::sugg::Sugg;
|
||||
use clippy_utils::ty::is_non_aggregate_primitive_type;
|
||||
use clippy_utils::{is_default_equivalent, is_res_lang_ctor, path_res, peel_ref_operators};
|
||||
use clippy_utils::{is_default_equivalent, is_res_lang_ctor, path_res, peel_ref_operators, std_or_core};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::LangItem::OptionNone;
|
||||
use rustc_hir::{Expr, ExprKind};
|
||||
|
|
@ -128,6 +128,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
|
|||
// check if replacement is mem::MaybeUninit::uninit().assume_init()
|
||||
&& cx.tcx.is_diagnostic_item(sym::assume_init, method_def_id)
|
||||
{
|
||||
let Some(top_crate) = std_or_core(cx) else { return };
|
||||
let mut applicability = Applicability::MachineApplicable;
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
|
|
@ -136,7 +137,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
|
|||
"replacing with `mem::MaybeUninit::uninit().assume_init()`",
|
||||
"consider using",
|
||||
format!(
|
||||
"std::ptr::read({})",
|
||||
"{top_crate}::ptr::read({})",
|
||||
snippet_with_applicability(cx, dest.span, "", &mut applicability)
|
||||
),
|
||||
applicability,
|
||||
|
|
@ -149,6 +150,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
|
|||
&& let Some(repl_def_id) = cx.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id()
|
||||
{
|
||||
if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, repl_def_id) {
|
||||
let Some(top_crate) = std_or_core(cx) else { return };
|
||||
let mut applicability = Applicability::MachineApplicable;
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
|
|
@ -157,7 +159,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
|
|||
"replacing with `mem::uninitialized()`",
|
||||
"consider using",
|
||||
format!(
|
||||
"std::ptr::read({})",
|
||||
"{top_crate}::ptr::read({})",
|
||||
snippet_with_applicability(cx, dest.span, "", &mut applicability)
|
||||
),
|
||||
applicability,
|
||||
|
|
@ -184,14 +186,17 @@ fn check_replace_with_default(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<
|
|||
return;
|
||||
}
|
||||
if is_default_equivalent(cx, src) && !in_external_macro(cx.tcx.sess, expr_span) {
|
||||
let Some(top_crate) = std_or_core(cx) else { return };
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
MEM_REPLACE_WITH_DEFAULT,
|
||||
expr_span,
|
||||
"replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`",
|
||||
&format!(
|
||||
"replacing a value of type `T` with `T::default()` is better expressed using `{top_crate}::mem::take`"
|
||||
),
|
||||
|diag| {
|
||||
if !expr_span.from_expansion() {
|
||||
let suggestion = format!("std::mem::take({})", snippet(cx, dest.span, ""));
|
||||
let suggestion = format!("{top_crate}::mem::take({})", snippet(cx, dest.span, ""));
|
||||
|
||||
diag.span_suggestion(
|
||||
expr_span,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::source::snippet;
|
||||
use clippy_utils::{get_expr_use_or_unification_node, is_no_std_crate, is_res_lang_ctor, path_res};
|
||||
use clippy_utils::{get_expr_use_or_unification_node, is_res_lang_ctor, path_res, std_or_core};
|
||||
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
||||
|
|
@ -58,10 +58,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: &str, re
|
|||
return;
|
||||
}
|
||||
|
||||
let Some(top_crate) = std_or_core(cx) else { return };
|
||||
if let Some(i) = item {
|
||||
let sugg = format!(
|
||||
"{}::iter::once({}{})",
|
||||
if is_no_std_crate(cx) { "core" } else { "std" },
|
||||
"{top_crate}::iter::once({}{})",
|
||||
iter_type.ref_prefix(),
|
||||
snippet(cx, i.span, "...")
|
||||
);
|
||||
|
|
@ -81,11 +81,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: &str, re
|
|||
expr.span,
|
||||
&format!("`{method_name}` call on an empty collection"),
|
||||
"try",
|
||||
if is_no_std_crate(cx) {
|
||||
"core::iter::empty()".to_string()
|
||||
} else {
|
||||
"std::iter::empty()".to_string()
|
||||
},
|
||||
format!("{top_crate}::iter::empty()"),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -204,7 +204,7 @@ pub(super) fn check<'tcx>(
|
|||
cx,
|
||||
UNNECESSARY_SORT_BY,
|
||||
expr.span,
|
||||
"use Vec::sort_by_key here instead",
|
||||
"consider using `sort_by_key`",
|
||||
"try",
|
||||
format!(
|
||||
"{}.sort{}_by_key(|{}| {})",
|
||||
|
|
@ -227,7 +227,7 @@ pub(super) fn check<'tcx>(
|
|||
cx,
|
||||
UNNECESSARY_SORT_BY,
|
||||
expr.span,
|
||||
"use Vec::sort here instead",
|
||||
"consider using `sort`",
|
||||
"try",
|
||||
format!(
|
||||
"{}.sort{}()",
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::source::snippet_opt;
|
||||
use clippy_utils::std_or_core;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
||||
use rustc_lint::LateContext;
|
||||
|
||||
use super::PTR_EQ;
|
||||
|
||||
static LINT_MSG: &str = "use `std::ptr::eq` when comparing raw pointers";
|
||||
|
||||
pub(super) fn check<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
expr: &'tcx Expr<'_>,
|
||||
|
|
@ -26,13 +25,14 @@ pub(super) fn check<'tcx>(
|
|||
&& let Some(left_snip) = snippet_opt(cx, left_var.span)
|
||||
&& let Some(right_snip) = snippet_opt(cx, right_var.span)
|
||||
{
|
||||
let Some(top_crate) = std_or_core(cx) else { return };
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
PTR_EQ,
|
||||
expr.span,
|
||||
LINT_MSG,
|
||||
&format!("use `{top_crate}::ptr::eq` when comparing raw pointers"),
|
||||
"try",
|
||||
format!("std::ptr::eq({left_snip}, {right_snip})"),
|
||||
format!("{top_crate}::ptr::eq({left_snip}, {right_snip})"),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use super::TRANSMUTE_INT_TO_CHAR;
|
||||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
use clippy_utils::sugg;
|
||||
use clippy_utils::{std_or_core, sugg};
|
||||
use rustc_ast as ast;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::Expr;
|
||||
|
|
@ -25,6 +25,7 @@ pub(super) fn check<'tcx>(
|
|||
e.span,
|
||||
&format!("transmute from a `{from_ty}` to a `char`"),
|
||||
|diag| {
|
||||
let Some(top_crate) = std_or_core(cx) else { return };
|
||||
let arg = sugg::Sugg::hir(cx, arg, "..");
|
||||
let arg = if let ty::Int(_) = from_ty.kind() {
|
||||
arg.as_ty(ast::UintTy::U32.name_str())
|
||||
|
|
@ -34,7 +35,7 @@ pub(super) fn check<'tcx>(
|
|||
diag.span_suggestion(
|
||||
e.span,
|
||||
"consider using",
|
||||
format!("std::char::from_u32({arg}).unwrap()"),
|
||||
format!("{top_crate}::char::from_u32({arg}).unwrap()"),
|
||||
Applicability::Unspecified,
|
||||
);
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use super::{TRANSMUTE_BYTES_TO_STR, TRANSMUTE_PTR_TO_PTR};
|
||||
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
|
||||
use clippy_utils::source::snippet;
|
||||
use clippy_utils::sugg;
|
||||
use clippy_utils::{std_or_core, sugg};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Expr, Mutability};
|
||||
use rustc_lint::LateContext;
|
||||
|
|
@ -25,6 +25,8 @@ pub(super) fn check<'tcx>(
|
|||
&& let ty::Uint(ty::UintTy::U8) = slice_ty.kind()
|
||||
&& from_mutbl == to_mutbl
|
||||
{
|
||||
let Some(top_crate) = std_or_core(cx) else { return true };
|
||||
|
||||
let postfix = if *from_mutbl == Mutability::Mut { "_mut" } else { "" };
|
||||
|
||||
let snippet = snippet(cx, arg.span, "..");
|
||||
|
|
@ -36,9 +38,9 @@ pub(super) fn check<'tcx>(
|
|||
&format!("transmute from a `{from_ty}` to a `{to_ty}`"),
|
||||
"consider using",
|
||||
if const_context {
|
||||
format!("std::str::from_utf8_unchecked{postfix}({snippet})")
|
||||
format!("{top_crate}::str::from_utf8_unchecked{postfix}({snippet})")
|
||||
} else {
|
||||
format!("std::str::from_utf8{postfix}({snippet}).unwrap()")
|
||||
format!("{top_crate}::str::from_utf8{postfix}({snippet}).unwrap()")
|
||||
},
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue