Skip to content

while_let_on_iterator suggested solution doesn't work in default implementations of traits without explicit Sized bound #16019

@mks-h

Description

@mks-h

Summary

The suggested solution for while_let_on_iterator fails to compile when applied in a default trait implementation that doesn't have an explicit Sized bound.

Reproducer

I tried this code:

pub trait CertainTrait: Iterator<Item = Result<u8, ()>> {
	fn parse(&mut self) {
		let mut data = Vec::new();
		while let Some(r) = self.next() {
			match r {
				Err(_) => {},
				Ok(v) => data.push(v),
			}
		}
	}
}

I expected to see this happen:
The suggestion should have possibly used self.into_iter(), as that works without additional requirements; Or it simply shouldn't have been suggested.

Instead, this happened:
The suggestion (for r in self.by_ref()) didn't compile, and the compiler suggested to add where Self: Sized to the function definition, which works.

Version

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=af6bab9b98cca79a75748f4f8963a7cb

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions