Auto merge of #10948 - Centri3:borrow_as_ptr, r=dswij

[`borrow_as_ptr`]: Ignore temporaries

Fixes #9884

changelog: [`borrow_as_ptr`]: Ignore temporaries
This commit is contained in:
bors 2023-06-21 17:07:52 +00:00
commit b3fd7b8120
4 changed files with 30 additions and 2 deletions

View File

@ -4,6 +4,7 @@
use rustc_errors::Applicability;
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
use rustc_lint::LateContext;
use rustc_middle::ty::adjustment::Adjust;
use super::BORROW_AS_PTR;
@ -23,6 +24,15 @@ pub(super) fn check<'tcx>(
};
let mut app = Applicability::MachineApplicable;
let snip = snippet_with_context(cx, e.span, cast_expr.span.ctxt(), "..", &mut app).0;
// Fix #9884
if !e.is_place_expr(|base| {
cx.typeck_results()
.adjustments()
.get(base.hir_id)
.is_some_and(|x| x.iter().any(|adj| matches!(adj.kind, Adjust::Deref(_))))
}) {
return;
}
span_lint_and_sugg(
cx,

View File

@ -1,9 +1,18 @@
//@run-rustfix
#![warn(clippy::borrow_as_ptr)]
#![allow(clippy::useless_vec)]
fn a() -> i32 {
0
}
fn main() {
let val = 1;
let _p = std::ptr::addr_of!(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);

View File

@ -1,9 +1,18 @@
//@run-rustfix
#![warn(clippy::borrow_as_ptr)]
#![allow(clippy::useless_vec)]
fn a() -> i32 {
0
}
fn main() {
let val = 1;
let _p = &val as *const i32;
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 = &mut val_mut as *mut i32;

View File

@ -1,5 +1,5 @@
error: borrow as raw pointer
--> $DIR/borrow_as_ptr.rs:6:14
--> $DIR/borrow_as_ptr.rs:11:14
|
LL | let _p = &val as *const i32;
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of!(val)`
@ -7,7 +7,7 @@ LL | let _p = &val as *const i32;
= note: `-D clippy::borrow-as-ptr` implied by `-D warnings`
error: borrow as raw pointer
--> $DIR/borrow_as_ptr.rs:9:18
--> $DIR/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)`