Skip to content

Commit 169adfc

Browse files
committed
Auto merge of rust-lang#13453 - samueltardieu:push-mtrklxqnmzzn, r=dswij
Use std_or_core to determine the correct prefix This is a cleanup commit. It replaces hand-crafted tests by the a call to the `std_or_core()` utility function. changelog: none
2 parents f01dfed + 0791efa commit 169adfc

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

clippy_lints/src/casts/borrow_as_ptr.rs

+3-3
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_with_context;
3+
use clippy_utils::std_or_core;
44
use rustc_errors::Applicability;
55
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
66
use rustc_lint::LateContext;
@@ -16,8 +16,8 @@ pub(super) fn check<'tcx>(
1616
) {
1717
if matches!(cast_to.kind, TyKind::Ptr(_))
1818
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
19+
&& let Some(std_or_core) = std_or_core(cx)
1920
{
20-
let core_or_std = if is_no_std_crate(cx) { "core" } else { "std" };
2121
let macro_name = match mutability {
2222
Mutability::Not => "addr_of",
2323
Mutability::Mut => "addr_of_mut",
@@ -40,7 +40,7 @@ pub(super) fn check<'tcx>(
4040
expr.span,
4141
"borrow as raw pointer",
4242
"try",
43-
format!("{core_or_std}::ptr::{macro_name}!({snip})"),
43+
format!("{std_or_core}::ptr::{macro_name}!({snip})"),
4444
Applicability::MachineApplicable,
4545
);
4646
}

clippy_lints/src/casts/ref_as_ptr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::sugg::Sugg;
4-
use clippy_utils::{ExprUseNode, expr_use_ctxt, is_no_std_crate};
4+
use clippy_utils::{ExprUseNode, expr_use_ctxt, std_or_core};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, Mutability, Ty, TyKind};
77
use rustc_lint::LateContext;
@@ -25,8 +25,8 @@ pub(super) fn check<'tcx>(
2525
&& let use_cx = expr_use_ctxt(cx, expr)
2626
// TODO: only block the lint if `cast_expr` is a temporary
2727
&& !matches!(use_cx.use_node(cx), ExprUseNode::LetStmt(_) | ExprUseNode::ConstStatic(_))
28+
&& let Some(std_or_core) = std_or_core(cx)
2829
{
29-
let core_or_std = if is_no_std_crate(cx) { "core" } else { "std" };
3030
let fn_name = match to_mutbl {
3131
Mutability::Not => "from_ref",
3232
Mutability::Mut => "from_mut",
@@ -56,7 +56,7 @@ pub(super) fn check<'tcx>(
5656
expr.span,
5757
"reference as raw pointer",
5858
"try",
59-
format!("{core_or_std}::ptr::{fn_name}{turbofish}({cast_expr_sugg})"),
59+
format!("{std_or_core}::ptr::{fn_name}{turbofish}({cast_expr_sugg})"),
6060
app,
6161
);
6262
}

clippy_lints/src/loops/missing_spin_loop.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::MISSING_SPIN_LOOP;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::is_no_std_crate;
3+
use clippy_utils::std_or_core;
44
use rustc_errors::Applicability;
55
use rustc_hir::{Block, Expr, ExprKind};
66
use rustc_lint::LateContext;
@@ -41,19 +41,15 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, cond: &'tcx Expr<'_>, body: &'
4141
&& [sym::load, sym::compare_exchange, sym::compare_exchange_weak].contains(&method.ident.name)
4242
&& let ty::Adt(def, _args) = cx.typeck_results().expr_ty(callee).kind()
4343
&& cx.tcx.is_diagnostic_item(sym::AtomicBool, def.did())
44+
&& let Some(std_or_core) = std_or_core(cx)
4445
{
4546
span_lint_and_sugg(
4647
cx,
4748
MISSING_SPIN_LOOP,
4849
body.span,
4950
"busy-waiting loop should at least have a spin loop hint",
5051
"try",
51-
(if is_no_std_crate(cx) {
52-
"{ core::hint::spin_loop() }"
53-
} else {
54-
"{ std::hint::spin_loop() }"
55-
})
56-
.into(),
52+
format!("{{ {std_or_core}::hint::spin_loop() }}"),
5753
Applicability::MachineApplicable,
5854
);
5955
}

0 commit comments

Comments
 (0)