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
16 changes: 12 additions & 4 deletions 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 let ExprKind::AddrOf(_, _, addr_inner) = cast_expr.kind
&& is_expr_temporary_value(cx, addr_inner)
&& matches!(
use_cx.use_node(cx),
ExprUseNode::LetStmt(_) | ExprUseNode::ConstStatic(_)
)
{
return;
}

let fn_name = match to_mutbl {
Mutability::Not => "from_ref",
Mutability::Mut => "from_mut",
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/ref_as_ptr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ fn main() {
f(std::ptr::from_mut::<[usize; 9]>(&mut std::array::from_fn(|i| i * i)));
//~^ ref_as_ptr

let x = (10, 20);
let _ = std::ptr::from_ref(&x);
//~^ ref_as_ptr
let _ = std::ptr::from_ref(&x.0);
//~^ ref_as_ptr

let x = Box::new(10);
let _ = std::ptr::from_ref(&*x);
//~^ ref_as_ptr

let _ = &String::new() as *const _;
let _ = &mut String::new() as *mut _;
const FOO: *const String = &String::new() as *const _;
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/ref_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ fn main() {
f(&mut std::array::from_fn(|i| i * i) as *mut [usize; 9]);
//~^ ref_as_ptr

let x = (10, 20);
let _ = &x as *const _;
//~^ ref_as_ptr
let _ = &x.0 as *const _;
//~^ ref_as_ptr

let x = Box::new(10);
let _ = &*x 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
42 changes: 30 additions & 12 deletions tests/ui/ref_as_ptr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -200,70 +200,88 @@ LL | f(&mut std::array::from_fn(|i| i * i) as *mut [usize; 9]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_mut::<[usize; 9]>(&mut std::array::from_fn(|i| i * i))`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:109:7
--> tests/ui/ref_as_ptr.rs:90:13
|
LL | let _ = &x as *const _;
| ^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref(&x)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:92:13
|
LL | let _ = &x.0 as *const _;
| ^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref(&x.0)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:96:13
|
LL | let _ = &*x as *const _;
| ^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref(&*x)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:119:7
|
LL | f(val as *const i32);
| ^^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref::<i32>(val)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:111:7
--> tests/ui/ref_as_ptr.rs:121:7
|
LL | f(mut_val as *mut i32);
| ^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_mut::<i32>(mut_val)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:116:7
--> tests/ui/ref_as_ptr.rs:126:7
|
LL | f(val as *const _);
| ^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref(val)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:118:7
--> tests/ui/ref_as_ptr.rs:128:7
|
LL | f(val as *const [u8]);
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref::<[u8]>(val)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:123:7
--> tests/ui/ref_as_ptr.rs:133:7
|
LL | f(val as *mut _);
| ^^^^^^^^^^^^^ help: try: `std::ptr::from_mut(val)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:125:7
--> tests/ui/ref_as_ptr.rs:135:7
|
LL | f(val as *mut str);
| ^^^^^^^^^^^^^^^ help: try: `std::ptr::from_mut::<str>(val)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:133:9
--> tests/ui/ref_as_ptr.rs:143:9
|
LL | self.0 as *const _ as *const _
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref(self.0)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:138:9
--> tests/ui/ref_as_ptr.rs:148:9
|
LL | self.0 as *const _ as *const _
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref(self.0)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:147:9
--> tests/ui/ref_as_ptr.rs:157:9
|
LL | self.0 as *const _ as *const _
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref(self.0)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:152:9
--> tests/ui/ref_as_ptr.rs:162:9
|
LL | self.0 as *const _ as *const _
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref(self.0)`

error: reference as raw pointer
--> tests/ui/ref_as_ptr.rs:157:9
--> tests/ui/ref_as_ptr.rs:167:9
|
LL | self.0 as *mut _ as *mut _
| ^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_mut(self.0)`

error: aborting due to 44 previous errors
error: aborting due to 47 previous errors