mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-22 12:43:18 +00:00
update borrow_as_ptr
to suggest &raw
syntax
This commit is contained in:
parent
b829d53c19
commit
39a1dfc8a4
6 changed files with 17 additions and 19 deletions
|
@ -1,6 +1,5 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::source::snippet_with_context;
|
||||
use clippy_utils::std_or_core;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
|
||||
use rustc_lint::LateContext;
|
||||
|
@ -16,11 +15,10 @@ pub(super) fn check<'tcx>(
|
|||
) {
|
||||
if matches!(cast_to.kind, TyKind::Ptr(_))
|
||||
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
|
||||
&& let Some(std_or_core) = std_or_core(cx)
|
||||
{
|
||||
let macro_name = match mutability {
|
||||
Mutability::Not => "addr_of",
|
||||
Mutability::Mut => "addr_of_mut",
|
||||
let operator_kind = match mutability {
|
||||
Mutability::Not => "const",
|
||||
Mutability::Mut => "mut",
|
||||
};
|
||||
let mut app = Applicability::MachineApplicable;
|
||||
let snip = snippet_with_context(cx, e.span, cast_expr.span.ctxt(), "..", &mut app).0;
|
||||
|
@ -40,7 +38,7 @@ pub(super) fn check<'tcx>(
|
|||
expr.span,
|
||||
"borrow as raw pointer",
|
||||
"try",
|
||||
format!("{std_or_core}::ptr::{macro_name}!({snip})"),
|
||||
format!("&raw {operator_kind} {snip}"),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -574,13 +574,13 @@ declare_clippy_lint! {
|
|||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
/// Checks for the usage of `&expr as *const T` or
|
||||
/// `&mut expr as *mut T`, and suggest using `ptr::addr_of` or
|
||||
/// `ptr::addr_of_mut` instead.
|
||||
/// `&mut expr as *mut T`, and suggest using `&raw const` or
|
||||
/// `&raw mut` instead.
|
||||
///
|
||||
/// ### Why is this bad?
|
||||
/// This would improve readability and avoid creating a reference
|
||||
/// that points to an uninitialized value or unaligned place.
|
||||
/// Read the `ptr::addr_of` docs for more information.
|
||||
/// Read the `&raw` explanation in the Reference for more information.
|
||||
///
|
||||
/// ### Example
|
||||
/// ```no_run
|
||||
|
@ -593,10 +593,10 @@ declare_clippy_lint! {
|
|||
/// Use instead:
|
||||
/// ```no_run
|
||||
/// let val = 1;
|
||||
/// let p = std::ptr::addr_of!(val);
|
||||
/// let p = &raw const val;
|
||||
///
|
||||
/// let mut val_mut = 1;
|
||||
/// let p_mut = std::ptr::addr_of_mut!(val_mut);
|
||||
/// let p_mut = &raw mut val_mut;
|
||||
/// ```
|
||||
#[clippy::version = "1.60.0"]
|
||||
pub BORROW_AS_PTR,
|
||||
|
|
|
@ -8,12 +8,12 @@ fn a() -> i32 {
|
|||
#[clippy::msrv = "1.75"]
|
||||
fn main() {
|
||||
let val = 1;
|
||||
let _p = std::ptr::addr_of!(val);
|
||||
let _p = &raw const val;
|
||||
let _p = &0 as *const i32;
|
||||
let _p = &a() as *const i32;
|
||||
let vec = vec![1];
|
||||
let _p = &vec.len() as *const usize;
|
||||
|
||||
let mut val_mut = 1;
|
||||
let _p_mut = std::ptr::addr_of_mut!(val_mut);
|
||||
let _p_mut = &raw mut val_mut;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ error: borrow as raw pointer
|
|||
--> tests/ui/borrow_as_ptr.rs:11:14
|
||||
|
|
||||
LL | let _p = &val as *const i32;
|
||||
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of!(val)`
|
||||
| ^^^^^^^^^^^^^^^^^^ help: try: `&raw const val`
|
||||
|
|
||||
= note: `-D clippy::borrow-as-ptr` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
|
||||
|
@ -11,7 +11,7 @@ error: borrow as raw pointer
|
|||
--> tests/ui/borrow_as_ptr.rs:18:18
|
||||
|
|
||||
LL | let _p_mut = &mut val_mut as *mut i32;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of_mut!(val_mut)`
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut val_mut`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
|
|
@ -6,10 +6,10 @@
|
|||
#[start]
|
||||
fn main(_argc: isize, _argv: *const *const u8) -> isize {
|
||||
let val = 1;
|
||||
let _p = core::ptr::addr_of!(val);
|
||||
let _p = &raw const val;
|
||||
|
||||
let mut val_mut = 1;
|
||||
let _p_mut = core::ptr::addr_of_mut!(val_mut);
|
||||
let _p_mut = &raw mut val_mut;
|
||||
0
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ error: borrow as raw pointer
|
|||
--> tests/ui/borrow_as_ptr_no_std.rs:9:14
|
||||
|
|
||||
LL | let _p = &val as *const i32;
|
||||
| ^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of!(val)`
|
||||
| ^^^^^^^^^^^^^^^^^^ help: try: `&raw const val`
|
||||
|
|
||||
= note: `-D clippy::borrow-as-ptr` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
|
||||
|
@ -11,7 +11,7 @@ error: borrow as raw pointer
|
|||
--> tests/ui/borrow_as_ptr_no_std.rs:12:18
|
||||
|
|
||||
LL | let _p_mut = &mut val_mut as *mut i32;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of_mut!(val_mut)`
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut val_mut`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue