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

explicit_auto_deref suggestion leads to non compiling code #9101

Closed
jonasbb opened this issue Jul 2, 2022 · 2 comments · Fixed by #9126
Closed

explicit_auto_deref suggestion leads to non compiling code #9101

jonasbb opened this issue Jul 2, 2022 · 2 comments · Fixed by #9126
Assignees
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

@jonasbb
Copy link

jonasbb commented Jul 2, 2022

Summary

The new explicit_auto_deref creates suggestions which do not compile. The lint already got ignored in serde serde-rs/serde@0ee71c7.

Lint Name

explicit_auto_deref

Reproducer

I tried this code:

use serde::Serializer;

fn serialize<S, const N: usize>(bytes: &&[u8; N], serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_bytes(*bytes)
}

Playground

I saw this happen:

warning: deref which would be done by auto-deref
 --> src/lib.rs:7:32
  |
7 |     serializer.serialize_bytes(*bytes)
  |                                ^^^^^^ help: try this: `bytes`
  |
  = 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

I expected to see this happen:

No lint should be emitted, since otherwise the function does not compile.

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
   --> src/lib.rs:7:32
    |
7   |     serializer.serialize_bytes(bytes)
    |                --------------- ^^^^^ expected slice `[u8]`, found `&[u8; N]`
    |                |
    |                arguments to this function are incorrect
    |
    = note: expected reference `&[u8]`
               found reference `&&[u8; N]`
note: associated function defined here
   --> /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.137/src/ser/mod.rs:737:8
    |
737 |     fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error>;
    |        ^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Version

Rust nightly clippy on the Playground.
clippy 0.1.64 (2022-07-01 46b8c23)

Build using the Nightly version: 1.64.0-nightly
(2022-07-01 46b8c23f3eb5e4d0e0aa)

Additional Labels

@rustbot label +I-suggestion-causes-error

@jonasbb jonasbb 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 2, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jul 2, 2022
jonasbb added a commit to jonasbb/serde_with that referenced this issue Jul 2, 2022
bors bot added a commit to jonasbb/serde_with that referenced this issue Jul 2, 2022
489: Suppress issue with `clippy::explicit_auto_deref` r=jonasbb a=jonasbb

rust-lang/rust-clippy#9101

bors r+

Co-authored-by: Jonas Bushart <[email protected]>
@tamaroning
Copy link
Contributor

@rustbot claim

@tamaroning
Copy link
Contributor

tamaroning commented Jul 3, 2022

According to RFC, I think clippy's suggestion is correct.
But rustc does not support all transitive type coercion for now (rust-lang/rust#18602)

In code below, changing line 3 to let _: &[u8] = a; is fine because of auto deref and unsized coercion but this change leads to type error.
&&[u8;2] -> &[u8;2] -> &[u8]

fn main() {
    let a: &&[u8; 2] = &&[1, 2];
    let _: &[u8] = *a;
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=b14623210347777dcdcfdbf8ae73a16a

MaxVerevkin added a commit to greshake/i3status-rust that referenced this issue Jul 7, 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 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.

3 participants