Skip to content

New lint: iter_missing_exact_size#17395

Open
DanielEScherzer wants to merge 5 commits into
rust-lang:masterfrom
DanielEScherzer:suggest-ExactSizeIterator
Open

New lint: iter_missing_exact_size#17395
DanielEScherzer wants to merge 5 commits into
rust-lang:masterfrom
DanielEScherzer:suggest-ExactSizeIterator

Conversation

@DanielEScherzer

@DanielEScherzer DanielEScherzer commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

View all comments

  • Followed lint naming conventions
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

Fixes #17393


changelog: [iter_missing_exact_size]: add lint

@rustbot rustbot added S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 10, 2026
@rustbot

This comment has been minimized.

@DanielEScherzer
DanielEScherzer force-pushed the suggest-ExactSizeIterator branch from e4c0d8c to 2b8d2b7 Compare July 10, 2026 02:48
@DanielEScherzer

DanielEScherzer commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Not exactly sure if the new name matches the guidelines, I didn't want the name to be too long, suggestions welcome

@rustbot label +A-lint

@rustbot

rustbot commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

Error: Unknown labels: A-lints

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

@rustbot rustbot added the A-lint Area: New lints label Jul 10, 2026
@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown

Lintcheck changes for cfb5ade

Lint Added Removed Changed
clippy::iter_missing_exact_size 6 0 0

This comment will be updated if you push new changes

@DanielEScherzer
DanielEScherzer force-pushed the suggest-ExactSizeIterator branch from 143523a to a9e1af0 Compare July 10, 2026 03:09
@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

So the first round of CI said that I should add size_hint to the symbols in clippy_utils/src/sym.rs, now the second round says to remove that symbol as unused?

// Remove symbols used as `sym::*`.
let used_re = Regex::new(r"\bsym::(\w+)\b").unwrap();
let rs_ext = OsStr::new("rs");
for dir in ["clippy_lints", "clippy_lints_internal", "clippy_utils", "src"] {
assumes that the clippy symbols are used as sym, I was using use clippy_utils::sym as clippy_sym; and use rustc_span::sym;, I'll switch them

Comment thread clippy_lints/src/iter_missing_exact_size.rs Outdated
Comment thread clippy_lints/src/iter_missing_exact_size.rs Outdated
@samueltardieu

samueltardieu commented Jul 10, 2026

Copy link
Copy Markdown
Member

So the first round of CI said that I should add size_hint to the symbols in clippy_utils/src/sym.rs, now the second round says to remove that symbol as unused?

// Remove symbols used as `sym::*`.
let used_re = Regex::new(r"\bsym::(\w+)\b").unwrap();
let rs_ext = OsStr::new("rs");
for dir in ["clippy_lints", "clippy_lints_internal", "clippy_utils", "src"] {

assumes that the clippy symbols are used as sym, I was using use clippy_utils::sym as clippy_sym; and use rustc_span::sym;, I'll switch them

All symbols of rustc_span::sym are reexported by clippy_utils::sym, using the latter should be enough.

@fpdotmonkey fpdotmonkey left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion for the name would be "no-delegated-exact-size-iter"

View changes since this review

Comment thread clippy_lints/src/iter_missing_exact_size.rs Outdated
span_lint_and_help(
cx,
ITER_MISSING_EXACT_SIZE,
lint_span,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lint_span,
item.span.into(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not have the label pointing to size_hint?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I wrote this because they bound a simple expression to a variable and just used it just once. I would think that the size hint would be good as the help span. Something like "size_hint is based on an ExactSizeIterator here"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjusted to have the size hint as a help span

@rustbot

This comment has been minimized.

@DanielEScherzer
DanielEScherzer force-pushed the suggest-ExactSizeIterator branch from 2dc95c6 to c864c24 Compare July 13, 2026 20:03
@rustbot

This comment has been minimized.

@DanielEScherzer
DanielEScherzer force-pushed the suggest-ExactSizeIterator branch from c864c24 to aaae638 Compare July 13, 2026 20:13
@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

My suggestion for the name would be "no-delegated-exact-size-iter"

View changes since this review

Is it possible to have the name be part of what is decided in the FCP?

@Gri-ffin

Copy link
Copy Markdown
Contributor

Is it possible to have the name be part of what is decided in the FCP?

Yes, feel free to join the discussion on Zulip (once the FCP starts).

I think this currently wouldn't fire if the user has

fn size_hint(&self) -> (usize, Option<usize>) {
    return self.range.size_hint();
}

If you can please add a test for this?

@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

Is it possible to have the name be part of what is decided in the FCP?

Yes, feel free to join the discussion on Zulip (once the FCP starts).

I think this currently wouldn't fire if the user has

fn size_hint(&self) -> (usize, Option<usize>) {
    return self.range.size_hint();
}

If you can please add a test for this?

I added support for function bodies where the only statement is a return (and optionally there might be a trailing expression that is dead code and is ignored)

@rustbot

This comment has been minimized.

@DanielEScherzer
DanielEScherzer force-pushed the suggest-ExactSizeIterator branch from eb066f0 to ca1028b Compare July 17, 2026 16:28
@rustbot

rustbot commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

Squashed and rebased
@Gri-ffin @fpdotmonkey @samueltardieu do you think this is ready for FCP?

@Gri-ffin Gri-ffin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gri-ffin @fpdotmonkey @samueltardieu do you think this is ready for FCP?

It's starting to look solid, but I'm not a maintainer, I'm just helping in the reviews, I'll leave that call to the maintainers.

View changes since this review

Comment thread clippy_lints/src/iter_missing_exact_size.rs Outdated
Comment thread clippy_lints/src/iter_missing_exact_size.rs Outdated
Comment thread clippy_lints/src/iter_missing_exact_size.rs Outdated
@fpdotmonkey

Copy link
Copy Markdown

Likewise, I am but a community reviewer. I think it looks good, though.

@DanielEScherzer

DanielEScherzer commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Okay, @Gri-ffin do you have any more feedback? If not I'll start the FCP

@Jarcho

Jarcho commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

@fpdotmonkey @Gri-ffin if you think the PR is ready then mark it as approved; that's what signals triagebot to assign a maintainer.

@DanielEScherzer The process for FCPs has a team member starting it. We could try and have it start once the the community review phase is done, but we'll need to see how the reviews play out first.

@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

@fpdotmonkey @Gri-ffin if you think the PR is ready then mark it as approved; that's what signals triagebot to assign a maintainer.

@DanielEScherzer The process for FCPs has a team member starting it. We could try and have it start once the the community review phase is done, but we'll need to see how the reviews play out first.

Oh, I thought I just needed to add lint-nominated label with triagebot, but I guess that isn't one of the labels that are listed under allow-unauthenticated, so nevermind

@Gri-ffin Gri-ffin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than these LGTM.

View changes since this review

Comment thread clippy_lints/src/iter_missing_exact_size.rs Outdated
Comment thread clippy_lints/src/iter_missing_exact_size.rs Outdated
Comment thread clippy_lints/src/iter_missing_exact_size.rs Outdated
Comment on lines +1 to +18
error: iterator can implement `ExactSizeIterator`
--> tests/ui/iter_missing_exact_size.rs:13:1
|
LL | / impl Iterator for StringRepeater1 {
LL | |
LL | | type Item = String;
LL | | fn next(&mut self) -> Option<Self::Item> {
... |
LL | | }
| |_^
|
help: this `size_hint()` implementation delegates to to the `size_hint()` of an `ExactSizeIterator`, so the overall iterator is likely to have an exact size
--> tests/ui/iter_missing_exact_size.rs:20:9
|
LL | self.range.size_hint()
| ^^^^^^^^^^^^^^^^^^^^^^
= note: `-D clippy::iter-missing-exact-size` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::iter_missing_exact_size)]`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally find this confusing but it's nothing currently blocking.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which part is confusing?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO if I see this suggestion I would assume I need to modify the impl block, which isn't the case, personally I would prefer to only highlight size_hint, but it's only a personal opinion, it can be discussed later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay. But since the issue is with the overall implementation (impl Iterator with a delegating size_hint but without also implementing ExactSizeIterator) I think pointing to the overall impl block is still helpful

Comment thread clippy_lints/src/iter_missing_exact_size.rs Outdated

@Gri-ffin Gri-ffin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

Likewise, I am but a community reviewer. I think it looks good, though.

Can you mark this as an approval in GitHub so that it counts as a community review and triggers the team review process?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-lint Area: New lints needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lint suggestion: implement ExactSizeIterator when size_hint() delegates to something implementing ExactSizeIterator

6 participants