Skip to content

Fix duration_suboptimal_units false positive on small literal values#16596

Closed
veeceey wants to merge 1 commit into
rust-lang:masterfrom
veeceey:fix/issue-16532-duration-suboptimal-units
Closed

Fix duration_suboptimal_units false positive on small literal values#16596
veeceey wants to merge 1 commit into
rust-lang:masterfrom
veeceey:fix/issue-16532-duration-suboptimal-units

Conversation

@veeceey
Copy link
Copy Markdown

@veeceey veeceey commented Feb 20, 2026

Summary

Fixes #16532

For plain integer literals, suppress the duration_suboptimal_units lint when the promoted (converted) value would be <= 10. Small promoted values like from_millis(1_000) -> from_secs(1) are not necessarily more readable, and keeping the smaller unit makes quick adjustments easier (e.g. changing 1_000 to 1_200).

For expressions (e.g. from_secs(10 * 60)), always trigger the lint regardless of the promoted value, since the expression already signals intent to compute a converted value.

This follows the approach discussed in the issue.

Test plan

  • Updated tests/ui/duration_suboptimal_units.rs with test cases for literals with small/large promoted values and expressions
  • Updated tests/ui/duration_suboptimal_units_days_weeks.rs with analogous test cases
  • All compile tests pass: cargo test --test compile-test -- duration_suboptimal_units

changelog: [duration_suboptimal_units]: no longer triggers on small literal values where the promoted unit conversion result is <= 10

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 20, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 20, 2026

r? @Jarcho

rustbot has assigned @Jarcho.
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, samueltardieu

@rustbot

This comment has been minimized.

@veeceey veeceey force-pushed the fix/issue-16532-duration-suboptimal-units branch from dee229e to fc970c2 Compare February 20, 2026 08:45
@rustbot

This comment has been minimized.

@veeceey veeceey force-pushed the fix/issue-16532-duration-suboptimal-units branch from fc970c2 to 1f9e74b Compare February 20, 2026 09:17
@ada4a
Copy link
Copy Markdown
Contributor

ada4a commented Feb 20, 2026

It looks like there is already another PR for this: #16565. Do you want to collaborate with the it's author to decide between, or maybe combine, your approaches?

@rustbot

This comment has been minimized.

@veeceey
Copy link
Copy Markdown
Author

veeceey commented Feb 23, 2026

Thanks for flagging #16565! I didn't see that one when I started working on this. Happy to close this in favor of that PR if the approach there covers the same case, or we can coordinate if there are differences. Let me know what makes sense.

For plain integer literals, suppress the lint when the promoted value
would be <= 10, as the conversion is not necessarily more readable and
keeping the smaller unit makes quick adjustments easier (e.g. changing
`from_millis(1_000)` to `from_millis(1_200)` without also changing the
function name). Expressions like `10 * 60` continue to always trigger
the lint since they already signal intent to compute a converted value.
@veeceey veeceey force-pushed the fix/issue-16532-duration-suboptimal-units branch from 1f9e74b to 19c7555 Compare February 24, 2026 07:35
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 24, 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.

@ada4a
Copy link
Copy Markdown
Contributor

ada4a commented Feb 24, 2026

I think I prefer your comments in the code and in the tests, since they describe things in a bit more detail, but the test cases themselves look fine in both PRs. I'd say let's concentrate our efforts in the older PR

@veeceey
Copy link
Copy Markdown
Author

veeceey commented Feb 28, 2026

Thanks for the pointer — happy to consolidate efforts on #16565. That PR covers the same fix and I agree concentrating on one is better. Closing this in favor of that one.

@veeceey veeceey closed this Feb 28, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

duration_suboptimal_units should not trigger on small values

4 participants