Skip to content

borrow-as-ptr eats parenthesis which break code #13882

@matthiaskrgr

Description

@matthiaskrgr

Summary

.

Lint Name

borrow-as-ptr

Reproducer

I tried this code:

// Make sure we cannot use raw ptrs that got transmuted from mutable references
// (i.e, no EscapeToRaw happened).
// We could, in principle, do EscapeToRaw lazily to allow this code, but that
// would no alleviate the need for EscapeToRaw (see `ref_raw_int_raw` in
// `run-pass/stacked-borrows.rs`), and thus increase overall complexity.
use std::mem;

fn main() {
    let mut x: [i32; 2] = [42, 43];
    let _raw: *mut i32 = unsafe { mem::transmute(&mut x[0]) };
    // `raw` still carries a tag, so we get another pointer to the same location that does not carry a tag
    let raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
    unsafe { *raw = 13 }; //~ ERROR: /write access .* tag does not exist in the borrow stack/
}

I saw this happen:
cargo +master clippy --fix -- -Wclippy::borrow-as-ptr

    Checking o v0.1.0 (/tmp/o)
warning: failed to automatically apply fixes suggested by rustc to crate `o`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0599]: no method named `wrapping_offset` found for type `i32` in the current scope
   --> src/main.rs:12:29
    |
12  |     let raw = &raw mut x[1].wrapping_offset(-1);
    |                             ^^^^^^^^^^^^^^^
    |
help: there is a method `wrapping_abs` with a similar name, but with different arguments
   --> /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/num/mod.rs:408:5
    |
408 | /     int_impl! {
409 | |         Self = i32,
410 | |         ActualT = i32,
411 | |         UnsignedT = u32,
...   |
426 | |         bound_condition = "",
427 | |     }
    | |_____^
    = note: this error originates in the macro `int_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
Original diagnostics will follow.

warning: transmute from a reference to a pointer
  --> src/main.rs:10:35
   |
10 |     let _raw: *mut i32 = unsafe { mem::transmute(&mut x[0]) };
   |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut x[0] as *mut i32`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_transmute
   = note: `#[warn(clippy::useless_transmute)]` on by default

warning: borrow as raw pointer
  --> src/main.rs:12:15
   |
12 |     let raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
   |               ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut x[1]`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_as_ptr
   = note: requested on the command line with `-W clippy::borrow-as-ptr`

warning: `o` (bin "o" test) generated 2 warnings (run `cargo clippy --fix --bin "o" --tests` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `o`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0599]: no method named `wrapping_offset` found for type `i32` in the current scope
   --> src/main.rs:12:29
    |
12  |     let raw = &raw mut x[1].wrapping_offset(-1);
    |                             ^^^^^^^^^^^^^^^
    |
help: there is a method `wrapping_abs` with a similar name, but with different arguments
   --> /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/num/mod.rs:408:5
    |
408 | /     int_impl! {
409 | |         Self = i32,
410 | |         ActualT = i32,
411 | |         UnsignedT = u32,
...   |
426 | |         bound_condition = "",
427 | |     }
    | |_____^
    = note: this error originates in the macro `int_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
Original diagnostics will follow.

warning: `o` (bin "o") generated 2 warnings (2 duplicates)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.03s

I expected to see this happen:

Version

rustc 1.85.0-nightly (19e75f4fb 2024-12-26)
binary: rustc
commit-hash: 19e75f4fb3f960267996e8788459e97b8769aac7
commit-date: 2024-12-26
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions