Skip to content

trait_duplication_in_bounds false positive for repeated From bound #9626

@jedel1043

Description

@jedel1043

Summary

Another false positive for trait_duplication_in_bounds, but now for repeated From clauses with different generic arguments.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=20909581fd02460b126ebe30d969ba51

Lint Name

trait_duplication_in_bounds

Reproducer

I tried this code:

#![deny(clippy::trait_duplication_in_bounds)]

pub struct JSInternedStrRef<'a, 'b> {
    utf8: Option<&'a str>,
    utf16: &'b [u16],
}

impl<'a, 'b> JSInternedStrRef<'a, 'b> {
    pub fn into_common<C>(self, prioritize_utf8: bool) -> C
    where
        C: From<&'a str> + From<&'b [u16]>,
    {
        if let Some(str) = self.utf8 {
            return C::from(str);
        }
        C::from(self.utf16)
    }
}

I saw this happen:

error: these where clauses contain repeated elements
  --> src/lib.rs:11:12
   |
11 |         C: From<&'a str> + From<&'b [u16]>,
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `From<&'a str>`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trait_duplication_in_bounds
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
1  | #![deny(clippy::trait_duplication_in_bounds)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I expected to see this happen:

Compilation without errors.

Following the suggestion causes:

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
  --> src/lib.rs:16:17
   |
16 |         C::from(self.utf16)
   |         ------- ^^^^^^^^^^ expected `str`, found slice `[u16]`
   |         |
   |         arguments to this function are incorrect
   |
   = note: expected reference `&'a str`
              found reference `&'b [u16]`
note: associated function defined here

Version

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-unknown-linux-gnu
release: 1.64.0
LLVM version: 14.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

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 haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions