Skip to content

Commit 9563a5c

Browse files
authored
{borrow,ptr}_as_ptr: don't lint inside proc-macros (#15473)
this could arguably be 2 separate PRs, but both of these were brought up in the same issue, so.. fixes rust-lang/rust-clippy#15398 - [x] I'm a bit doubtful about the last commit -- see the message for my reasoning and do let me know whether it's right. changelog: [`borrow_as_ptr`]: don't lint inside proc-macros changelog: [`ptr_as_ptr`]: don't lint inside proc-macros
2 parents aeaa348 + 9570ed8 commit 9563a5c

File tree

8 files changed

+75
-54
lines changed

8 files changed

+75
-54
lines changed

clippy_lints/src/casts/borrow_as_ptr.rs

Lines changed: 4 additions & 5 deletions
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;
@@ -22,13 +22,12 @@ pub(super) fn check<'tcx>(
2222
&& !matches!(target.ty.kind, TyKind::TraitObject(..))
2323
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
2424
&& !is_lint_allowed(cx, BORROW_AS_PTR, expr.hir_id)
25+
// Fix #9884
26+
&& !is_expr_temporary_value(cx, e)
27+
&& !is_from_proc_macro(cx, expr)
2528
{
2629
let mut app = Applicability::MachineApplicable;
2730
let snip = snippet_with_context(cx, e.span, cast_expr.span.ctxt(), "..", &mut app).0;
28-
// Fix #9884
29-
if is_expr_temporary_value(cx, e) {
30-
return false;
31-
}
3231

3332
let (suggestion, span) = if msrv.meets(cx, msrvs::RAW_REF_OP) {
3433
// Make sure that the span to be replaced doesn't include parentheses, that could break the

clippy_lints/src/casts/ptr_as_ptr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_from_proc_macro;
23
use clippy_utils::msrvs::{self, Msrv};
34
use clippy_utils::source::snippet_with_applicability;
45
use clippy_utils::sugg::Sugg;
@@ -25,7 +26,7 @@ impl OmitFollowedCastReason<'_> {
2526
}
2627
}
2728

28-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Msrv) {
29+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv) {
2930
if let ExprKind::Cast(cast_expr, cast_to_hir_ty) = expr.kind
3031
&& let (cast_from, cast_to) = (cx.typeck_results().expr_ty(cast_expr), cx.typeck_results().expr_ty(expr))
3132
&& let ty::RawPtr(_, from_mutbl) = cast_from.kind()
@@ -36,6 +37,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Msrv) {
3637
// as explained here: https://github.com/rust-lang/rust/issues/60602.
3738
&& to_pointee_ty.is_sized(cx.tcx, cx.typing_env())
3839
&& msrv.meets(cx, msrvs::POINTER_CAST)
40+
&& !is_from_proc_macro(cx, expr)
3941
{
4042
let mut app = Applicability::MachineApplicable;
4143
let turbofish = match &cast_to_hir_ty.kind {

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
| ^^^^

tests/ui/ptr_as_ptr.fixed

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#![warn(clippy::ptr_as_ptr)]
44

5-
#[macro_use]
65
extern crate proc_macros;
6+
use proc_macros::{external, inline_macros, with_span};
77

88
mod issue_11278_a {
99
#[derive(Debug)]
@@ -53,11 +53,13 @@ fn main() {
5353
//~^ ptr_as_ptr
5454

5555
// Make sure the lint is triggered inside a macro
56-
let _ = inline!($ptr.cast::<i32>());
57-
//~^ ptr_as_ptr
56+
// FIXME: `is_from_proc_macro` incorrectly stops the lint from firing here
57+
let _ = inline!($ptr as *const i32);
5858

5959
// Do not lint inside macros from external crates
6060
let _ = external!($ptr as *const i32);
61+
62+
let _ = with_span!(expr $ptr as *const i32);
6163
}
6264

6365
#[clippy::msrv = "1.37"]

tests/ui/ptr_as_ptr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#![warn(clippy::ptr_as_ptr)]
44

5-
#[macro_use]
65
extern crate proc_macros;
6+
use proc_macros::{external, inline_macros, with_span};
77

88
mod issue_11278_a {
99
#[derive(Debug)]
@@ -53,11 +53,13 @@ fn main() {
5353
//~^ ptr_as_ptr
5454

5555
// Make sure the lint is triggered inside a macro
56+
// FIXME: `is_from_proc_macro` incorrectly stops the lint from firing here
5657
let _ = inline!($ptr as *const i32);
57-
//~^ ptr_as_ptr
5858

5959
// Do not lint inside macros from external crates
6060
let _ = external!($ptr as *const i32);
61+
62+
let _ = with_span!(expr $ptr as *const i32);
6163
}
6264

6365
#[clippy::msrv = "1.37"]

0 commit comments

Comments
 (0)