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

Misleading error message on traits after #8229 #8306

Closed
HollayHorvath opened this issue Jan 18, 2022 · 2 comments · Fixed by #8315
Closed

Misleading error message on traits after #8229 #8306

HollayHorvath opened this issue Jan 18, 2022 · 2 comments · Fixed by #8315
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@HollayHorvath
Copy link

HollayHorvath commented Jan 18, 2022

Summary

I have a simple trait that extends the iterators (just like itertools does). This trait has Iterator bound, but my function also has Iterator in its Self argument (just a more precise one).

Before #8229 it was fine, now it hits trait_duplication_in_bounds (which is great!) but the error message is misleading and points to the function's where-clause, however removing that bound does not solve this issue.

Reproducer

I tried this code:

pub trait Iter: Iterator {
    fn into_group_btreemap<K, V>(self) -> BTreeMap<K, Vec<V>>
    where
        Self: Iterator<Item = (K, V)> + Sized,
        K: Ord + Eq,
    {
        into_group_btreemap(self)
    }
}

I expected to see this happen:
The lint should show that the trait bound is superfluous:

3 |     pub trait Iter: Iterator {
  |                     ^^^^^^^^

Instead, this happened:
The lint shows the real bound:

6 |         Self: Iterator<Item = (K, V)> + Sized,
  |               ^^^^^^^^^^^^^^^^^^^^^^^

Version

rustc 1.60.0-nightly (bd3cb5256 2022-01-16)
binary: rustc
commit-hash: bd3cb52565faab2755ff1bdb54d88bc91f47b4b9
commit-date: 2022-01-16
host: x86_64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0

Additional Labels

No response

@HollayHorvath HollayHorvath added the C-bug Category: Clippy is not doing the correct thing label Jan 18, 2022
@Jarcho
Copy link
Contributor

Jarcho commented Jan 18, 2022

Clippy should definitely not be suggesting to remove the super trait as it's not superfluous. It adds a constraint to the type implementing Iter which would otherwise not exist. The where clause is only restricting the default function implementation so other implementations of the trait are only bound by them if they do not provide an implementation. Any type constrained by Iter will be able to make use of any of Iterator's items. e.g.

fn f<I: Iter>(mut i: I) -> Option<I::Item>  {
    i.next()
}

Removing the Iterator bound on the trait would make the previous code fail to compile.

With that said this is a false positive. The bound Self: Iterator<Item = (K, V)> can not be expressed without duplicating the bound on the trait.

@dswij
Copy link
Member

dswij commented Jan 18, 2022

@rustbot claim

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants