rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`clippy::explicit_auto_deref` incorrectly removes deref when ampersand is present

Open wr7 opened this issue 2 months ago • 0 comments

Summary

clippy::explicit_auto_deref falsely triggers when an ampersand is present. Its suggestion does not compile.

This issue seems to be related to #9841

Lint Name

clippy::explicit_auto_deref

Reproducer

use std::ops::Deref;

struct Wrapper<T>(T);

impl<T> Deref for Wrapper<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}

fn foo(_bar: &str) {}

fn main() {
    let wrapped_bar = Wrapper("");

    foo(&*wrapped_bar);
}

cargo clippy --fix attempts to change foo(&*wrapped_bar) into foo(wrapped_bar) which does not compile. The following is the output given:

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[E0308]: mismatched types
  --> src/main.rs:18:9
   |
18 |     foo(wrapped_bar);
   |     --- ^^^^^^^^^^^ expected `&str`, found `Wrapper<&str>`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected reference `&str`
                 found struct `Wrapper<&str>`
note: function defined here
  --> src/main.rs:13:4
   |
13 | fn foo(_bar: &str) {}
   |    ^^^ ----------
help: consider borrowing here
   |
18 |     foo(&wrapped_bar);
   |         +

error: aborting due to 1 previous error

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

warning: this expression creates a reference which is immediately dereferenced by the compiler
  --> src/main.rs:18:9
   |
18 |     foo(&*wrapped_bar);
   |         ^^^^^^^^^^^^^ help: change this to: `*wrapped_bar`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
   = note: `#[warn(clippy::needless_borrow)]` on by default

warning: deref which would be done by auto-deref
  --> src/main.rs:18:9
   |
18 |     foo(&*wrapped_bar);
   |         ^^^^^^^^^^^^^ help: try: `wrapped_bar`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_auto_deref
   = note: `#[warn(clippy::explicit_auto_deref)]` on by default

warning: `bug_test` (bin "bug_test") generated 2 warnings (run `cargo clippy --fix --bin "bug_test"` to apply 2 suggestions)
warning: failed to automatically apply fixes suggested by rustc to crate `bug_test`

It seems that clippy::needless_borrow provides the correct suggestion which is to change the code to foo(*wrapped_bar) which does not give any warnings.

Version

No response

Additional Labels

@rustbot label +I-suggestion-causes-error

wr7 avatar Jun 20 '24 20:06 wr7