Skip to content

Make the dangerous_implicit_autorefs lint deny-by-default #141661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/autorefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare_lint! {
///
/// ### Example
///
/// ```rust
/// ```rust,compile_fail
/// unsafe fn fun(ptr: *mut [u8]) -> *mut [u8] {
/// unsafe { &raw mut (*ptr)[..16] }
/// // ^^^^^^ this calls `IndexMut::index_mut(&mut ..., ..16)`,
Expand Down Expand Up @@ -51,7 +51,7 @@ declare_lint! {
/// }
/// ```
pub DANGEROUS_IMPLICIT_AUTOREFS,
Warn,
Deny,
"implicit reference to a dereference of a raw pointer",
report_in_external_macro
}
Expand Down
53 changes: 28 additions & 25 deletions tests/ui/lint/implicit_autorefs.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ check-pass
//@ check-fail
//@ run-rustfix

#![allow(dead_code)] // For the rustfix-ed code.
Expand All @@ -8,7 +8,7 @@ use std::ops::Deref;

unsafe fn test_const(ptr: *const [u8]) {
let _ = (&(*ptr))[..16];
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

struct Test {
Expand All @@ -17,89 +17,92 @@ struct Test {

unsafe fn test_field(ptr: *const Test) -> *const [u8] {
let l = (&(*ptr).field).len();
//~^ WARN implicit autoref
//~^ ERROR implicit autoref

&raw const (&(*ptr).field)[..l - 1]
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_builtin_index(a: *mut [String]) {
_ = (&(*a)[0]).len();
//~^ WARN implicit autoref
//~^ ERROR implicit autoref

_ = (&(&(*a))[..1][0]).len();
//~^ WARN implicit autoref
//~^^ WARN implicit autoref
//~^ ERROR implicit autoref
//~^^ ERROR implicit autoref
}

unsafe fn test_overloaded_deref_const(ptr: *const ManuallyDrop<Test>) {
let _ = (&(*ptr)).field;
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
let _ = &raw const (&(*ptr)).field;
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_overloaded_deref_mut(ptr: *mut ManuallyDrop<Test>) {
let _ = (&(*ptr)).field;
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_double_overloaded_deref_const(ptr: *const ManuallyDrop<ManuallyDrop<Test>>) {
let _ = (&(*ptr)).field;
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_manually_overloaded_deref() {
struct W<T>(T);

impl<T> Deref for W<T> {
type Target = T;
fn deref(&self) -> &T { &self.0 }
fn deref(&self) -> &T {
&self.0
}
}

let w: W<i32> = W(5);
let w = &raw const w;
let _p: *const i32 = &raw const *(&**w);
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

struct Test2 {
// Derefs to `[u8]`.
field: &'static [u8]
field: &'static [u8],
}

fn test_more_manual_deref(ptr: *const Test2) -> usize {
unsafe { (&(*ptr).field).len() }
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_no_attr(ptr: *mut ManuallyDrop<u8>) {
ptr.write(ManuallyDrop::new(1)); // Should not warn, as `ManuallyDrop::write` is not
// annotated with `#[rustc_no_implicit_auto_ref]`
// Should not warn, as `ManuallyDrop::write` is not
// annotated with `#[rustc_no_implicit_auto_ref]`
ptr.write(ManuallyDrop::new(1));
}

unsafe fn test_vec_get(ptr: *mut Vec<u8>) {
let _ = (&(*ptr)).get(0);
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
let _ = (&(*ptr)).get_unchecked(0);
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
let _ = (&mut (*ptr)).get_mut(0);
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
let _ = (&mut (*ptr)).get_unchecked_mut(0);
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_string(ptr: *mut String) {
let _ = (&(*ptr)).len();
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
let _ = (&(*ptr)).is_empty();
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn slice_ptr_len_because_of_msrv<T>(slice: *const [T]) {
let _ = (&(&(*slice))[..]).len();
//~^ WARN implicit autoref
//~^^ WARN implicit autoref
//~^ ERROR implicit autoref
//~^^ ERROR implicit autoref
}

fn main() {}
53 changes: 28 additions & 25 deletions tests/ui/lint/implicit_autorefs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ check-pass
//@ check-fail
//@ run-rustfix

#![allow(dead_code)] // For the rustfix-ed code.
Expand All @@ -8,7 +8,7 @@ use std::ops::Deref;

unsafe fn test_const(ptr: *const [u8]) {
let _ = (*ptr)[..16];
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

struct Test {
Expand All @@ -17,89 +17,92 @@ struct Test {

unsafe fn test_field(ptr: *const Test) -> *const [u8] {
let l = (*ptr).field.len();
//~^ WARN implicit autoref
//~^ ERROR implicit autoref

&raw const (*ptr).field[..l - 1]
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_builtin_index(a: *mut [String]) {
_ = (*a)[0].len();
//~^ WARN implicit autoref
//~^ ERROR implicit autoref

_ = (*a)[..1][0].len();
//~^ WARN implicit autoref
//~^^ WARN implicit autoref
//~^ ERROR implicit autoref
//~^^ ERROR implicit autoref
}

unsafe fn test_overloaded_deref_const(ptr: *const ManuallyDrop<Test>) {
let _ = (*ptr).field;
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
let _ = &raw const (*ptr).field;
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_overloaded_deref_mut(ptr: *mut ManuallyDrop<Test>) {
let _ = (*ptr).field;
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_double_overloaded_deref_const(ptr: *const ManuallyDrop<ManuallyDrop<Test>>) {
let _ = (*ptr).field;
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_manually_overloaded_deref() {
struct W<T>(T);

impl<T> Deref for W<T> {
type Target = T;
fn deref(&self) -> &T { &self.0 }
fn deref(&self) -> &T {
&self.0
}
}

let w: W<i32> = W(5);
let w = &raw const w;
let _p: *const i32 = &raw const **w;
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

struct Test2 {
// Derefs to `[u8]`.
field: &'static [u8]
field: &'static [u8],
}

fn test_more_manual_deref(ptr: *const Test2) -> usize {
unsafe { (*ptr).field.len() }
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_no_attr(ptr: *mut ManuallyDrop<u8>) {
ptr.write(ManuallyDrop::new(1)); // Should not warn, as `ManuallyDrop::write` is not
// annotated with `#[rustc_no_implicit_auto_ref]`
// Should not warn, as `ManuallyDrop::write` is not
// annotated with `#[rustc_no_implicit_auto_ref]`
ptr.write(ManuallyDrop::new(1));
}

unsafe fn test_vec_get(ptr: *mut Vec<u8>) {
let _ = (*ptr).get(0);
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
let _ = (*ptr).get_unchecked(0);
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
let _ = (*ptr).get_mut(0);
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
let _ = (*ptr).get_unchecked_mut(0);
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn test_string(ptr: *mut String) {
let _ = (*ptr).len();
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
let _ = (*ptr).is_empty();
//~^ WARN implicit autoref
//~^ ERROR implicit autoref
}

unsafe fn slice_ptr_len_because_of_msrv<T>(slice: *const [T]) {
let _ = (*slice)[..].len();
//~^ WARN implicit autoref
//~^^ WARN implicit autoref
//~^ ERROR implicit autoref
//~^^ ERROR implicit autoref
}

fn main() {}
Loading
Loading