Skip to content

Lint for unbounded iterations checking#17242

Draft
PLeVasseur wants to merge 1 commit into
rust-lang:masterfrom
PLeVasseur:feat/unbounded-iterations-check
Draft

Lint for unbounded iterations checking#17242
PLeVasseur wants to merge 1 commit into
rust-lang:masterfrom
PLeVasseur:feat/unbounded-iterations-check

Conversation

@PLeVasseur

Copy link
Copy Markdown
Contributor

Thank you for making Clippy better!

We're collecting our changelog from pull request descriptions.
If your PR only includes internal changes, you can just write
changelog: none. Otherwise, please write a short comment
explaining your change.

It's also helpful for us that the lint name is put within backticks (` `),
and then encapsulated by square brackets ([]), for example:

changelog: [`unbounded_iter`]: <still need to write>

If your PR fixes an issue, you can add fixes #issue_number into this
PR description. This way the issue will be automatically closed when
your PR is merged.

If you added a new lint, here's a checklist for things that will be
checked during review or continuous integration.

  • 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

Note that you can skip the above if you are just opening a WIP PR in
order to get feedback.

Delete this line and everything above before opening your PR.


Please write a short comment explaining your change (or "none" for internal only changes)

@llogiq last month at RustWeek 2026 was walking me through the "how" of writing a Clippy lint.

Life got a little busy, but he suggested that I open this as a WIP for now so that I can get a little feedback and then keep chipping away at it =]

changelog:

@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Jun 14, 2026

@llogiq llogiq 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.

This is a good start. I'd first concentrate on the test cases. Then adding code re the comments I left and then I'll do another round of review.

View changes since this review

ExprKind::Block(block, _) => block.expr.as_ref().map_or(Bounded, |e| is_unbounded(cx, e)),
ExprKind::AddrOf(BorrowKind::Ref, _, e) => is_unbounded(cx, e),
ExprKind::Struct(..) => higher::Range::hir(cx, expr).is_some_and(|r| r.end.is_some()).into(),
_ => Bounded,

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.

We should have a comment explaining that other expression kinds don't fit the lint, therefore we mark it as bounded to avoid fiurther calculation

is_unbounded(cx, receiver).and(is_unbounded(cx, &args[0]))
} else if method.ident.name == sym::chain {
is_unbounded(cx, receiver).or(is_unbounded(cx, &args[0]))
} else {

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.

flat_map and flatten should always be ``Unbounded`.

Also take should always be Bounded


fn main() {
(0..).all(|x| x > 0);
//~^ unbounded_iter

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.

We want a lot more test cases, both positive and negative ones.

For each case in our is_unbounded function I'd like to have at least one test case.

I'd also like to see method calls like .max(), .sum() and .collect() to see which part of the expression gets linted (we may want to have special handling for those).

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

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants