Skip to content

Commit

Permalink
Auto merge of #111567 - Urgau:uplift_cast_ref_to_mut, r=b-naber
Browse files Browse the repository at this point in the history
Uplift `clippy::cast_ref_to_mut` lint

This PR aims at uplifting the `clippy::cast_ref_to_mut` lint into rustc.

## `cast_ref_to_mut`

(deny-by-default)

The `cast_ref_to_mut` lint checks for casts of `&T` to `&mut T` without using interior mutability.

### Example

```rust,compile_fail
fn x(r: &i32) {
    unsafe {
        *(r as *const i32 as *mut i32) += 1;
    }
}
```

### Explanation

Casting `&T` to `&mut T` without interior mutability is undefined behavior, as it's a violation of Rust reference aliasing requirements.

-----

Mostly followed the instructions for uplifting a clippy lint described here: rust-lang/rust#99696 (review)

`@rustbot` label: +I-lang-nominated
r? compiler

-----

For Clippy:

changelog: Moves: Uplifted `clippy::cast_ref_to_mut` into rustc
  • Loading branch information
bors committed Jun 1, 2023
2 parents 6e859d5 + ec3175e commit 38218d6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/fail/modifying_constants.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This should fail even without validation/SB
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows

#![allow(cast_ref_to_mut)]

fn main() {
let x = &1; // the `&1` is promoted to a constant, but it used to be that only the pointer is marked static, not the pointee
let y = unsafe { &mut *(x as *const i32 as *mut i32) };
Expand Down
2 changes: 2 additions & 0 deletions tests/fail/stacked_borrows/shr_frozen_violation1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(cast_ref_to_mut)]

fn foo(x: &mut i32) -> i32 {
*x = 5;
unknown_code(&*x);
Expand Down

0 comments on commit 38218d6

Please sign in to comment.