Skip to content

Commit b8a64f6

Browse files
committed
update borrow_as_ptr to suggest &raw syntax
1 parent b829d53 commit b8a64f6

File tree

8 files changed

+19
-21
lines changed

8 files changed

+19
-21
lines changed

clippy_lints/src/casts/borrow_as_ptr.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_context;
3-
use clippy_utils::std_or_core;
43
use rustc_errors::Applicability;
54
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
65
use rustc_lint::LateContext;
@@ -16,11 +15,10 @@ pub(super) fn check<'tcx>(
1615
) {
1716
if matches!(cast_to.kind, TyKind::Ptr(_))
1817
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
19-
&& let Some(std_or_core) = std_or_core(cx)
2018
{
21-
let macro_name = match mutability {
22-
Mutability::Not => "addr_of",
23-
Mutability::Mut => "addr_of_mut",
19+
let operator_kind = match mutability {
20+
Mutability::Not => "const",
21+
Mutability::Mut => "mut",
2422
};
2523
let mut app = Applicability::MachineApplicable;
2624
let snip = snippet_with_context(cx, e.span, cast_expr.span.ctxt(), "..", &mut app).0;
@@ -40,7 +38,7 @@ pub(super) fn check<'tcx>(
4038
expr.span,
4139
"borrow as raw pointer",
4240
"try",
43-
format!("{std_or_core}::ptr::{macro_name}!({snip})"),
41+
format!("&raw {operator_kind} {snip}"),
4442
Applicability::MachineApplicable,
4543
);
4644
}

clippy_lints/src/casts/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,13 +574,13 @@ declare_clippy_lint! {
574574
declare_clippy_lint! {
575575
/// ### What it does
576576
/// Checks for the usage of `&expr as *const T` or
577-
/// `&mut expr as *mut T`, and suggest using `ptr::addr_of` or
578-
/// `ptr::addr_of_mut` instead.
577+
/// `&mut expr as *mut T`, and suggest using `&raw const` or
578+
/// `&raw mut` instead.
579579
///
580580
/// ### Why is this bad?
581581
/// This would improve readability and avoid creating a reference
582582
/// that points to an uninitialized value or unaligned place.
583-
/// Read the `ptr::addr_of` docs for more information.
583+
/// Read the `&raw` explanation in the Reference for more information.
584584
///
585585
/// ### Example
586586
/// ```no_run
@@ -593,10 +593,10 @@ declare_clippy_lint! {
593593
/// Use instead:
594594
/// ```no_run
595595
/// let val = 1;
596-
/// let p = std::ptr::addr_of!(val);
596+
/// let p = &raw const val;
597597
///
598598
/// let mut val_mut = 1;
599-
/// let p_mut = std::ptr::addr_of_mut!(val_mut);
599+
/// let p_mut = &raw mut val_mut;
600600
/// ```
601601
#[clippy::version = "1.60.0"]
602602
pub BORROW_AS_PTR,

tests/ui/borrow_as_ptr.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ fn a() -> i32 {
88
#[clippy::msrv = "1.75"]
99
fn main() {
1010
let val = 1;
11-
let _p = std::ptr::addr_of!(val);
11+
let _p = &raw const val;
1212
let _p = &0 as *const i32;
1313
let _p = &a() as *const i32;
1414
let vec = vec![1];
1515
let _p = &vec.len() as *const usize;
1616

1717
let mut val_mut = 1;
18-
let _p_mut = std::ptr::addr_of_mut!(val_mut);
18+
let _p_mut = &raw mut val_mut;
1919
}

tests/ui/borrow_as_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn a() -> i32 {
55
0
66
}
77

8-
#[clippy::msrv = "1.75"]
8+
#[clippy::msrv = "1.82"]
99
fn main() {
1010
let val = 1;
1111
let _p = &val as *const i32;

tests/ui/borrow_as_ptr.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: borrow as raw pointer
22
--> tests/ui/borrow_as_ptr.rs:11:14
33
|
44
LL | let _p = &val as *const i32;
5-
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of!(val)`
5+
| ^^^^^^^^^^^^^^^^^^ help: try: `&raw const val`
66
|
77
= note: `-D clippy::borrow-as-ptr` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
@@ -11,7 +11,7 @@ error: borrow as raw pointer
1111
--> tests/ui/borrow_as_ptr.rs:18:18
1212
|
1313
LL | let _p_mut = &mut val_mut as *mut i32;
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of_mut!(val_mut)`
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut val_mut`
1515

1616
error: aborting due to 2 previous errors
1717

tests/ui/borrow_as_ptr_no_std.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#[start]
77
fn main(_argc: isize, _argv: *const *const u8) -> isize {
88
let val = 1;
9-
let _p = core::ptr::addr_of!(val);
9+
let _p = &raw const val;
1010

1111
let mut val_mut = 1;
12-
let _p_mut = core::ptr::addr_of_mut!(val_mut);
12+
let _p_mut = &raw mut val_mut;
1313
0
1414
}
1515

tests/ui/borrow_as_ptr_no_std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![feature(lang_items, start, libc)]
33
#![no_std]
44

5-
#[clippy::msrv = "1.75"]
5+
#[clippy::msrv = "1.82"]
66
#[start]
77
fn main(_argc: isize, _argv: *const *const u8) -> isize {
88
let val = 1;

tests/ui/borrow_as_ptr_no_std.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: borrow as raw pointer
22
--> tests/ui/borrow_as_ptr_no_std.rs:9:14
33
|
44
LL | let _p = &val as *const i32;
5-
| ^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of!(val)`
5+
| ^^^^^^^^^^^^^^^^^^ help: try: `&raw const val`
66
|
77
= note: `-D clippy::borrow-as-ptr` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
@@ -11,7 +11,7 @@ error: borrow as raw pointer
1111
--> tests/ui/borrow_as_ptr_no_std.rs:12:18
1212
|
1313
LL | let _p_mut = &mut val_mut as *mut i32;
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of_mut!(val_mut)`
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut val_mut`
1515

1616
error: aborting due to 2 previous errors
1717

0 commit comments

Comments
 (0)