Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::default_numeric_fallback: default numeric fallback might occur
What it meansChecks for usage of unconstrained numeric literals which may cause default numeric fallback in type inference. Default numeric fallback means that if numeric types have not yet been bound to concrete types at the end of type inference, then integer type is bound to i32, and similarly floating type is bound to f64. See RFC0212 for more information about the fallback. It is in the restriction group and off by default.
What you see
warning: default numeric fallback might occur --> main.rs:3:9 | 3 | let i = 10; | ^^ help: consider adding suffix: `10_i32` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#default_numeric_fallback = note: requested on the command line with `-W clippy::default-numeric-fallback`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
To ensure that every numeric type is chosen explicitly rather than implicitly.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let i = 10;
let f = 1.23;
Write this instead
let i = 10_i32;
let f = 1.23_f64;
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
default_numeric_fallback = "warn"In code
#![warn(clippy::default_numeric_fallback)]One run
cargo clippy -- -W clippy::default-numeric-fallbackThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type inference.
Default numeric fallback means that if numeric types have not yet been bound to concrete
types at the end of type inference, then integer type is bound to i32, and similarly
floating type is bound to f64.
See RFC0212 for more information about the fallback.
Why restrict this?
To ensure that every numeric type is chosen explicitly rather than implicitly.
Known problems
This lint is implemented using a custom algorithm independent of rustc’s inference, which results in many false positives and false negatives.
Example
let i = 10;
let f = 1.23;
Use instead:
let i = 10_i32;
let f = 1.23_f64;
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- create_dircalling `std::fs::create_dir` where there may be a better way
- dbg_macroChecks for usage of the dbg! macro
- decimal_literal_representationWarns if there is a better representation for a numeric literal
- default_union_representationthis union has the default representation
- definition_in_module_rootChecks for definitions (structs, functions, traits, etc.) in mod.rs files
- deref_by_slicingslicing when dereferencing would work
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?