Skip to content

Commit 874f851

Browse files
Use std_or_core instead of doing check by hand every time
1 parent 40a45a4 commit 874f851

File tree

6 files changed

+20
-29
lines changed

6 files changed

+20
-29
lines changed

clippy_lints/src/default_instead_of_iter_empty.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_context;
3-
use clippy_utils::{is_no_std_crate, last_path_segment};
3+
use clippy_utils::{last_path_segment, std_or_core};
44
use rustc_errors::Applicability;
55
use rustc_hir::{def, Expr, ExprKind, GenericArg, QPath, TyKind};
66
use rustc_lint::{LateContext, LateLintPass};
@@ -42,12 +42,9 @@ impl<'tcx> LateLintPass<'tcx> for DefaultIterEmpty {
4242
&& ty.span.ctxt() == ctxt
4343
{
4444
let mut applicability = Applicability::MachineApplicable;
45-
let path = if is_no_std_crate(cx) {
46-
"core::iter::empty"
47-
} else {
48-
"std::iter::empty"
49-
};
50-
let sugg = make_sugg(cx, ty_path, ctxt, &mut applicability, path);
45+
let Some(path) = std_or_core(cx) else { return };
46+
let path = format!("{path}::iter::empty");
47+
let sugg = make_sugg(cx, ty_path, ctxt, &mut applicability, &path);
5148
span_lint_and_sugg(
5249
cx,
5350
DEFAULT_INSTEAD_OF_ITER_EMPTY,

clippy_lints/src/mem_replace.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lin
33
use clippy_utils::source::{snippet, snippet_with_applicability};
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::ty::is_non_aggregate_primitive_type;
6-
use clippy_utils::{is_default_equivalent, is_no_std_crate, is_res_lang_ctor, path_res, peel_ref_operators};
6+
use clippy_utils::{is_default_equivalent, is_res_lang_ctor, path_res, peel_ref_operators, std_or_core};
77
use rustc_errors::Applicability;
88
use rustc_hir::LangItem::OptionNone;
99
use rustc_hir::{Expr, ExprKind};
@@ -123,15 +123,12 @@ fn check_replace_option_with_none(cx: &LateContext<'_>, dest: &Expr<'_>, expr_sp
123123
);
124124
}
125125

126-
fn get_top_crate(cx: &LateContext<'_>) -> &'static str {
127-
if is_no_std_crate(cx) { "core" } else { "std" }
128-
}
129-
130126
fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'_>, expr_span: Span) {
131127
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(src.hir_id)
132128
// check if replacement is mem::MaybeUninit::uninit().assume_init()
133129
&& cx.tcx.is_diagnostic_item(sym::assume_init, method_def_id)
134130
{
131+
let Some(top_crate) = std_or_core(cx) else { return };
135132
let mut applicability = Applicability::MachineApplicable;
136133
span_lint_and_sugg(
137134
cx,
@@ -140,8 +137,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
140137
"replacing with `mem::MaybeUninit::uninit().assume_init()`",
141138
"consider using",
142139
format!(
143-
"{}::ptr::read({})",
144-
get_top_crate(cx),
140+
"{top_crate}::ptr::read({})",
145141
snippet_with_applicability(cx, dest.span, "", &mut applicability)
146142
),
147143
applicability,
@@ -154,6 +150,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
154150
&& let Some(repl_def_id) = cx.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id()
155151
{
156152
if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, repl_def_id) {
153+
let Some(top_crate) = std_or_core(cx) else { return };
157154
let mut applicability = Applicability::MachineApplicable;
158155
span_lint_and_sugg(
159156
cx,
@@ -162,8 +159,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
162159
"replacing with `mem::uninitialized()`",
163160
"consider using",
164161
format!(
165-
"{}::ptr::read({})",
166-
get_top_crate(cx),
162+
"{top_crate}::ptr::read({})",
167163
snippet_with_applicability(cx, dest.span, "", &mut applicability)
168164
),
169165
applicability,
@@ -190,7 +186,7 @@ fn check_replace_with_default(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<
190186
return;
191187
}
192188
if is_default_equivalent(cx, src) && !in_external_macro(cx.tcx.sess, expr_span) {
193-
let top_crate = get_top_crate(cx);
189+
let Some(top_crate) = std_or_core(cx) else { return };
194190
span_lint_and_then(
195191
cx,
196192
MEM_REPLACE_WITH_DEFAULT,

clippy_lints/src/methods/iter_on_single_or_empty_collections.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet;
3-
use clippy_utils::{get_expr_use_or_unification_node, is_no_std_crate, is_res_lang_ctor, path_res};
3+
use clippy_utils::{get_expr_use_or_unification_node, is_res_lang_ctor, path_res, std_or_core};
44

55
use rustc_errors::Applicability;
66
use rustc_hir::LangItem::{OptionNone, OptionSome};
@@ -58,7 +58,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: &str, re
5858
return;
5959
}
6060

61-
let top_crate = if is_no_std_crate(cx) { "core" } else { "std" };
61+
let Some(top_crate) = std_or_core(cx) else { return };
6262
if let Some(i) = item {
6363
let sugg = format!(
6464
"{top_crate}::iter::once({}{})",

clippy_lints/src/operators/ptr_eq.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::is_no_std_crate;
32
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::std_or_core;
44
use rustc_errors::Applicability;
55
use rustc_hir::{BinOpKind, Expr, ExprKind};
66
use rustc_lint::LateContext;
@@ -25,7 +25,7 @@ pub(super) fn check<'tcx>(
2525
&& let Some(left_snip) = snippet_opt(cx, left_var.span)
2626
&& let Some(right_snip) = snippet_opt(cx, right_var.span)
2727
{
28-
let top_crate = if is_no_std_crate(cx) { "core" } else { "std" };
28+
let Some(top_crate) = std_or_core(cx) else { return };
2929
span_lint_and_sugg(
3030
cx,
3131
PTR_EQ,

clippy_lints/src/transmute/transmute_int_to_char.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::TRANSMUTE_INT_TO_CHAR;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::{is_no_std_crate, sugg};
3+
use clippy_utils::{std_or_core, sugg};
44
use rustc_ast as ast;
55
use rustc_errors::Applicability;
66
use rustc_hir::Expr;
@@ -25,6 +25,7 @@ pub(super) fn check<'tcx>(
2525
e.span,
2626
&format!("transmute from a `{from_ty}` to a `char`"),
2727
|diag| {
28+
let Some(top_crate) = std_or_core(cx) else { return };
2829
let arg = sugg::Sugg::hir(cx, arg, "..");
2930
let arg = if let ty::Int(_) = from_ty.kind() {
3031
arg.as_ty(ast::UintTy::U32.name_str())
@@ -34,10 +35,7 @@ pub(super) fn check<'tcx>(
3435
diag.span_suggestion(
3536
e.span,
3637
"consider using",
37-
format!(
38-
"{}::char::from_u32({arg}).unwrap()",
39-
if is_no_std_crate(cx) { "core" } else { "std" }
40-
),
38+
format!("{top_crate}::char::from_u32({arg}).unwrap()"),
4139
Applicability::Unspecified,
4240
);
4341
},

clippy_lints/src/transmute/transmute_ref_to_ref.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{TRANSMUTE_BYTES_TO_STR, TRANSMUTE_PTR_TO_PTR};
22
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::source::snippet;
4-
use clippy_utils::{is_no_std_crate, sugg};
4+
use clippy_utils::{std_or_core, sugg};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, Mutability};
77
use rustc_lint::LateContext;
@@ -25,12 +25,12 @@ pub(super) fn check<'tcx>(
2525
&& let ty::Uint(ty::UintTy::U8) = slice_ty.kind()
2626
&& from_mutbl == to_mutbl
2727
{
28+
let Some(top_crate) = std_or_core(cx) else { return true };
29+
2830
let postfix = if *from_mutbl == Mutability::Mut { "_mut" } else { "" };
2931

3032
let snippet = snippet(cx, arg.span, "..");
3133

32-
let top_crate = if is_no_std_crate(cx) { "core" } else { "std" };
33-
3434
span_lint_and_sugg(
3535
cx,
3636
TRANSMUTE_BYTES_TO_STR,

0 commit comments

Comments
 (0)