Use std_or_core instead of doing check by hand every time

This commit is contained in:
Guillaume Gomez 2024-01-15 20:53:00 +01:00
parent 40a45a463f
commit 874f851ac5
6 changed files with 20 additions and 29 deletions

View file

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_context;
use clippy_utils::{is_no_std_crate, last_path_segment};
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,9 @@ impl<'tcx> LateLintPass<'tcx> for DefaultIterEmpty {
&& ty.span.ctxt() == ctxt
{
let mut applicability = Applicability::MachineApplicable;
let path = if is_no_std_crate(cx) {
"core::iter::empty"
} else {
"std::iter::empty"
};
let sugg = make_sugg(cx, ty_path, ctxt, &mut applicability, path);
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,

View file

@ -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_no_std_crate, 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};
@ -123,15 +123,12 @@ fn check_replace_option_with_none(cx: &LateContext<'_>, dest: &Expr<'_>, expr_sp
);
}
fn get_top_crate(cx: &LateContext<'_>) -> &'static str {
if is_no_std_crate(cx) { "core" } else { "std" }
}
fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'_>, expr_span: Span) {
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(src.hir_id)
// 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,
@ -140,8 +137,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
"replacing with `mem::MaybeUninit::uninit().assume_init()`",
"consider using",
format!(
"{}::ptr::read({})",
get_top_crate(cx),
"{top_crate}::ptr::read({})",
snippet_with_applicability(cx, dest.span, "", &mut applicability)
),
applicability,
@ -154,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,
@ -162,8 +159,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
"replacing with `mem::uninitialized()`",
"consider using",
format!(
"{}::ptr::read({})",
get_top_crate(cx),
"{top_crate}::ptr::read({})",
snippet_with_applicability(cx, dest.span, "", &mut applicability)
),
applicability,
@ -190,7 +186,7 @@ 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 top_crate = get_top_crate(cx);
let Some(top_crate) = std_or_core(cx) else { return };
span_lint_and_then(
cx,
MEM_REPLACE_WITH_DEFAULT,

View file

@ -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,7 +58,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: &str, re
return;
}
let top_crate = if is_no_std_crate(cx) { "core" } else { "std" };
let Some(top_crate) = std_or_core(cx) else { return };
if let Some(i) = item {
let sugg = format!(
"{top_crate}::iter::once({}{})",

View file

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_no_std_crate;
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;
@ -25,7 +25,7 @@ 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 top_crate = if is_no_std_crate(cx) { "core" } else { "std" };
let Some(top_crate) = std_or_core(cx) else { return };
span_lint_and_sugg(
cx,
PTR_EQ,

View file

@ -1,6 +1,6 @@
use super::TRANSMUTE_INT_TO_CHAR;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::{is_no_std_crate, 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,10 +35,7 @@ pub(super) fn check<'tcx>(
diag.span_suggestion(
e.span,
"consider using",
format!(
"{}::char::from_u32({arg}).unwrap()",
if is_no_std_crate(cx) { "core" } else { "std" }
),
format!("{top_crate}::char::from_u32({arg}).unwrap()"),
Applicability::Unspecified,
);
},

View file

@ -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::{is_no_std_crate, sugg};
use clippy_utils::{std_or_core, sugg};
use rustc_errors::Applicability;
use rustc_hir::{Expr, Mutability};
use rustc_lint::LateContext;
@ -25,12 +25,12 @@ 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, "..");
let top_crate = if is_no_std_crate(cx) { "core" } else { "std" };
span_lint_and_sugg(
cx,
TRANSMUTE_BYTES_TO_STR,