Skip to content

Commit 21f5954

Browse files
committed
Move BorrowAsPtr into Casts lint pass
1 parent 8ab2f88 commit 21f5954

File tree

6 files changed

+79
-104
lines changed

6 files changed

+79
-104
lines changed

clippy_lints/src/borrow_as_ptr.rs

Lines changed: 0 additions & 99 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_no_std_crate;
3+
use clippy_utils::source::snippet_with_context;
4+
use rustc_errors::Applicability;
5+
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
6+
use rustc_lint::LateContext;
7+
8+
use super::BORROW_AS_PTR;
9+
10+
pub(super) fn check<'tcx>(
11+
cx: &LateContext<'tcx>,
12+
expr: &'tcx Expr<'_>,
13+
cast_expr: &'tcx Expr<'_>,
14+
cast_to: &'tcx Ty<'_>,
15+
) {
16+
if matches!(cast_to.kind, TyKind::Ptr(_))
17+
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
18+
{
19+
let core_or_std = if is_no_std_crate(cx) { "core" } else { "std" };
20+
let macro_name = match mutability {
21+
Mutability::Not => "addr_of",
22+
Mutability::Mut => "addr_of_mut",
23+
};
24+
let mut app = Applicability::MachineApplicable;
25+
let snip = snippet_with_context(cx, e.span, cast_expr.span.ctxt(), "..", &mut app).0;
26+
27+
span_lint_and_sugg(
28+
cx,
29+
BORROW_AS_PTR,
30+
expr.span,
31+
"borrow as raw pointer",
32+
"try",
33+
format!("{}::ptr::{}!({})", core_or_std, macro_name, snip),
34+
Applicability::MachineApplicable,
35+
);
36+
}
37+
}

clippy_lints/src/casts/mod.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod as_underscore;
2+
mod borrow_as_ptr;
23
mod cast_abs_to_unsigned;
34
mod cast_enum_constructor;
45
mod cast_lossless;
@@ -17,7 +18,7 @@ mod ptr_as_ptr;
1718
mod unnecessary_cast;
1819
mod utils;
1920

20-
use clippy_utils::is_hir_ty_cfg_dependant;
21+
use clippy_utils::{is_hir_ty_cfg_dependant, meets_msrv, msrvs};
2122
use rustc_hir::{Expr, ExprKind};
2223
use rustc_lint::{LateContext, LateLintPass, LintContext};
2324
use rustc_middle::lint::in_external_macro;
@@ -535,6 +536,39 @@ declare_clippy_lint! {
535536
"detects `as _` conversion"
536537
}
537538

539+
declare_clippy_lint! {
540+
/// ### What it does
541+
/// Checks for the usage of `&expr as *const T` or
542+
/// `&mut expr as *mut T`, and suggest using `ptr::addr_of` or
543+
/// `ptr::addr_of_mut` instead.
544+
///
545+
/// ### Why is this bad?
546+
/// This would improve readability and avoid creating a reference
547+
/// that points to an uninitialized value or unaligned place.
548+
/// Read the `ptr::addr_of` docs for more information.
549+
///
550+
/// ### Example
551+
/// ```rust
552+
/// let val = 1;
553+
/// let p = &val as *const i32;
554+
///
555+
/// let mut val_mut = 1;
556+
/// let p_mut = &mut val_mut as *mut i32;
557+
/// ```
558+
/// Use instead:
559+
/// ```rust
560+
/// let val = 1;
561+
/// let p = std::ptr::addr_of!(val);
562+
///
563+
/// let mut val_mut = 1;
564+
/// let p_mut = std::ptr::addr_of_mut!(val_mut);
565+
/// ```
566+
#[clippy::version = "1.60.0"]
567+
pub BORROW_AS_PTR,
568+
pedantic,
569+
"borrowing just to cast to a raw pointer"
570+
}
571+
538572
pub struct Casts {
539573
msrv: Option<RustcVersion>,
540574
}
@@ -565,6 +599,7 @@ impl_lint_pass!(Casts => [
565599
CAST_ENUM_CONSTRUCTOR,
566600
CAST_ABS_TO_UNSIGNED,
567601
AS_UNDERSCORE,
602+
BORROW_AS_PTR,
568603
]);
569604

570605
impl<'tcx> LateLintPass<'tcx> for Casts {
@@ -607,6 +642,10 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
607642
}
608643

609644
as_underscore::check(cx, expr, cast_to_hir);
645+
646+
if meets_msrv(self.msrv, msrvs::BORROW_AS_PTR) {
647+
borrow_as_ptr::check(cx, expr, cast_expr, cast_to_hir);
648+
}
610649
}
611650

612651
cast_ref_to_mut::check(cx, expr);

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ store.register_lints(&[
5858
bool_assert_comparison::BOOL_ASSERT_COMPARISON,
5959
booleans::NONMINIMAL_BOOL,
6060
booleans::OVERLY_COMPLEX_BOOL_EXPR,
61-
borrow_as_ptr::BORROW_AS_PTR,
6261
borrow_deref_ref::BORROW_DEREF_REF,
6362
bytecount::NAIVE_BYTECOUNT,
6463
bytes_count_to_len::BYTES_COUNT_TO_LEN,
@@ -69,6 +68,7 @@ store.register_lints(&[
6968
cargo::WILDCARD_DEPENDENCIES,
7069
case_sensitive_file_extension_comparisons::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
7170
casts::AS_UNDERSCORE,
71+
casts::BORROW_AS_PTR,
7272
casts::CAST_ABS_TO_UNSIGNED,
7373
casts::CAST_ENUM_CONSTRUCTOR,
7474
casts::CAST_ENUM_TRUNCATION,

clippy_lints/src/lib.register_pedantic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
66
LintId::of(attrs::INLINE_ALWAYS),
7-
LintId::of(borrow_as_ptr::BORROW_AS_PTR),
87
LintId::of(bytecount::NAIVE_BYTECOUNT),
98
LintId::of(case_sensitive_file_extension_comparisons::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS),
9+
LintId::of(casts::BORROW_AS_PTR),
1010
LintId::of(casts::CAST_LOSSLESS),
1111
LintId::of(casts::CAST_POSSIBLE_TRUNCATION),
1212
LintId::of(casts::CAST_POSSIBLE_WRAP),

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ mod await_holding_invalid;
179179
mod blocks_in_if_conditions;
180180
mod bool_assert_comparison;
181181
mod booleans;
182-
mod borrow_as_ptr;
183182
mod borrow_deref_ref;
184183
mod bytecount;
185184
mod bytes_count_to_len;
@@ -893,7 +892,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
893892
store.register_late_pass(|| Box::new(return_self_not_must_use::ReturnSelfNotMustUse));
894893
store.register_late_pass(|| Box::new(init_numbered_fields::NumberedFields));
895894
store.register_early_pass(|| Box::new(single_char_lifetime_names::SingleCharLifetimeNames));
896-
store.register_late_pass(move || Box::new(borrow_as_ptr::BorrowAsPtr::new(msrv)));
897895
store.register_late_pass(move || Box::new(manual_bits::ManualBits::new(msrv)));
898896
store.register_late_pass(|| Box::new(default_union_representation::DefaultUnionRepresentation));
899897
store.register_early_pass(|| Box::new(doc_link_with_quotes::DocLinkWithQuotes));

0 commit comments

Comments
 (0)