Skip to content

unnecessary_cast causes borrowcheck problems #9906

Closed
@matthiaskrgr

Description

@matthiaskrgr

Summary

.

Lint Name

unnecessary_cast

Reproducer

I tried this code:

// run-pass
// check that casts are not being treated as lexprs.

fn main() {
    let mut a = 0i32;
    let b = &(a as i32);
    a = 1;
    assert_ne!(&a as *const i32, b as *const i32);
    assert_eq!(*b, 0);

    assert_eq!(issue_36936(), 1);
}


struct A(u32);

impl Drop for A {
    fn drop(&mut self) {
        self.0 = 0;
    }
}

fn issue_36936() -> u32 {
    let a = &(A(1) as A);
    a.0
}

I saw this happen:
cargo clippy --fix -- -Aclippy::all -Wclippy::unnecessary_cast

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

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/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[E0506]: cannot assign to `a` because it is borrowed
 --> src/main.rs:4:5
  |
3 |     let b = &a;
  |             -- borrow of `a` occurs here
4 |     a = 1;
  |     ^^^^^ assignment to borrowed `a` occurs here
5 |     assert_ne!(&a as *const i32, b as *const i32);
  |                                  - borrow later used here

error: aborting due to previous error

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

warning: casting to the same type is unnecessary (`i32` -> `i32`)
 --> src/main.rs:3:14
  |
3 |     let b = &(a as i32);
  |              ^^^^^^^^^^ help: try: `a`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
  = note: requested on the command line with `-W clippy::unnecessary-cast`

I expected to see this happen:

Version

rustc 1.67.0-nightly (c5d82ed7a 2022-11-19)
binary: rustc
commit-hash: c5d82ed7a4ad94a538bb87e5016e7d5ce0bd434b
commit-date: 2022-11-19
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions