Skip to content

Fix FP in default_numeric_fallback when type is context-constrained#16908

Open
BootstrapperSBL wants to merge 1 commit intorust-lang:masterfrom
BootstrapperSBL:fix/default-numeric-fallback-context-resolved
Open

Fix FP in default_numeric_fallback when type is context-constrained#16908
BootstrapperSBL wants to merge 1 commit intorust-lang:masterfrom
BootstrapperSBL:fix/default-numeric-fallback-context-resolved

Conversation

@BootstrapperSBL
Copy link
Copy Markdown

@BootstrapperSBL BootstrapperSBL commented Apr 25, 2026

fixes #16745

clippy::default_numeric_fallback was flagging numeric literals whose resolved type happened to be i32 / f64, even when that type was forced by a typed neighbor in a binary expression rather than by fallback.

Before

#![warn(clippy::default_numeric_fallback)]
let a = 92i32;
let _ = a + 3;     // warns on `3` even though `a: i32` already pins the type
let b = 92f64;
let _ = b + 3.0;   // warns on `3.0` for the same reason

After

The above no longer warns. The lint now treats a binary operator (other than << / >>) whose sibling operand has a concrete numeric type from a non-(unsuffixed-literal) source as supplying an explicit type bound, the same way it already does for fn arguments and struct fields. The check propagates outer bounds downward, so transitively constrained literals such as the inner ones in a + 1 + 2 and (1 + 2) + a are also accepted.

Shifts are intentionally skipped because << and >> permit each side to have its own integer type, so a typed RHS does not constrain a literal LHS:

let a = 92i32;
let _ = 3 << a;   // still warns: type of `3` is not pinned by `a`

Cases where neither side carries explicit type information continue to warn (e.g. let _ = 1 + 2;).

changelog: [default_numeric_fallback]: do not warn on numeric literals whose type is constrained by a typed operand in a binary expression

Previously the lint flagged any unsuffixed numeric literal whose
resolved type was the fallback type (`i32` / `f64`) without checking
whether the type was actually picked by fallback. In `let _ = a + 3;`
where `a: i32`, the literal `3` is `i32` because the binary operator
forces both operands to share a type, not because of fallback, so no
warning should fire.

Handle `ExprKind::Binary` in the visitor: for operators that require
both operands to share a type (everything except shifts), if the
sibling operand has a concrete numeric type that came from a non-
(unsuffixed-literal) source, push an `ExplicitTyBound(true)` while
visiting the other operand. Outer bounds propagate downward so nested
arithmetic like `a + 1 + 2` is also recognised as constrained. Shifts
are intentionally skipped because `<<` and `>>` allow each side to
have its own integer type, so a typed RHS does not constrain the LHS.

fixes rust-lang#16745
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 25, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 25, 2026

r? @dswij

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

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 25, 2026

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@github-actions
Copy link
Copy Markdown

Lintcheck changes for 9ada8b9

Lint Added Removed Changed
clippy::default_numeric_fallback 0 789 20

This comment will be updated if you push new changes

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

Labels

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.

False Positive: default_numeric_fallback emits warning when the inferred type ends up being the fallback type.

3 participants