Skip to content
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

unnecessary_cast causes borrowcheck problems #9906

Closed
matthiaskrgr opened this issue Nov 20, 2022 · 0 comments · Fixed by #9996
Closed

unnecessary_cast causes borrowcheck problems #9906

matthiaskrgr opened this issue Nov 20, 2022 · 0 comments · Fixed by #9996
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

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

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 20, 2022
@bors bors closed this as completed in 846c9b8 Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant