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

borrow_deref_ref breaks code #9905

Open
matthiaskrgr opened this issue Nov 20, 2022 · 1 comment
Open

borrow_deref_ref breaks code #9905

matthiaskrgr opened this issue Nov 20, 2022 · 1 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

Comments

@matthiaskrgr
Copy link
Member

Summary

.

Lint Name

borrow_deref_ref

Reproducer

I tried this code:

fn main() {
    let x: &'static str = "x";

    {
        let y = "y".to_string();
        let ref mut x = &*x;
        *x = &*y;
    }

    assert_eq!(x, "x");
}

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

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[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
 --> src/main.rs:6:13
  |
2 |     let x: &'static str = "x";
  |         - help: consider changing this to be mutable: `mut x`
...
6 |         let ref mut x = x;
  |             ^^^^^^^^^ cannot borrow as mutable

error[E0597]: `y` does not live long enough
 --> src/main.rs:7:16
  |
2 |     let x: &'static str = "x";
  |            ------------ type annotation requires that `y` is borrowed for `'static`
...
7 |         *x = &*y;
  |                ^ borrowed value does not live long enough
8 |     }
  |     - `y` dropped here while still borrowed

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0596, E0597.
For more information about an error, try `rustc --explain E0596`.
Original diagnostics will follow.

warning: deref on an immutable reference
 --> src/main.rs:6:25
  |
6 |         let ref mut x = &*x;
  |                         ^^^ help: if you would like to reborrow, try removing `&*`: `x`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_deref_ref
  = note: requested on the command line with `-W clippy::borrow-deref-ref`

warning: `clpy` (bin "clpy" test) generated 1 warning (run `cargo fix --bin "clpy" --tests` to apply 1 suggestion)
warning: `clpy` (bin "clpy") generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 0.61s

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
@roy-work
Copy link

I think I've hit another instance of this? We've the following type:

/// Abstracts over `stdio` and files.
#[derive(Debug)]
pub enum File {
  Stdio,
  File(fs::File),
}

Someone implemented the following on this type:

impl io::Read for &'_ File {
  fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
    match self {
      File::Stdio => io::stdin().read(buf),
      File::File(file) => (&*file).read(buf),
    }
  }
}

This triggers the clippy lint:

warning: deref on an immutable reference
    |
117 |       File::File(file) => (&*file).read(buf),
    |                           ^^^^^^^^ help: if you would like to reborrow, try removing `&*`: `file`

If you attempt to take Clippy's advice, however, and change the expression to file.read(buf), you get:

error[E0596]: cannot borrow `file` as mutable, as it is not declared as mutable
    |
117 |       File::File(file) => file.read(buf),
    |                           ^^^^^^^^^^^^^^ cannot borrow as mutable

I'm not sure how else to write this code except as it was originally written.

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

No branches or pull requests

2 participants