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

@theemathas theemathas commented Jun 23, 2026

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. See the test changed in the fourth commit for code demonstrating the change in 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}`.
@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
@adwinwhite

Copy link
Copy Markdown
Contributor

I personally lean towards catching more overflows at compile-time if possible, and we can clarify this in reference instead?

Of course we should hear more opinions. Is starting FCP the right process for that?

@theemathas theemathas added the I-lang-nominated Nominated for discussion during a lang team meeting. label Jun 27, 2026
@theemathas

Copy link
Copy Markdown
Contributor Author

If the arithmetic_overflow and overflowing_literal lints are both enabled, then, in my testing, this PR doesn't change what code gets linted. It just changes which lints fire.

I've just noticed that the arithmetic_overflow lint appears to not be firing at all in the ui tests. I'm very confused on why this is happening.

In any case, I'm lang-nominating for opinions on when exactly these two lints should fire.

@theemathas

Copy link
Copy Markdown
Contributor Author

I figured out that the arithmetic_overflow lint wasn't firing in the tests because compiletest was doing the equivalent of cargo check. So, I edited the test to use --emit link (the equivalent of cargo build), so the lint would fire.

Hopefully it should be obvious from the tests now, that the same amount of overflows are still caught by lints.

@theemathas

Copy link
Copy Markdown
Contributor Author

Another thing I forgot to mention previously: The run time behavior of an allow(arithmetic_overflow) is a panic in debug mode. In contrast, the run time behavior of an allow(overflowing_literals) is seemingly supposed to be silently overflowing, even in debug mode. This PR makes the lint fire exactly when such silent overflows would otherwise happen.

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.
@traviscross traviscross added the P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang label Jul 1, 2026
@scottmcm

scottmcm commented Jul 1, 2026

Copy link
Copy Markdown
Member

I agree with @theemathas's analysis of this. In particular I think the

In contrast, the run time behavior of an allow(overflowing_literals) is seemingly supposed to be silently overflowing, even in debug mode.

note is a good one, and a particularly good reason for it to be deny-by-default and not warn-by-default. (Though it looks like both are currently deny-by-default.)

Not saying (indirectly) that --1 is "a literal" is definitely an improvement, in my mind.


So let's say "yes, for the lint we consider -1 a single literal (since otherwise there's no way to write -128_i8) but further negation is overflow, not part of the literal".

@rfcbot fcp merge lang

(Admittedly the whole thing is a bit weird, since of course -1.exp() isn't a negated literal, but that's not this lint's problem.)


One day I'll remember how to use the bot, sorry

@rust-rfcbot

rust-rfcbot commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

@scottmcm has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. and removed needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Jul 1, 2026
@rust-lang rust-lang deleted a comment from rustbot Jul 1, 2026
@traviscross

Copy link
Copy Markdown
Contributor

Makes sense to me.

@rfcbot reviewed

@traviscross traviscross added I-lang-radar Items that are on lang's radar and will need eventual work or consideration. and removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Jul 1, 2026
@tmandry

tmandry commented Jul 1, 2026

Copy link
Copy Markdown
Member

@rfcbot reviewed

(I believe this is a quality of implementation issue and doesn't need a lang FCP.)

@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jul 1, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

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

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. 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

7 participants