Skip to content

Commit 5ab42d8

Browse files
committed
Take lifetime extension into account in ref_as_ptr
1 parent 5a52c8a commit 5ab42d8

File tree

4 files changed

+226
-206
lines changed

4 files changed

+226
-206
lines changed

clippy_lints/src/casts/ref_as_ptr.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::is_no_std_crate;
32
use clippy_utils::source::snippet_with_applicability;
43
use clippy_utils::sugg::Sugg;
4+
use clippy_utils::{expr_use_ctxt, is_no_std_crate, ExprUseNode};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, Mutability, Ty, TyKind};
77
use rustc_lint::LateContext;
88
use rustc_middle::ty::{self, TypeAndMut};
99

1010
use super::REF_AS_PTR;
1111

12-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to_hir_ty: &Ty<'_>) {
12+
pub(super) fn check<'tcx>(
13+
cx: &LateContext<'tcx>,
14+
expr: &'tcx Expr<'_>,
15+
cast_expr: &'tcx Expr<'_>,
16+
cast_to_hir_ty: &Ty<'_>,
17+
) {
1318
let (cast_from, cast_to) = (
1419
cx.typeck_results().expr_ty(cast_expr),
1520
cx.typeck_results().expr_ty(expr),
1621
);
1722

1823
if matches!(cast_from.kind(), ty::Ref(..))
1924
&& let ty::RawPtr(TypeAndMut { mutbl: to_mutbl, .. }) = cast_to.kind()
25+
&& let Some(use_cx) = expr_use_ctxt(cx, expr)
26+
// TODO: only block the lint if `cast_expr` is a temporary
27+
&& !matches!(use_cx.node, ExprUseNode::Local(_) | ExprUseNode::ConstStatic(_))
2028
{
2129
let core_or_std = if is_no_std_crate(cx) { "core" } else { "std" };
2230
let fn_name = match to_mutbl {

tests/ui/ref_as_ptr.fixed

+47-41
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,61 @@
11
#![warn(clippy::ref_as_ptr)]
22
#![allow(clippy::unnecessary_mut_passed)]
33

4+
fn f<T>(_: T) {}
5+
46
fn main() {
5-
let _ = std::ptr::from_ref(&1u8);
6-
let _ = std::ptr::from_ref::<u32>(&2u32);
7-
let _ = std::ptr::from_ref::<f64>(&3.0f64);
7+
f(std::ptr::from_ref(&1u8));
8+
f(std::ptr::from_ref::<u32>(&2u32));
9+
f(std::ptr::from_ref::<f64>(&3.0f64));
810

9-
let _ = std::ptr::from_ref(&4) as *const f32;
10-
let _ = std::ptr::from_ref::<f32>(&5.0f32) as *const u32;
11+
f(std::ptr::from_ref(&4) as *const f32);
12+
f(std::ptr::from_ref::<f32>(&5.0f32) as *const u32);
1113

12-
let _ = std::ptr::from_ref(&mut 6u8);
13-
let _ = std::ptr::from_ref::<u32>(&mut 7u32);
14-
let _ = std::ptr::from_ref::<f64>(&mut 8.0f64);
14+
f(std::ptr::from_ref(&mut 6u8));
15+
f(std::ptr::from_ref::<u32>(&mut 7u32));
16+
f(std::ptr::from_ref::<f64>(&mut 8.0f64));
1517

16-
let _ = std::ptr::from_ref(&mut 9) as *const f32;
17-
let _ = std::ptr::from_ref::<f32>(&mut 10.0f32) as *const u32;
18+
f(std::ptr::from_ref(&mut 9) as *const f32);
19+
f(std::ptr::from_ref::<f32>(&mut 10.0f32) as *const u32);
1820

19-
let _ = std::ptr::from_mut(&mut 11u8);
20-
let _ = std::ptr::from_mut::<u32>(&mut 12u32);
21-
let _ = std::ptr::from_mut::<f64>(&mut 13.0f64);
21+
f(std::ptr::from_mut(&mut 11u8));
22+
f(std::ptr::from_mut::<u32>(&mut 12u32));
23+
f(std::ptr::from_mut::<f64>(&mut 13.0f64));
2224

23-
let _ = std::ptr::from_mut(&mut 14) as *const f32;
24-
let _ = std::ptr::from_mut::<f32>(&mut 15.0f32) as *const u32;
25+
f(std::ptr::from_mut(&mut 14) as *const f32);
26+
f(std::ptr::from_mut::<f32>(&mut 15.0f32) as *const u32);
2527

26-
let _ = std::ptr::from_ref(&1u8);
27-
let _ = std::ptr::from_ref::<u32>(&2u32);
28-
let _ = std::ptr::from_ref::<f64>(&3.0f64);
28+
f(std::ptr::from_ref(&1u8));
29+
f(std::ptr::from_ref::<u32>(&2u32));
30+
f(std::ptr::from_ref::<f64>(&3.0f64));
2931

30-
let _ = std::ptr::from_ref(&4) as *const f32;
31-
let _ = std::ptr::from_ref::<f32>(&5.0f32) as *const u32;
32+
f(std::ptr::from_ref(&4) as *const f32);
33+
f(std::ptr::from_ref::<f32>(&5.0f32) as *const u32);
3234

3335
let val = 1;
34-
let _ = std::ptr::from_ref(&val);
35-
let _ = std::ptr::from_ref::<i32>(&val);
36+
f(std::ptr::from_ref(&val));
37+
f(std::ptr::from_ref::<i32>(&val));
3638

37-
let _ = std::ptr::from_ref(&val) as *const f32;
38-
let _ = std::ptr::from_ref::<i32>(&val) as *const f64;
39+
f(std::ptr::from_ref(&val) as *const f32);
40+
f(std::ptr::from_ref::<i32>(&val) as *const f64);
3941

4042
let mut val: u8 = 2;
41-
let _ = std::ptr::from_mut::<u8>(&mut val);
42-
let _ = std::ptr::from_mut(&mut val);
43+
f(std::ptr::from_mut::<u8>(&mut val));
44+
f(std::ptr::from_mut(&mut val));
45+
46+
f(std::ptr::from_ref::<u8>(&mut val));
47+
f(std::ptr::from_ref(&mut val));
4348

44-
let _ = std::ptr::from_ref::<u8>(&mut val);
45-
let _ = std::ptr::from_ref(&mut val);
49+
f(std::ptr::from_ref::<u8>(&mut val) as *const f64);
50+
f::<*const Option<u8>>(std::ptr::from_ref(&mut val) as *const _);
4651

47-
let _ = std::ptr::from_ref::<u8>(&mut val) as *const f64;
48-
let _: *const Option<u8> = std::ptr::from_ref(&mut val) as *const _;
52+
f(std::ptr::from_ref::<[usize; 7]>(&std::array::from_fn(|i| i * i)));
53+
f(std::ptr::from_ref::<[usize; 8]>(&mut std::array::from_fn(|i| i * i)));
54+
f(std::ptr::from_mut::<[usize; 9]>(&mut std::array::from_fn(|i| i * i)));
4955

50-
let _ = std::ptr::from_ref::<[usize; 7]>(&std::array::from_fn(|i| i * i));
51-
let _ = std::ptr::from_ref::<[usize; 8]>(&mut std::array::from_fn(|i| i * i));
52-
let _ = std::ptr::from_mut::<[usize; 9]>(&mut std::array::from_fn(|i| i * i));
56+
let _ = &String::new() as *const _;
57+
let _ = &mut String::new() as *mut _;
58+
const FOO: *const String = &String::new() as *const _;
5359
}
5460

5561
#[clippy::msrv = "1.75"]
@@ -58,27 +64,27 @@ fn _msrv_1_75() {
5864
let mut_val = &mut 42_i32;
5965

6066
// `std::ptr::from_{ref, mut}` was stabilized in 1.76. Do not lint this
61-
let _ = val as *const i32;
62-
let _ = mut_val as *mut i32;
67+
f(val as *const i32);
68+
f(mut_val as *mut i32);
6369
}
6470

6571
#[clippy::msrv = "1.76"]
6672
fn _msrv_1_76() {
6773
let val = &42_i32;
6874
let mut_val = &mut 42_i32;
6975

70-
let _ = std::ptr::from_ref::<i32>(val);
71-
let _ = std::ptr::from_mut::<i32>(mut_val);
76+
f(std::ptr::from_ref::<i32>(val));
77+
f(std::ptr::from_mut::<i32>(mut_val));
7278
}
7379

7480
fn foo(val: &[u8]) {
75-
let _ = std::ptr::from_ref(val);
76-
let _ = std::ptr::from_ref::<[u8]>(val);
81+
f(std::ptr::from_ref(val));
82+
f(std::ptr::from_ref::<[u8]>(val));
7783
}
7884

7985
fn bar(val: &mut str) {
80-
let _ = std::ptr::from_mut(val);
81-
let _ = std::ptr::from_mut::<str>(val);
86+
f(std::ptr::from_mut(val));
87+
f(std::ptr::from_mut::<str>(val));
8288
}
8389

8490
struct X<'a>(&'a i32);

tests/ui/ref_as_ptr.rs

+47-41
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,61 @@
11
#![warn(clippy::ref_as_ptr)]
22
#![allow(clippy::unnecessary_mut_passed)]
33

4+
fn f<T>(_: T) {}
5+
46
fn main() {
5-
let _ = &1u8 as *const _;
6-
let _ = &2u32 as *const u32;
7-
let _ = &3.0f64 as *const f64;
7+
f(&1u8 as *const _);
8+
f(&2u32 as *const u32);
9+
f(&3.0f64 as *const f64);
810

9-
let _ = &4 as *const _ as *const f32;
10-
let _ = &5.0f32 as *const f32 as *const u32;
11+
f(&4 as *const _ as *const f32);
12+
f(&5.0f32 as *const f32 as *const u32);
1113

12-
let _ = &mut 6u8 as *const _;
13-
let _ = &mut 7u32 as *const u32;
14-
let _ = &mut 8.0f64 as *const f64;
14+
f(&mut 6u8 as *const _);
15+
f(&mut 7u32 as *const u32);
16+
f(&mut 8.0f64 as *const f64);
1517

16-
let _ = &mut 9 as *const _ as *const f32;
17-
let _ = &mut 10.0f32 as *const f32 as *const u32;
18+
f(&mut 9 as *const _ as *const f32);
19+
f(&mut 10.0f32 as *const f32 as *const u32);
1820

19-
let _ = &mut 11u8 as *mut _;
20-
let _ = &mut 12u32 as *mut u32;
21-
let _ = &mut 13.0f64 as *mut f64;
21+
f(&mut 11u8 as *mut _);
22+
f(&mut 12u32 as *mut u32);
23+
f(&mut 13.0f64 as *mut f64);
2224

23-
let _ = &mut 14 as *mut _ as *const f32;
24-
let _ = &mut 15.0f32 as *mut f32 as *const u32;
25+
f(&mut 14 as *mut _ as *const f32);
26+
f(&mut 15.0f32 as *mut f32 as *const u32);
2527

26-
let _ = &1u8 as *const _;
27-
let _ = &2u32 as *const u32;
28-
let _ = &3.0f64 as *const f64;
28+
f(&1u8 as *const _);
29+
f(&2u32 as *const u32);
30+
f(&3.0f64 as *const f64);
2931

30-
let _ = &4 as *const _ as *const f32;
31-
let _ = &5.0f32 as *const f32 as *const u32;
32+
f(&4 as *const _ as *const f32);
33+
f(&5.0f32 as *const f32 as *const u32);
3234

3335
let val = 1;
34-
let _ = &val as *const _;
35-
let _ = &val as *const i32;
36+
f(&val as *const _);
37+
f(&val as *const i32);
3638

37-
let _ = &val as *const _ as *const f32;
38-
let _ = &val as *const i32 as *const f64;
39+
f(&val as *const _ as *const f32);
40+
f(&val as *const i32 as *const f64);
3941

4042
let mut val: u8 = 2;
41-
let _ = &mut val as *mut u8;
42-
let _ = &mut val as *mut _;
43+
f(&mut val as *mut u8);
44+
f(&mut val as *mut _);
45+
46+
f(&mut val as *const u8);
47+
f(&mut val as *const _);
4348

44-
let _ = &mut val as *const u8;
45-
let _ = &mut val as *const _;
49+
f(&mut val as *const u8 as *const f64);
50+
f::<*const Option<u8>>(&mut val as *const _ as *const _);
4651

47-
let _ = &mut val as *const u8 as *const f64;
48-
let _: *const Option<u8> = &mut val as *const _ as *const _;
52+
f(&std::array::from_fn(|i| i * i) as *const [usize; 7]);
53+
f(&mut std::array::from_fn(|i| i * i) as *const [usize; 8]);
54+
f(&mut std::array::from_fn(|i| i * i) as *mut [usize; 9]);
4955

50-
let _ = &std::array::from_fn(|i| i * i) as *const [usize; 7];
51-
let _ = &mut std::array::from_fn(|i| i * i) as *const [usize; 8];
52-
let _ = &mut std::array::from_fn(|i| i * i) as *mut [usize; 9];
56+
let _ = &String::new() as *const _;
57+
let _ = &mut String::new() as *mut _;
58+
const FOO: *const String = &String::new() as *const _;
5359
}
5460

5561
#[clippy::msrv = "1.75"]
@@ -58,27 +64,27 @@ fn _msrv_1_75() {
5864
let mut_val = &mut 42_i32;
5965

6066
// `std::ptr::from_{ref, mut}` was stabilized in 1.76. Do not lint this
61-
let _ = val as *const i32;
62-
let _ = mut_val as *mut i32;
67+
f(val as *const i32);
68+
f(mut_val as *mut i32);
6369
}
6470

6571
#[clippy::msrv = "1.76"]
6672
fn _msrv_1_76() {
6773
let val = &42_i32;
6874
let mut_val = &mut 42_i32;
6975

70-
let _ = val as *const i32;
71-
let _ = mut_val as *mut i32;
76+
f(val as *const i32);
77+
f(mut_val as *mut i32);
7278
}
7379

7480
fn foo(val: &[u8]) {
75-
let _ = val as *const _;
76-
let _ = val as *const [u8];
81+
f(val as *const _);
82+
f(val as *const [u8]);
7783
}
7884

7985
fn bar(val: &mut str) {
80-
let _ = val as *mut _;
81-
let _ = val as *mut str;
86+
f(val as *mut _);
87+
f(val as *mut str);
8288
}
8389

8490
struct X<'a>(&'a i32);

0 commit comments

Comments
 (0)