[borrow_as_ptr]: Ignore temporaries

This commit is contained in:
Centri3 2023-06-13 14:55:54 -05:00
parent eefc2a0ac4
commit d2725402a9
4 changed files with 27 additions and 3 deletions

View File

@ -1,6 +1,8 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use std::ops::ControlFlow;
use clippy_utils::is_no_std_crate;
use clippy_utils::source::snippet_with_context;
use clippy_utils::{diagnostics::span_lint_and_sugg, visitors::for_each_unconsumed_temporary};
use rustc_errors::Applicability;
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
use rustc_lint::LateContext;
@ -23,6 +25,10 @@ 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 for_each_unconsumed_temporary(cx, expr, |_| ControlFlow::Break(true)).is_break() {
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)`