Skip to content

Commit b311db2

Browse files
committed
Fix clippy::ref_as_ptr for non-temporary references in let/const
1 parent 961ea1c commit b311db2

File tree

4 files changed

+85
-49
lines changed

4 files changed

+85
-49
lines changed

clippy_lints/src/casts/ref_as_ptr.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::sugg::Sugg;
4-
use clippy_utils::{ExprUseNode, expr_use_ctxt, std_or_core};
4+
use clippy_utils::{ExprUseNode, expr_use_ctxt, is_expr_temporary_value, std_or_core};
55
use rustc_errors::Applicability;
6-
use rustc_hir::{Expr, Mutability, Ty, TyKind};
6+
use rustc_hir::{Expr, ExprKind, Mutability, Ty, TyKind};
77
use rustc_lint::LateContext;
88
use rustc_middle::ty;
99

@@ -23,10 +23,18 @@ pub(super) fn check<'tcx>(
2323
if matches!(cast_from.kind(), ty::Ref(..))
2424
&& let ty::RawPtr(_, to_mutbl) = cast_to.kind()
2525
&& let use_cx = expr_use_ctxt(cx, expr)
26-
// TODO: only block the lint if `cast_expr` is a temporary
27-
&& !matches!(use_cx.use_node(cx), ExprUseNode::LetStmt(_) | ExprUseNode::ConstStatic(_))
2826
&& let Some(std_or_core) = std_or_core(cx)
2927
{
28+
if matches!(
29+
use_cx.use_node(cx),
30+
ExprUseNode::LetStmt(_) | ExprUseNode::ConstStatic(_)
31+
) {
32+
if let ExprKind::AddrOf(_, _, addr_inner) = cast_expr.kind
33+
&& is_expr_temporary_value(cx, addr_inner)
34+
{
35+
return;
36+
}
37+
}
3038
let fn_name = match to_mutbl {
3139
Mutability::Not => "from_ref",
3240
Mutability::Mut => "from_mut",

tests/ui/ref_as_ptr.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

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

6+
static STATIC_I32: i32 = 42;
7+
68
fn main() {
79
f(std::ptr::from_ref(&1u8));
810
//~^ ref_as_ptr
@@ -63,6 +65,9 @@ fn main() {
6365
f(std::ptr::from_ref::<i32>(&val) as *const f64);
6466
//~^ ref_as_ptr
6567

68+
let _ptr_from_local: *const i32 = std::ptr::from_ref(&val);
69+
//~^ ref_as_ptr
70+
6671
let mut val: u8 = 2;
6772
f(std::ptr::from_mut::<u8>(&mut val));
6873
//~^ ref_as_ptr
@@ -86,6 +91,9 @@ fn main() {
8691
f(std::ptr::from_mut::<[usize; 9]>(&mut std::array::from_fn(|i| i * i)));
8792
//~^ ref_as_ptr
8893

94+
let _ptr_from_static: *const i32 = std::ptr::from_ref(&STATIC_I32);
95+
//~^ ref_as_ptr
96+
8997
let _ = &String::new() as *const _;
9098
let _ = &mut String::new() as *mut _;
9199
const FOO: *const String = &String::new() as *const _;

tests/ui/ref_as_ptr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

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

6+
static STATIC_I32: i32 = 42;
7+
68
fn main() {
79
f(&1u8 as *const _);
810
//~^ ref_as_ptr
@@ -63,6 +65,9 @@ fn main() {
6365
f(&val as *const i32 as *const f64);
6466
//~^ ref_as_ptr
6567

68+
let _ptr_from_local: *const i32 = &val as *const _;
69+
//~^ ref_as_ptr
70+
6671
let mut val: u8 = 2;
6772
f(&mut val as *mut u8);
6873
//~^ ref_as_ptr
@@ -86,6 +91,9 @@ fn main() {
8691
f(&mut std::array::from_fn(|i| i * i) as *mut [usize; 9]);
8792
//~^ ref_as_ptr
8893

94+
let _ptr_from_static: *const i32 = &STATIC_I32 as *const _;
95+
//~^ ref_as_ptr
96+
8997
let _ = &String::new() as *const _;
9098
let _ = &mut String::new() as *mut _;
9199
const FOO: *const String = &String::new() as *const _;

0 commit comments

Comments
 (0)