Skip to content

Commit af4ade4

Browse files
committed
implicit_unsafe_autorefs: lint field access too
1 parent 4a7791d commit af4ade4

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

compiler/rustc_lint/src/implicit_unsafe_autorefs.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitUnsafeAutorefs {
5858
&& let [adjustment] = &**adjustments
5959
// An auto-borrow
6060
&& let Adjust::Borrow(AutoBorrow::Ref(_, mutbl)) = adjustment.kind
61-
// ... of a deref
62-
&& let ExprKind::Unary(UnOp::Deref, dereferenced) = expr.kind
61+
// ... of a place derived from a deref
62+
&& let ExprKind::Unary(UnOp::Deref, dereferenced) = skip_field_access(&expr.kind)
6363
// ... of a raw pointer
6464
&& typeck.expr_ty(dereferenced).is_unsafe_ptr()
6565
{
@@ -81,3 +81,10 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitUnsafeAutorefs {
8181
}
8282
}
8383
}
84+
85+
fn skip_field_access<'a>(mut expr: &'a ExprKind<'a>) -> &'a ExprKind<'a> {
86+
while let ExprKind::Field(e, _) = expr {
87+
expr = &e.kind;
88+
}
89+
expr
90+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
// check-pass
22
// run-rustfix
3+
#![allow(dead_code)]
34
use std::ptr::{addr_of, addr_of_mut};
45

5-
unsafe fn _test_mut(ptr: *mut [u8]) -> *mut [u8] {
6+
unsafe fn test_mut(ptr: *mut [u8]) -> *mut [u8] {
67
addr_of_mut!((&mut (*ptr))[..16])
78
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer
89
}
910

10-
unsafe fn _test_const(ptr: *const [u8]) -> *const [u8] {
11+
unsafe fn test_const(ptr: *const [u8]) -> *const [u8] {
1112
addr_of!((&(*ptr))[..16])
1213
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer
1314
}
1415

16+
struct Test {
17+
field: [u8],
18+
}
19+
20+
unsafe fn test_field(ptr: *const Test) -> *const [u8] {
21+
let l = (&(*ptr).field).len();
22+
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer
23+
24+
addr_of!((&(*ptr).field)[..l - 1])
25+
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer
26+
}
27+
1528
fn main() {}
+15-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
// check-pass
22
// run-rustfix
3+
#![allow(dead_code)]
34
use std::ptr::{addr_of, addr_of_mut};
45

5-
unsafe fn _test_mut(ptr: *mut [u8]) -> *mut [u8] {
6+
unsafe fn test_mut(ptr: *mut [u8]) -> *mut [u8] {
67
addr_of_mut!((*ptr)[..16])
78
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer
89
}
910

10-
unsafe fn _test_const(ptr: *const [u8]) -> *const [u8] {
11+
unsafe fn test_const(ptr: *const [u8]) -> *const [u8] {
1112
addr_of!((*ptr)[..16])
1213
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer
1314
}
1415

16+
struct Test {
17+
field: [u8],
18+
}
19+
20+
unsafe fn test_field(ptr: *const Test) -> *const [u8] {
21+
let l = (*ptr).field.len();
22+
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer
23+
24+
addr_of!((*ptr).field[..l - 1])
25+
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer
26+
}
27+
1528
fn main() {}

src/test/ui/lint/implicit_unsafe_autorefs.stderr

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: implicit auto-ref creates a reference to a dereference of a raw pointer
2-
--> $DIR/implicit_unsafe_autorefs.rs:6:18
2+
--> $DIR/implicit_unsafe_autorefs.rs:7:18
33
|
44
LL | addr_of_mut!((*ptr)[..16])
55
| ^^^^^^
@@ -12,7 +12,7 @@ LL | addr_of_mut!((&mut (*ptr))[..16])
1212
| +++++ +
1313

1414
warning: implicit auto-ref creates a reference to a dereference of a raw pointer
15-
--> $DIR/implicit_unsafe_autorefs.rs:11:14
15+
--> $DIR/implicit_unsafe_autorefs.rs:12:14
1616
|
1717
LL | addr_of!((*ptr)[..16])
1818
| ^^^^^^
@@ -23,5 +23,29 @@ help: try using a raw pointer method instead; or if this reference is intentiona
2323
LL | addr_of!((&(*ptr))[..16])
2424
| ++ +
2525

26-
warning: 2 warnings emitted
26+
warning: implicit auto-ref creates a reference to a dereference of a raw pointer
27+
--> $DIR/implicit_unsafe_autorefs.rs:21:13
28+
|
29+
LL | let l = (*ptr).field.len();
30+
| ^^^^^^^^^^^^
31+
|
32+
= note: creating a reference requires the pointer to be valid and imposes aliasing requirements
33+
help: try using a raw pointer method instead; or if this reference is intentional, make it explicit
34+
|
35+
LL | let l = (&(*ptr).field).len();
36+
| ++ +
37+
38+
warning: implicit auto-ref creates a reference to a dereference of a raw pointer
39+
--> $DIR/implicit_unsafe_autorefs.rs:24:14
40+
|
41+
LL | addr_of!((*ptr).field[..l - 1])
42+
| ^^^^^^^^^^^^
43+
|
44+
= note: creating a reference requires the pointer to be valid and imposes aliasing requirements
45+
help: try using a raw pointer method instead; or if this reference is intentional, make it explicit
46+
|
47+
LL | addr_of!((&(*ptr).field)[..l - 1])
48+
| ++ +
49+
50+
warning: 4 warnings emitted
2751

0 commit comments

Comments
 (0)