Skip to content

New lint: bitwise_not_zero, rewrite !0 as {int}::MAX#16806

Open
nik-rev wants to merge 1 commit intorust-lang:masterfrom
nik-contrib:bitwise-not-on-zero
Open

New lint: bitwise_not_zero, rewrite !0 as {int}::MAX#16806
nik-rev wants to merge 1 commit intorust-lang:masterfrom
nik-contrib:bitwise-not-on-zero

Conversation

@nik-rev
Copy link
Copy Markdown

@nik-rev nik-rev commented Apr 3, 2026

This lint suggests to rewrite expressions like:

bar_length.unwrap_or(!0)

As:

bar_length.unwrap_or(u32::MAX)

Because !0 is confusing, it performs the bitwise NOT operator, equivalent to specifying the MAX value.


changelog: [bitwise_new_lint]: added lint that replaces !0 with{int}::MAX

@rustbot rustbot added 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 Apr 3, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 3, 2026

r? @samueltardieu

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

@nik-rev nik-rev force-pushed the bitwise-not-on-zero branch 2 times, most recently from df6f69d to beb9d9a Compare April 3, 2026 23:32
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 3, 2026

Lintcheck changes for 1acfd24

Lint Added Removed Changed
clippy::bitwise_not_zero 44 0 0

This comment will be updated if you push new changes

@nik-rev nik-rev force-pushed the bitwise-not-on-zero branch 2 times, most recently from 95ec375 to 299fac0 Compare April 3, 2026 23:52
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@nik-rev nik-rev force-pushed the bitwise-not-on-zero branch 2 times, most recently from 5c65746 to 5620a80 Compare April 3, 2026 23:53
@rustbot

This comment has been minimized.

@nik-rev nik-rev force-pushed the bitwise-not-on-zero branch 2 times, most recently from ec2a486 to 6769831 Compare April 3, 2026 23:55
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 3, 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.

"usage of the bitwise not `!` on zero",
"this is clearer written as the maximum value",
format!("{integer}::MAX"),
Applicability::MaybeIncorrect,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why MaybeIncorrect?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It suggests to rewrite with u32::MAX, but someone may not have that in scope. e.g. they might define struct u32;

Comment thread clippy_lints/src/operators/bitwise_not_zero.rs Outdated
Comment thread clippy_lints/src/operators/mod.rs Outdated
Comment thread clippy_lints/src/operators/numeric_arithmetic.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 Apr 5, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 5, 2026

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

@samueltardieu samueltardieu added the lint-nominated Create an FCP-thread on Zulip for this PR label Apr 5, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 5, 2026

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

Comment thread clippy_lints/src/operators/mod.rs Outdated
Comment thread tests/ui/bitwise_not_zero.rs Outdated
Comment thread tests/ui/unnecessary_min_or_max.rs Outdated
@nik-rev nik-rev force-pushed the bitwise-not-on-zero branch 3 times, most recently from 4a63587 to 469ad15 Compare April 5, 2026 18:20
@nik-rev nik-rev changed the title New lint: bitwise_not_zero, rewrite !0 as i32::MAX New lint: bitwise_not_zero, rewrite !0 as {int}::MAX Apr 5, 2026
@nik-rev nik-rev force-pushed the bitwise-not-on-zero branch 2 times, most recently from f9dd43d to 91d9717 Compare April 5, 2026 18:23
@nik-rev nik-rev force-pushed the bitwise-not-on-zero branch 4 times, most recently from b8c7588 to 441cc5d Compare April 5, 2026 18:35
Co-authored-by: Samuel Tardieu <sam@rfc1149.net>
Co-authored-by: Ada Alakbarova <58857108+ada4a@users.noreply.github.com>
@nik-rev nik-rev force-pushed the bitwise-not-on-zero branch from 441cc5d to 1acfd24 Compare April 5, 2026 18:46
@nik-rev
Copy link
Copy Markdown
Author

nik-rev commented Apr 6, 2026

@rustbot ready

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Apr 6, 2026
@nik-rev nik-rev requested a review from samueltardieu April 6, 2026 06:30
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lint-nominated Create an FCP-thread on Zulip for this PR 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants