Skip to content

Fix duration_suboptimal_units for small literals#16922

Open
tanndlin wants to merge 1 commit intorust-lang:masterfrom
tanndlin:duration_suboptimal_units
Open

Fix duration_suboptimal_units for small literals#16922
tanndlin wants to merge 1 commit intorust-lang:masterfrom
tanndlin:duration_suboptimal_units

Conversation

@tanndlin
Copy link
Copy Markdown

@tanndlin tanndlin commented Apr 27, 2026

fixes #16532

changelog: Fix [duration_suboptimal_units] false positive for small integer literals whose promoted value would be at most 10.

Combined #16565 and #16596

Copilot AI review requested due to automatic review settings April 27, 2026 22:19
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 27, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 27, 2026

r? @llogiq

rustbot has assigned @llogiq.
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: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates duration_suboptimal_units to avoid linting on small integer literal values where the promoted (larger-unit) value would be <= 10, addressing the false positive described in #16532.

Changes:

  • Adjusts lint logic to suppress suggestions for small integer literals while still linting expressions.
  • Updates lint documentation/examples to avoid the new suppression edge cases.
  • Expands UI tests (including duration_constructors coverage) to validate the new behavior and expected suggestions.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
clippy_lints/src/duration_suboptimal_units.rs Implements the <= 10 suppression rule for integer literals and updates lint docs/examples.
tests/ui/duration_suboptimal_units.rs Adds UI coverage for small literals vs expressions and updates expected lint sites.
tests/ui/duration_suboptimal_units.fixed Updates fixed-output expectations to match the new lint behavior.
tests/ui/duration_suboptimal_units.stderr Updates stderr expectations for the new/shifted diagnostics.
tests/ui/duration_suboptimal_units_days_weeks.rs Adds non-linting literal + linting expression cases under duration_constructors.
tests/ui/duration_suboptimal_units_days_weeks.fixed Updates fixed-output expectations for the days/weeks test.
tests/ui/duration_suboptimal_units_days_weeks.stderr Updates stderr expectations for the days/weeks test.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

View changes since this review

Comment thread tests/ui/duration_suboptimal_units.rs Outdated
Comment thread clippy_lints/src/duration_suboptimal_units.rs Outdated
Comment thread tests/ui/duration_suboptimal_units.rs Outdated
Comment thread tests/ui/duration_suboptimal_units.fixed Outdated
Comment thread tests/ui/duration_suboptimal_units.rs
Copy link
Copy Markdown
Contributor

@ada4a ada4a left a comment

Choose a reason for hiding this comment

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

Thank you for picking this up:)

View changes since this review

Comment thread clippy_lints/src/duration_suboptimal_units.rs
Comment thread clippy_lints/src/duration_suboptimal_units.rs Outdated
Comment thread tests/ui/duration_suboptimal_units.rs Outdated
Comment thread tests/ui/duration_suboptimal_units.rs Outdated
Comment thread tests/ui/duration_suboptimal_units.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 4, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 4, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@tanndlin
Copy link
Copy Markdown
Author

tanndlin commented May 4, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 4, 2026
@ada4a
Copy link
Copy Markdown
Contributor

ada4a commented May 6, 2026

Could you please squash down the commits? I think it would also be nice to add @Hectonight and @veeceey as co-authors to the commit

@tanndlin tanndlin force-pushed the duration_suboptimal_units branch from 3c536bd to a1ffdf3 Compare May 6, 2026 21:33
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 6, 2026

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.

@tanndlin
Copy link
Copy Markdown
Author

tanndlin commented May 6, 2026

Could you please squash down the commits? I think it would also be nice to add @Hectonight and @veeceey as co-authors to the commit

Absolutely! It's squashed with all 3 of us on the commit now

Copy link
Copy Markdown
Contributor

@ada4a ada4a left a comment

Choose a reason for hiding this comment

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

Sorry, one last thing^^ Feel free to force-push

View changes since this review

Comment on lines 9 to 15
let dur = Duration::from_secs(60);
let dur = Duration::from_secs(61 - 1);
//~^ duration_suboptimal_units

let dur = Duration::from_hours(24);
let dur = Duration::from_hours(12 * 2);
//~^ duration_suboptimal_units
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.

Instead of using these imo awkward expressions, could you please use literals that are large enough to trigger the lint? I think it'd make sense to use 6000 and 2400, respectively, here, as those are clear multiples of the minimal values that the lint fires on (60 and 24, respectively). You could then move the value-too-small cases into fn issue16532() { } again, but it's fine to not bother to...

Copy link
Copy Markdown
Author

@tanndlin tanndlin May 6, 2026

Choose a reason for hiding this comment

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

lol i never even realized the original commit touches this file. Are we cool with deleting this file?

Instead of using these imo awkward expressions, could you please use literals that are large enough to trigger the lint?

I do know part of the intent of these test cases is that it only triggers for literals, not expressions, because an expression would likely show intent that you are trying to use a bigger unit. I'll add some that are quite as awkward

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.

Are we cool with deleting this file?

The reason it's there is to test the currently unstable constructors Duration::from_{days,weeks}. Granted, I'm not sure why it also has the test for minutes (the first one highlighted) – maybe it's just a sanity check to ensure that the logic for the unstable constructors doesn't mess up the logic for the stable ones.

So I'd say let's leave it be^^

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 6, 2026
Co-authored-by: tanndlin <tanndlin@gmail.com>
Co-authored-by: veeceey <varun_6april@hotmail.com>
@tanndlin tanndlin force-pushed the duration_suboptimal_units branch from a1ffdf3 to da72354 Compare May 7, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

duration_suboptimal_units should not trigger on small values

6 participants