Skip to content

Lint against iterator functions that panics when N is zero #153563

Open
Urgau wants to merge 3 commits intorust-lang:mainfrom
Urgau:lint-panics-when-n-is-zero
Open

Lint against iterator functions that panics when N is zero #153563
Urgau wants to merge 3 commits intorust-lang:mainfrom
Urgau:lint-panics-when-n-is-zero

Conversation

@Urgau
Copy link
Member

@Urgau Urgau commented Mar 8, 2026

This PR adds new kind of variant to the deny-by-default unconditional_panic lint, by linting on iterator functions that panics when N (chunks/windows size) is zero.

error: this operation will panic at runtime
  --> $DIR/const-n-is-zero.rs:11:13
   |
LL |     let _ = s.array_windows::<0>();
   |             ^^^^^^^^^^^^^^^^^^^^^^ const parameter `N` is zero
   |
   = note: `#[deny(unconditional_panic)]` on by default

cc @rust-lang/libs-api

@rustbot
Copy link
Collaborator

rustbot commented Mar 8, 2026

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @vakaras

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Mar 8, 2026
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 8, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 8, 2026

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 16 candidates

@Urgau Urgau force-pushed the lint-panics-when-n-is-zero branch from ff233d2 to 44380ea Compare March 8, 2026 12:25
@RalfJung
Copy link
Member

RalfJung commented Mar 8, 2026

This PR adds new kind of variant to the deny-by-default unconditional_panic lint, by linting on iterator functions that panics when N (chunks/windows size) is zero.

This seems to do much more than that. It adds a new language primitive, a new kind of built-in assertion. Could you motivate that?

@Urgau
Copy link
Member Author

Urgau commented Mar 8, 2026

This seems to do much more than that. It adds a new language primitive, a new kind of built-in assertion. Could you motivate that?

The code is unfortunately tangled, it doesn't add a new language primitive per se, but report_assert_as_lint requires a AssertKind variant even if it's just for linting in one place.

I could probably modify that function so it doesn't take an AssertKind, but I wasn't sure about that.

@RalfJung
Copy link
Member

RalfJung commented Mar 8, 2026

Given that AssertKind is part of the MIR syntax, I feel very strongly that we shouldn't modify it for non-MIR/opsem purposes.

@Urgau Urgau force-pushed the lint-panics-when-n-is-zero branch from 44380ea to 36de202 Compare March 8, 2026 13:23
@Urgau
Copy link
Member Author

Urgau commented Mar 8, 2026

Understandable, I've reworked the PR to avoid touching at AssertKind.

Copy link
Member

@JohnTitor JohnTitor left a comment

Choose a reason for hiding this comment

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

I love the idea but it should better for someone more familiar with this topic other than me to review. @RalfJung Could you take over? Otherwise I'd reroll.

View changes since this review

@RalfJung
Copy link
Member

RalfJung commented Mar 9, 2026

I'm afraid not, I don't have capacity at the moment.

@rustbot reroll

@rustbot rustbot assigned fee1-dead and unassigned JohnTitor Mar 9, 2026
@rust-bors

This comment has been minimized.

@Urgau Urgau force-pushed the lint-panics-when-n-is-zero branch from 36de202 to 0a8cacf Compare March 12, 2026 06:47
@rustbot
Copy link
Collaborator

rustbot commented Mar 12, 2026

This PR was rebased onto a different main 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.

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants