Skip to content
Closed
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
16 changes: 12 additions & 4 deletions src/tools/clippy/clippy_lints/src/casts/ref_as_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use clippy_utils::{ExprUseNode, expr_use_ctxt, std_or_core};
use clippy_utils::{ExprUseNode, expr_use_ctxt, is_expr_temporary_value, std_or_core};
use rustc_errors::Applicability;
use rustc_hir::{Expr, Mutability, Ty, TyKind};
use rustc_hir::{Expr, ExprKind, Mutability, Ty, TyKind};
use rustc_lint::LateContext;
use rustc_middle::ty;

Expand All @@ -23,10 +23,18 @@ pub(super) fn check<'tcx>(
if matches!(cast_from.kind(), ty::Ref(..))
&& let ty::RawPtr(_, to_mutbl) = cast_to.kind()
&& let use_cx = expr_use_ctxt(cx, expr)
// TODO: only block the lint if `cast_expr` is a temporary
&& !matches!(use_cx.use_node(cx), ExprUseNode::LetStmt(_) | ExprUseNode::ConstStatic(_))
&& let Some(std_or_core) = std_or_core(cx)
{
if matches!(
use_cx.use_node(cx),
ExprUseNode::LetStmt(_) | ExprUseNode::ConstStatic(_)
) {
if let ExprKind::AddrOf(_, _, addr_inner) = cast_expr.kind
&& is_expr_temporary_value(cx, addr_inner)
{
return;
}
}
let fn_name = match to_mutbl {
Mutability::Not => "from_ref",
Mutability::Mut => "from_mut",
Expand Down
8 changes: 8 additions & 0 deletions src/tools/clippy/tests/ui/ref_as_ptr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

fn f<T>(_: T) {}

static STATIC_I32: i32 = 42;

fn main() {
f(std::ptr::from_ref(&1u8));
//~^ ref_as_ptr
Expand Down Expand Up @@ -63,6 +65,9 @@ fn main() {
f(std::ptr::from_ref::<i32>(&val) as *const f64);
//~^ ref_as_ptr

let _ptr_from_local: *const i32 = std::ptr::from_ref(&val);
//~^ ref_as_ptr

let mut val: u8 = 2;
f(std::ptr::from_mut::<u8>(&mut val));
//~^ ref_as_ptr
Expand All @@ -86,6 +91,9 @@ fn main() {
f(std::ptr::from_mut::<[usize; 9]>(&mut std::array::from_fn(|i| i * i)));
//~^ ref_as_ptr

let _ptr_from_static: *const i32 = std::ptr::from_ref(&STATIC_I32);
//~^ ref_as_ptr

let _ = &String::new() as *const _;
let _ = &mut String::new() as *mut _;
const FOO: *const String = &String::new() as *const _;
Expand Down
8 changes: 8 additions & 0 deletions src/tools/clippy/tests/ui/ref_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

fn f<T>(_: T) {}

static STATIC_I32: i32 = 42;

fn main() {
f(&1u8 as *const _);
//~^ ref_as_ptr
Expand Down Expand Up @@ -63,6 +65,9 @@ fn main() {
f(&val as *const i32 as *const f64);
//~^ ref_as_ptr

let _ptr_from_local: *const i32 = &val as *const _;
//~^ ref_as_ptr

let mut val: u8 = 2;
f(&mut val as *mut u8);
//~^ ref_as_ptr
Expand All @@ -86,6 +91,9 @@ fn main() {
f(&mut std::array::from_fn(|i| i * i) as *mut [usize; 9]);
//~^ ref_as_ptr

let _ptr_from_static: *const i32 = &STATIC_I32 as *const _;
//~^ ref_as_ptr

let _ = &String::new() as *const _;
let _ = &mut String::new() as *mut _;
const FOO: *const String = &String::new() as *const _;
Expand Down
Loading
Loading