Skip to content

explicit_auto_deref overeager with Any #9143

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
ezrosent opened this issue Jul 10, 2022 · 0 comments · Fixed by #9126
Closed

explicit_auto_deref overeager with Any #9143

ezrosent opened this issue Jul 10, 2022 · 0 comments · Fixed by #9126
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

Comments

@ezrosent
Copy link

Summary

My read is that this is a separate bug to those fixed in #9126, apologies if it is actually the same.

Lint Name

explicit_auto_deref

Reproducer

I tried this code, which completes successfully:

use std::any::Any;
struct C(Box<dyn Any>);
impl C {
    fn new<T: Any>(t: T) -> C {
        C(Box::new(t))
    }
    fn as_any(&self) -> &dyn Any {
        &*self.0
    }
}
fn main() {
    assert_eq!(C::new(0usize).as_any().downcast_ref::<usize>().unwrap(), &0);
}

Clippy gives the following output

warning: deref which would be done by auto-deref
 --> src/main.rs:9:10
  |
9 |         &*self.0
  |          ^^^^^^^ help: try this: `self.0`
  |
  = 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 replacing as_any with the suggestion:

fn as_any(&self) -> &dyn Any {
    &self.0
}

Causes the program to fail (downcast_ref now returns None). This is (presumably) because self.0 has type Box<dyn Any>, which itself implements Any, so the suggested rewrite does not behave the same as the original code.

Version

rustc 1.64.0-nightly (1517f5de0 2022-07-07)
binary: rustc
commit-hash: 1517f5de01c445b5124b30f02257b02b4c5ef3b2
commit-date: 2022-07-07
host: aarch64-apple-darwin
release: 1.64.0-nightly
LLVM version: 14.0.6

Additional Labels

No response

@ezrosent ezrosent 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 10, 2022
bors added a commit that referenced this issue Aug 8, 2022
`explicit_auto_deref` changes

fixes #9123
fixes #9109
fixes #9143
fixes #9101

This avoid suggesting code which hits a rustc bug. Basically `&{x}` won't use auto-deref if the target type is `Sized`.

changelog: Don't suggest using auto deref for block expressions when the target type is `Sized`
changelog: Include the borrow in the suggestion for `explicit_auto_deref`
changelog: Don't lint `explicit_auto_deref` on `dyn Trait` return
changelog: Don't lint `explicit_auto_deref` when other adjustments are required
@bors bors closed this as completed in 4912c0e Aug 8, 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant