Skip to content

Incorrect flagging of clippy::trait_duplication_in_bounds with "bounds in associated type position" feature #12927

@Wodann

Description

@Wodann

Summary

Rust 1.79 released the "bounds in associated type position" feature. clippy is flagging a clippy::trait_duplication_in_bounds lint for code that seemingly does not duplicate code for all possible instantiations of the generics.

Reproducer

I tried this code (simplified):

pub trait ChainSpec:
    Debug
    + RpcSpec<
        RpcBlock<<Self as RpcSpec>::RpcTransaction>: EthRpcBlock + IntoRemoteBlock<Self>,
        RpcTransaction: EthRpcTransaction,
    > + RpcSpec<RpcBlock<B256>: EthRpcBlock>
{
    type SignedTransaction: Clone + Debug;
}

pub trait RpcSpec: Sized {
    type RpcBlock<Data>: serde::DeserializeOwned + serde::Serialize
    where
        Data: Default + serde::DeserializeOwned + serde::Serialize;

    type RpcTransaction: Default + serde::DeserializeOwned + serde::Serialize;
}

pub trait EthRpcBlock {
    /// Returns the root of the block's state trie.
    fn state_root(&self) -> &B256;

    /// Returns the total difficulty of the chain until this block for finalised
    /// blocks. For pending blocks, returns `None`.
    fn total_difficulty(&self) -> Option<&U256>;
}

I expected this to not result in a clippy linting error.

Instead, I received the following warning:

error: these bounds contain repeated elements
  --> crates/edr_evm/src/chain_spec.rs:13:5
   |
13 | /     Debug
14 | |     + alloy_rlp::Encodable
15 | |     + RpcSpec<
16 | |         RpcBlock<<Self as RpcSpec>::RpcTransaction>: EthRpcBlock + IntoRemoteBlock<Self>,
17 | |         RpcTransaction: EthRpcTransaction,
18 | |     > + RpcSpec<RpcBlock<B256>: EthRpcBlock>
   | |____________________________________________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trait_duplication_in_bounds
   = note: `-D clippy::trait-duplication-in-bounds` implied by `-D warnings`
   = help: to override `-D warnings` add `#[allow(clippy::trait_duplication_in_bounds)]`
help: try
   |
13 ~     Debug + alloy_rlp::Encodable + RpcSpec<
14 +         RpcBlock<<Self as RpcSpec>::RpcTransaction>: EthRpcBlock + IntoRemoteBlock<Self>,
15 +         RpcTransaction: EthRpcTransaction,
16 +     >
   |

It seems like the lint doesn't recognise that the two bounds for RpcSpec<A> and RpcSpec<B> have types where potentially A != B.

Version

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions