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

`trait_duplication_in_bounds` false positive for complex trait bounds

Open Licenser opened this issue 3 years ago • 2 comments

Summary

When having complex trait bounds trait_duplication_in_bounds warns incorrectly

Lint Name

trait_duplication_in_bounds

Reproducer

I tried this code:

fn parse_fields<'input, V>(total_idx: usize, mut input: &'input str) -> Result<(V, usize)>
where
    V: Value + Mutable + ValueAccess + Builder<'input> + 'input + std::fmt::Debug,
    <V as ValueAccess>::Key: From<Cow<'input, str>>,
    <V as ValueAccess>::Target: From<V>,
{
    let mut offset = 0;
    let mut res = V::object_with_capacity(16);
    loop {
        let (key, idx1) = parse_to(total_idx + offset, input, |c| c == '=')?;
        input = get_rest(input, idx1 + 1)?;
        offset += idx1 + 1;
        let (val, c, idx2): (V, _, _) = parse_value(total_idx + offset, input)?;
        input = get_rest(input, idx2 + 1)?;
        offset += idx2 + 1;

        match c {
            Some(',') => {
                cant_error!(res.insert(key, val));
            }
            Some(' ') | None => {
                cant_error!(res.insert(key, val));
                return Ok((res, offset));
            }
            _ => return Err(Error::InvalidFields(total_idx + offset)),
        };
    }
}

I saw this happen:

   --> tremor-influx/src/decoder.rs:202:30
    |
202 |     <V as ValueAccess>::Key: From<Cow<'input, str>>,
    |                              ^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: consider removing this trait bound
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trait_duplication_in_bounds

I expected to see this happen:

No error

Version

rustc 1.62.0 (a8314ef7d 2022-06-27)
binary: rustc
commit-hash: a8314ef7d0ec7b75c336af2c9857bfaf43002bfc
commit-date: 2022-06-27
host: x86_64-unknown-linux-gnu
release: 1.62.0
LLVM version: 14.0.5

Additional Labels

No response

Licenser avatar Jun 30 '22 17:06 Licenser

I have two more testcases for this from the itertools project:

pub mod one {
    #[derive(Clone, Debug)]
    struct MultiProductIter<I>
    where
        I: Iterator + Clone,
        I::Item: Clone,
    {
        _marker: I,
    }

    pub struct MultiProduct<I>(Vec<MultiProductIter<I>>)
    where
        I: Iterator + Clone,
        I::Item: Clone;

    pub fn multi_cartesian_product<H>(_: H) -> MultiProduct<<H::Item as IntoIterator>::IntoIter>
    where
        H: Iterator,
        H::Item: IntoIterator,
        <H::Item as IntoIterator>::IntoIter: Clone,
        <H::Item as IntoIterator>::Item: Clone,
    {
        todo!()
    }
}

pub mod two {
    use std::iter::Peekable;

    pub struct MergeBy<I, J, F>
    where
        I: Iterator,
        J: Iterator<Item = I::Item>,
    {
        _i: Peekable<I>,
        _j: Peekable<J>,
        _f: F,
    }

    impl<I, J, F> Clone for MergeBy<I, J, F>
    where
        I: Iterator,
        J: Iterator<Item = I::Item>,
        std::iter::Peekable<I>: Clone,
        std::iter::Peekable<J>: Clone,
        F: Clone,
    {
        fn clone(&self) -> Self {
            Self {
                _i: self._i.clone(),
                _j: self._j.clone(),
                _f: self._f.clone(),
            }
        }
    }
}
warning: this trait bound is already specified in the where clause
  --> src/main.rs:20:46
   |
20 |         <H::Item as IntoIterator>::IntoIter: Clone,
   |                                              ^^^^^
   |
   = note: requested on the command line with `-W clippy::trait-duplication-in-bounds`
   = help: consider removing this trait bound
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trait_duplication_in_bounds

warning: this trait bound is already specified in the where clause
  --> src/main.rs:44:33
   |
44 |         std::iter::Peekable<I>: Clone,
   |                                 ^^^^^
   |
   = help: consider removing this trait bound
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trait_duplication_in_bounds

hellow554 avatar Jul 07 '22 09:07 hellow554

Here's another test case if you want/need one; just came across this false positive today: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=9cddc1fe2df2b270406da614a3090de2

ilyvion avatar Jul 24 '22 17:07 ilyvion