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

Prefer std::iter::empty() to std::iter::Empty::default() #8915

Closed
adamchalmers opened this issue May 30, 2022 · 1 comment · Fixed by #8989
Closed

Prefer std::iter::empty() to std::iter::Empty::default() #8915

adamchalmers opened this issue May 30, 2022 · 1 comment · Fixed by #8989
Assignees
Labels
A-lint Area: New lints

Comments

@adamchalmers
Copy link

adamchalmers commented May 30, 2022

What it does

Replace std::iter::Empty::default() with std::iter::empty().

These two are equivalent, but one should be preferred: iter::empty requires learning less of the API than iter::Empty::default and it's also simpler.

I actually made this mistake consistently in a PR, it seems silly, but it happens! I caught this during code review and thought, hey, clippy should be able to detect that.

Lint Name

iter-empty-not-iter-empty-default

Category

style

Advantage

  • It's shorter
  • Rust generally discourages you from knowing the concrete type of an iterator combinator, e.g. you shouldn't really need to know Map or Empty, instead you should just call the map() and empty() functions. If you need to name the output type you should use impl Iterator instead.

Drawbacks

No response

Example

let x = std::iter::Empty::default();

Could be written as:

let x = std::iter::empty();
@adamchalmers adamchalmers added the A-lint Area: New lints label May 30, 2022
@kyoto7250
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants