Skip to content

Fix overflowing_literals lint with repeated negation#158302

Open
theemathas wants to merge 4 commits into
rust-lang:mainfrom
theemathas:neg-lit-lint
Open

Fix overflowing_literals lint with repeated negation#158302
theemathas wants to merge 4 commits into
rust-lang:mainfrom
theemathas:neg-lit-lint

Conversation

@theemathas

Copy link
Copy Markdown
Contributor

Fixes #158295

This PR consists of: two commits with non-functional cleanups, a commit that adds a test, and a commit that actually changes the behavior. The following description concerns the changed behavior.


The overflowing_literal lint is, I believe, supposed to consider the literal and not the surrounding context when deciding whether to lint. This is in contrast to the arithmetic_overflow lint, which only considers code that executes at run time, which excludes the negation inside a literal.

According to the reference, a negation in front of an integer does not result in an overflow. I think of this as: a negation, and only one negation, in front of an integer, is "part of" the integer. Therefore, the overflowing_literal lint should consider that when linting. It should not consider the second or third negation.

Therefore, this PR changes overflowing_literals so that it only lints on 128_i8 when there is no preceding negation at all. This is in contrast to the previous behavior, which lints on 128_i8 preceded by an even number of negations.

This better matches how the argument is actually used.
See `rustc_hir::intravisit::{walk_expr,walk_pat_expr}`.
Fixes rust-lang#158295

The `overflowing_literal` lint is, I believe, supposed to consider the
literal and not the surrounding context when deciding whether to lint.
This is in contrast to the `arithmetic_overflow` lint, which only
considers code that executes at run time, which excludes the negation
inside a literal.

According to [the reference](https://doc.rust-lang.org/1.96.0/reference/expressions/operator-expr.html#r-expr.operator.int-overflow.unary-neg),
a negation in front of an integer does not result in an overflow.
I think of this as: a negation, and only one negation, in front of
an integer, is "part of" the integer. Therefore,
the `overflowing_literal` lint should consider that when linting.
It should not consider the second or third negation.

Therefore, this commit changes `overflowing_literals` so that it only
lints on `128_i8` when there is no preceding negation at all. This is
in contrast to the previous behavior, which lints on `128_i8` preceded
by an even number of negations.
@theemathas theemathas added T-lang Relevant to the language team needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Jun 23, 2026
@rustbot

rustbot commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. labels Jun 23, 2026
@rustbot

rustbot commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

r? @adwinwhite

rustbot has assigned @adwinwhite.
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 73 candidates
  • Random selection from 22 candidates

@theemathas theemathas added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

overflowing_literals lint behaves strangely with repeated negation

3 participants