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

clippy::explicit_auto_deref incorrectly removes deref in referenced deref #12969

Closed
wr7 opened this issue Jun 20, 2024 · 0 comments · Fixed by #12976
Closed

clippy::explicit_auto_deref incorrectly removes deref in referenced deref #12969

wr7 opened this issue Jun 20, 2024 · 0 comments · Fixed by #12976
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

@wr7
Copy link

wr7 commented Jun 20, 2024

Summary

clippy::explicit_auto_deref falsely triggers in expressions like &*foo. Its suggestion does not compile.

In this specific case, clippy::explicit_auto_deref should not trigger because clippy::needless_borrow already triggers and provides the correct suggestion.

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 wr7 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jun 20, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jun 20, 2024
@wr7 wr7 changed the title clippy::explicit_auto_deref incorrectly removes deref when ampersand is present clippy::explicit_auto_deref incorrectly removes deref in referenced deref Jul 2, 2024
@bors bors closed this as completed in 0f4035f Jul 3, 2024
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.

2 participants