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

`explicit_auto_deref` gives wrong advice related to anyhow::Error

Open Swatinem opened this issue 2 years ago • 1 comments

Summary

Trying to cast an anyhow::Error to a std::error::Error via let stderr: &dyn std::error::Error = &*err; is being wrongly flagged. The provided suggestion does not work, neither does a straight-up borrow.

Lint Name

explicit_auto_deref

Reproducer

I tried this code:

let err = anyhow::Error::msg("oh hi");
let stderr: &dyn std::error::Error = &*err;

I saw this happen:

warning: deref which would be done by auto-deref
  --> crates/symbolicator/src/main.rs:47:43
   |
47 |     let stderr: &dyn std::error::Error = &*err;
   |                                           ^^^^ help: try this: `err`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_auto_deref

I expected to see this happen:

essentially nothing, as applying the suggestion does not compile:

  --> crates/symbolicator/src/main.rs:47:42
   |
47 |     let stderr: &dyn std::error::Error = err;
   |                 ----------------------   ^^^ expected reference, found struct `anyhow::Error`
   |                 |
   |                 expected due to this
   |
   = note: expected reference `&dyn std::error::Error`
                 found struct `anyhow::Error`

neither does using just a simple reference:

error[E0277]: the trait bound `anyhow::Error: std::error::Error` is not satisfied
  --> crates/symbolicator/src/main.rs:47:42
   |
47 |     let stderr: &dyn std::error::Error = &err;
   |                                          ^^^^ the trait `std::error::Error` is not implemented for `anyhow::Error`
   |
   = note: required for the cast from `anyhow::Error` to the object type `dyn std::error::Error`

Version

rustc 1.65.0-nightly (f03ce3096 2022-08-08)
binary: rustc
commit-hash: f03ce30962cf1b2a5158667eabae8bf6e8d1cb03
commit-date: 2022-08-08
host: x86_64-apple-darwin
release: 1.65.0-nightly
LLVM version: 14.0.6

Additional Labels

No response

Swatinem avatar Aug 09 '22 08:08 Swatinem

Fixed in #9126

Jarcho avatar Aug 09 '22 14:08 Jarcho

Seems like this should be closed.

Darksonn avatar Nov 20 '22 12:11 Darksonn