Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
implicit_unsafe_autorefs: lint field access too
  • Loading branch information
WaffleLapkin committed Nov 2, 2022
commit af4ade4cf60d64b9c8406bc32b21a2caef865fdd
11 changes: 9 additions & 2 deletions compiler/rustc_lint/src/implicit_unsafe_autorefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitUnsafeAutorefs {
&& let [adjustment] = &**adjustments
// An auto-borrow
&& let Adjust::Borrow(AutoBorrow::Ref(_, mutbl)) = adjustment.kind
// ... of a deref
&& let ExprKind::Unary(UnOp::Deref, dereferenced) = expr.kind
// ... of a place derived from a deref
&& let ExprKind::Unary(UnOp::Deref, dereferenced) = skip_field_access(&expr.kind)
// ... of a raw pointer
&& typeck.expr_ty(dereferenced).is_unsafe_ptr()
{
Expand All @@ -81,3 +81,10 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitUnsafeAutorefs {
}
}
}

fn skip_field_access<'a>(mut expr: &'a ExprKind<'a>) -> &'a ExprKind<'a> {
while let ExprKind::Field(e, _) = expr {
expr = &e.kind;
}
expr
}
17 changes: 15 additions & 2 deletions src/test/ui/lint/implicit_unsafe_autorefs.fixed
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
// check-pass
// run-rustfix
#![allow(dead_code)]
use std::ptr::{addr_of, addr_of_mut};

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

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

struct Test {
field: [u8],
}

unsafe fn test_field(ptr: *const Test) -> *const [u8] {
let l = (&(*ptr).field).len();
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer

addr_of!((&(*ptr).field)[..l - 1])
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer
}

fn main() {}
17 changes: 15 additions & 2 deletions src/test/ui/lint/implicit_unsafe_autorefs.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
// check-pass
// run-rustfix
#![allow(dead_code)]
use std::ptr::{addr_of, addr_of_mut};

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

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

struct Test {
field: [u8],
}

unsafe fn test_field(ptr: *const Test) -> *const [u8] {
let l = (*ptr).field.len();
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer

addr_of!((*ptr).field[..l - 1])
//~^ warn: implicit auto-ref creates a reference to a dereference of a raw pointer
}

fn main() {}
30 changes: 27 additions & 3 deletions src/test/ui/lint/implicit_unsafe_autorefs.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: implicit auto-ref creates a reference to a dereference of a raw pointer
--> $DIR/implicit_unsafe_autorefs.rs:6:18
--> $DIR/implicit_unsafe_autorefs.rs:7:18
|
LL | addr_of_mut!((*ptr)[..16])
| ^^^^^^
Expand All @@ -12,7 +12,7 @@ LL | addr_of_mut!((&mut (*ptr))[..16])
| +++++ +

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

warning: 2 warnings emitted
warning: implicit auto-ref creates a reference to a dereference of a raw pointer
--> $DIR/implicit_unsafe_autorefs.rs:21:13
|
LL | let l = (*ptr).field.len();
| ^^^^^^^^^^^^
|
= note: creating a reference requires the pointer to be valid and imposes aliasing requirements
help: try using a raw pointer method instead; or if this reference is intentional, make it explicit
|
LL | let l = (&(*ptr).field).len();
| ++ +

warning: implicit auto-ref creates a reference to a dereference of a raw pointer
--> $DIR/implicit_unsafe_autorefs.rs:24:14
|
LL | addr_of!((*ptr).field[..l - 1])
| ^^^^^^^^^^^^
|
= note: creating a reference requires the pointer to be valid and imposes aliasing requirements
help: try using a raw pointer method instead; or if this reference is intentional, make it explicit
|
LL | addr_of!((&(*ptr).field)[..l - 1])
| ++ +

warning: 4 warnings emitted