fix borrow_as_ptr: don't lint in proc-macros

This commit is contained in:
Ada Alakbarova 2025-08-13 11:10:44 +02:00
parent 371b174711
commit bc0e43c0f2
No known key found for this signature in database
4 changed files with 33 additions and 8 deletions

View file

@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::msrvs::Msrv;
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::sugg::has_enclosing_paren;
use clippy_utils::{get_parent_expr, is_expr_temporary_value, is_lint_allowed, msrvs, std_or_core};
use clippy_utils::{get_parent_expr, is_expr_temporary_value, is_from_proc_macro, is_lint_allowed, msrvs, std_or_core};
use rustc_errors::Applicability;
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
use rustc_lint::LateContext;
@ -24,6 +24,7 @@ pub(super) fn check<'tcx>(
&& !is_lint_allowed(cx, BORROW_AS_PTR, expr.hir_id)
// Fix #9884
&& !is_expr_temporary_value(cx, e)
&& !is_from_proc_macro(cx, expr)
{
let mut app = Applicability::MachineApplicable;
let snip = snippet_with_context(cx, e.span, cast_expr.span.ctxt(), "..", &mut app).0;

View file

@ -1,6 +1,9 @@
//@aux-build:proc_macros.rs
#![warn(clippy::borrow_as_ptr)]
#![allow(clippy::useless_vec)]
extern crate proc_macros;
fn a() -> i32 {
0
}
@ -53,3 +56,12 @@ fn issue_15141() {
// Don't lint cast to dyn trait pointers
let b = &a as *const dyn std::any::Any;
}
fn issue15389() {
proc_macros::with_span! {
span
let var = 0u32;
// Don't lint in proc-macros
let _ = &var as *const u32;
};
}

View file

@ -1,6 +1,9 @@
//@aux-build:proc_macros.rs
#![warn(clippy::borrow_as_ptr)]
#![allow(clippy::useless_vec)]
extern crate proc_macros;
fn a() -> i32 {
0
}
@ -53,3 +56,12 @@ fn issue_15141() {
// Don't lint cast to dyn trait pointers
let b = &a as *const dyn std::any::Any;
}
fn issue15389() {
proc_macros::with_span! {
span
let var = 0u32;
// Don't lint in proc-macros
let _ = &var as *const u32;
};
}

View file

@ -1,5 +1,5 @@
error: borrow as raw pointer
--> tests/ui/borrow_as_ptr.rs:11:14
--> tests/ui/borrow_as_ptr.rs:14:14
|
LL | let _p = &val as *const i32;
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of!(val)`
@ -8,25 +8,25 @@ LL | let _p = &val as *const i32;
= help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
error: borrow as raw pointer
--> tests/ui/borrow_as_ptr.rs:19:18
--> tests/ui/borrow_as_ptr.rs:22:18
|
LL | let _p_mut = &mut val_mut as *mut i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of_mut!(val_mut)`
error: borrow as raw pointer
--> tests/ui/borrow_as_ptr.rs:23:16
--> tests/ui/borrow_as_ptr.rs:26:16
|
LL | let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of_mut!(x[1])`
error: borrow as raw pointer
--> tests/ui/borrow_as_ptr.rs:29:17
--> tests/ui/borrow_as_ptr.rs:32:17
|
LL | let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut x[1]`
error: implicit borrow as raw pointer
--> tests/ui/borrow_as_ptr.rs:35:25
--> tests/ui/borrow_as_ptr.rs:38:25
|
LL | let p: *const i32 = &val;
| ^^^^
@ -37,7 +37,7 @@ LL | let p: *const i32 = &raw const val;
| +++++++++
error: implicit borrow as raw pointer
--> tests/ui/borrow_as_ptr.rs:39:23
--> tests/ui/borrow_as_ptr.rs:42:23
|
LL | let p: *mut i32 = &mut val;
| ^^^^^^^^
@ -48,7 +48,7 @@ LL | let p: *mut i32 = &raw mut val;
| +++
error: implicit borrow as raw pointer
--> tests/ui/borrow_as_ptr.rs:44:19
--> tests/ui/borrow_as_ptr.rs:47:19
|
LL | core::ptr::eq(&val, &1);
| ^^^^