Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions clippy_lints/src/casts/borrow_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,13 +22,12 @@ pub(super) fn check<'tcx>(
&& !matches!(target.ty.kind, TyKind::TraitObject(..))
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
&& !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;
// Fix #9884
if is_expr_temporary_value(cx, e) {
return false;
}

let (suggestion, span) = if msrv.meets(cx, msrvs::RAW_REF_OP) {
// Make sure that the span to be replaced doesn't include parentheses, that could break the
Expand Down
4 changes: 3 additions & 1 deletion clippy_lints/src/casts/ptr_as_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_from_proc_macro;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
Expand All @@ -25,7 +26,7 @@ impl OmitFollowedCastReason<'_> {
}
}

pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Msrv) {
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv) {
if let ExprKind::Cast(cast_expr, cast_to_hir_ty) = expr.kind
&& let (cast_from, cast_to) = (cx.typeck_results().expr_ty(cast_expr), cx.typeck_results().expr_ty(expr))
&& let ty::RawPtr(_, from_mutbl) = cast_from.kind()
Expand All @@ -36,6 +37,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Msrv) {
// as explained here: https://github.com/rust-lang/rust/issues/60602.
&& to_pointee_ty.is_sized(cx.tcx, cx.typing_env())
&& msrv.meets(cx, msrvs::POINTER_CAST)
&& !is_from_proc_macro(cx, expr)
{
let mut app = Applicability::MachineApplicable;
let turbofish = match &cast_to_hir_ty.kind {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/borrow_as_ptr.fixed
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down Expand Up @@ -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;
};
}
12 changes: 12 additions & 0 deletions tests/ui/borrow_as_ptr.rs
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down Expand Up @@ -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;
};
}
14 changes: 7 additions & 7 deletions tests/ui/borrow_as_ptr.stderr
Original file line number Diff line number Diff line change
@@ -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)`
Expand All @@ -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;
| ^^^^
Expand All @@ -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;
| ^^^^^^^^
Expand All @@ -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);
| ^^^^
Expand Down
8 changes: 5 additions & 3 deletions tests/ui/ptr_as_ptr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#![warn(clippy::ptr_as_ptr)]

#[macro_use]
extern crate proc_macros;
use proc_macros::{external, inline_macros, with_span};

mod issue_11278_a {
#[derive(Debug)]
Expand Down Expand Up @@ -53,11 +53,13 @@ fn main() {
//~^ ptr_as_ptr

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

// Do not lint inside macros from external crates
let _ = external!($ptr as *const i32);

let _ = with_span!(expr $ptr as *const i32);
}

#[clippy::msrv = "1.37"]
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/ptr_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#![warn(clippy::ptr_as_ptr)]

#[macro_use]
extern crate proc_macros;
use proc_macros::{external, inline_macros, with_span};

mod issue_11278_a {
#[derive(Debug)]
Expand Down Expand Up @@ -53,11 +53,13 @@ fn main() {
//~^ ptr_as_ptr

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

// Do not lint inside macros from external crates
let _ = external!($ptr as *const i32);

let _ = with_span!(expr $ptr as *const i32);
}

#[clippy::msrv = "1.37"]
Expand Down
Loading