Skip to content

Commit bc0e43c

Browse files
committed
fix borrow_as_ptr: don't lint in proc-macros
1 parent 371b174 commit bc0e43c

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

clippy_lints/src/casts/borrow_as_ptr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::msrvs::Msrv;
33
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
44
use clippy_utils::sugg::has_enclosing_paren;
5-
use clippy_utils::{get_parent_expr, is_expr_temporary_value, is_lint_allowed, msrvs, std_or_core};
5+
use clippy_utils::{get_parent_expr, is_expr_temporary_value, is_from_proc_macro, is_lint_allowed, msrvs, std_or_core};
66
use rustc_errors::Applicability;
77
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
88
use rustc_lint::LateContext;
@@ -24,6 +24,7 @@ pub(super) fn check<'tcx>(
2424
&& !is_lint_allowed(cx, BORROW_AS_PTR, expr.hir_id)
2525
// Fix #9884
2626
&& !is_expr_temporary_value(cx, e)
27+
&& !is_from_proc_macro(cx, expr)
2728
{
2829
let mut app = Applicability::MachineApplicable;
2930
let snip = snippet_with_context(cx, e.span, cast_expr.span.ctxt(), "..", &mut app).0;

tests/ui/borrow_as_ptr.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//@aux-build:proc_macros.rs
12
#![warn(clippy::borrow_as_ptr)]
23
#![allow(clippy::useless_vec)]
34

5+
extern crate proc_macros;
6+
47
fn a() -> i32 {
58
0
69
}
@@ -53,3 +56,12 @@ fn issue_15141() {
5356
// Don't lint cast to dyn trait pointers
5457
let b = &a as *const dyn std::any::Any;
5558
}
59+
60+
fn issue15389() {
61+
proc_macros::with_span! {
62+
span
63+
let var = 0u32;
64+
// Don't lint in proc-macros
65+
let _ = &var as *const u32;
66+
};
67+
}

tests/ui/borrow_as_ptr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//@aux-build:proc_macros.rs
12
#![warn(clippy::borrow_as_ptr)]
23
#![allow(clippy::useless_vec)]
34

5+
extern crate proc_macros;
6+
47
fn a() -> i32 {
58
0
69
}
@@ -53,3 +56,12 @@ fn issue_15141() {
5356
// Don't lint cast to dyn trait pointers
5457
let b = &a as *const dyn std::any::Any;
5558
}
59+
60+
fn issue15389() {
61+
proc_macros::with_span! {
62+
span
63+
let var = 0u32;
64+
// Don't lint in proc-macros
65+
let _ = &var as *const u32;
66+
};
67+
}

tests/ui/borrow_as_ptr.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: borrow as raw pointer
2-
--> tests/ui/borrow_as_ptr.rs:11:14
2+
--> tests/ui/borrow_as_ptr.rs:14:14
33
|
44
LL | let _p = &val as *const i32;
55
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of!(val)`
@@ -8,25 +8,25 @@ LL | let _p = &val as *const i32;
88
= help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
99

1010
error: borrow as raw pointer
11-
--> tests/ui/borrow_as_ptr.rs:19:18
11+
--> tests/ui/borrow_as_ptr.rs:22:18
1212
|
1313
LL | let _p_mut = &mut val_mut as *mut i32;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of_mut!(val_mut)`
1515

1616
error: borrow as raw pointer
17-
--> tests/ui/borrow_as_ptr.rs:23:16
17+
--> tests/ui/borrow_as_ptr.rs:26:16
1818
|
1919
LL | let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
2020
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of_mut!(x[1])`
2121

2222
error: borrow as raw pointer
23-
--> tests/ui/borrow_as_ptr.rs:29:17
23+
--> tests/ui/borrow_as_ptr.rs:32:17
2424
|
2525
LL | let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
2626
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut x[1]`
2727

2828
error: implicit borrow as raw pointer
29-
--> tests/ui/borrow_as_ptr.rs:35:25
29+
--> tests/ui/borrow_as_ptr.rs:38:25
3030
|
3131
LL | let p: *const i32 = &val;
3232
| ^^^^
@@ -37,7 +37,7 @@ LL | let p: *const i32 = &raw const val;
3737
| +++++++++
3838

3939
error: implicit borrow as raw pointer
40-
--> tests/ui/borrow_as_ptr.rs:39:23
40+
--> tests/ui/borrow_as_ptr.rs:42:23
4141
|
4242
LL | let p: *mut i32 = &mut val;
4343
| ^^^^^^^^
@@ -48,7 +48,7 @@ LL | let p: *mut i32 = &raw mut val;
4848
| +++
4949

5050
error: implicit borrow as raw pointer
51-
--> tests/ui/borrow_as_ptr.rs:44:19
51+
--> tests/ui/borrow_as_ptr.rs:47:19
5252
|
5353
LL | core::ptr::eq(&val, &1);
5454
| ^^^^

0 commit comments

Comments
 (0)