Skip to content

explicit_auto_deref gives wrong advise for Boxed types #9127

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

Closed
asomers opened this issue Jul 5, 2022 · 2 comments
Closed

explicit_auto_deref gives wrong advise for Boxed types #9127

asomers opened this issue Jul 5, 2022 · 2 comments
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

@asomers
Copy link

asomers commented Jul 5, 2022

Summary

The explicit_auto_deref warning suggests a code change that does not compile when trying to get a mutable pointer to the inside of a Box.

Possibly related to #9123

Lint Name

explicit_auto_deref

Reproducer

I tried this code:

struct Foo { pub x: Box<u32> }

pub fn bar(_x: &mut u32) {unimplemented!()}

pub fn baz() {
    let mut foo = Foo{x: Box::new(42u32)};
    bar(&mut *foo.x);
}

I saw this happen:

warning: deref which would be done by auto-deref
 --> mockall/tests/explicit_auto_deref_box.rs:7:14
  |
7 |     bar(&mut *foo.x);
  |              ^^^^^^ help: try this: `foo.x`
  |
  = note: `#[warn(clippy::explicit_auto_deref)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_auto_deref

But when I apply Clippy's advise I get this error:

error[E0308]: mismatched types
 --> mockall/tests/explicit_auto_deref_box.rs:7:9
  |
7 |     bar(foo.x);
  |     --- ^^^^^
  |     |   |
  |     |   expected `&mut u32`, found struct `std::boxed::Box`
  |     |   help: consider mutably borrowing here: `&mut foo.x`
  |     arguments to this function are incorrect
  |
  = note: expected mutable reference `&mut u32`
                        found struct `std::boxed::Box<u32>`

I expected to see this happen:
No error.

Version

rustc 1.64.0-nightly (27eb6d701 2022-07-04)
binary: rustc
commit-hash: 27eb6d7018e397cf98d51c205e3576951d766323
commit-date: 2022-07-04
host: x86_64-unknown-freebsd
release: 1.64.0-nightly
LLVM version: 14.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

@asomers asomers 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 Jul 5, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jul 5, 2022
asomers added a commit to asomers/mockall that referenced this issue Jul 5, 2022
* clippy::explicit_auto_deref (but workaround clippy bug 9127)
* allow dead_code (in the tests)
* Remove some unneeded dead code in the tests

rust-lang/rust-clippy#9127
@Jarcho
Copy link
Contributor

Jarcho commented Jul 6, 2022

The suggestion is to use bar(&mut foo.x)

@asomers
Copy link
Author

asomers commented Jul 6, 2022

Ahh, I'm stupid. I'm just so used to seeing this warning flag expressions like &*xxx that I didn't read this one closely enough.

@asomers asomers closed this as completed Jul 6, 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

No branches or pull requests

3 participants