diff options
| author | Ada Alakbarova <ada.alakbarova@proton.me> | 2025-08-13 11:10:44 +0200 |
|---|---|---|
| committer | Ada Alakbarova <ada.alakbarova@proton.me> | 2025-08-13 19:35:31 +0200 |
| commit | bc0e43c0f2f76796066e4ec8335b44c00eefbc00 (patch) | |
| tree | 87a749c6668ea29eab4017641ff43993eb2ce2c0 | |
| parent | 371b1747115d240696cef30657bd0ee99aab19d2 (diff) | |
| download | rust-bc0e43c0f2f76796066e4ec8335b44c00eefbc00.tar.gz rust-bc0e43c0f2f76796066e4ec8335b44c00eefbc00.zip | |
fix `borrow_as_ptr`: don't lint in proc-macros
| -rw-r--r-- | clippy_lints/src/casts/borrow_as_ptr.rs | 3 | ||||
| -rw-r--r-- | tests/ui/borrow_as_ptr.fixed | 12 | ||||
| -rw-r--r-- | tests/ui/borrow_as_ptr.rs | 12 | ||||
| -rw-r--r-- | tests/ui/borrow_as_ptr.stderr | 14 |
4 files changed, 33 insertions, 8 deletions
diff --git a/clippy_lints/src/casts/borrow_as_ptr.rs b/clippy_lints/src/casts/borrow_as_ptr.rs index b84ff008eb8..eb75d5576f5 100644 --- a/clippy_lints/src/casts/borrow_as_ptr.rs +++ b/clippy_lints/src/casts/borrow_as_ptr.rs @@ -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; diff --git a/tests/ui/borrow_as_ptr.fixed b/tests/ui/borrow_as_ptr.fixed index 3f6e5245b87..bfe826508f3 100644 --- a/tests/ui/borrow_as_ptr.fixed +++ b/tests/ui/borrow_as_ptr.fixed @@ -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; + }; +} diff --git a/tests/ui/borrow_as_ptr.rs b/tests/ui/borrow_as_ptr.rs index 20f4f40e001..ce248f157c6 100644 --- a/tests/ui/borrow_as_ptr.rs +++ b/tests/ui/borrow_as_ptr.rs @@ -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; + }; +} diff --git a/tests/ui/borrow_as_ptr.stderr b/tests/ui/borrow_as_ptr.stderr index b1fcce49403..b371b477a50 100644 --- a/tests/ui/borrow_as_ptr.stderr +++ b/tests/ui/borrow_as_ptr.stderr @@ -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); | ^^^^ |
