Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::init_numbered_fields: used a field initializer for a tuple struct
What it meansChecks for tuple structs initialized with field syntax. It will however not lint if a base initializer is present. The lint will also ignore code in macros. It is in the style group and warns by default.
What you see
warning: used a field initializer for a tuple struct --> main.rs:5:9 | 5 | let _ = TupleStruct { | _________^ 6 | | 0: 1, 7 | | 1: 23, 8 | | }; | |_^ help: use tuple initialization: `TupleStruct(1, 23)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#init_numbered_fields = note: `#[warn(clippy::init_numbered_fields)]` on by default
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?
This may be confusing to the uninitiated and adds no benefit as opposed to tuple initializers
Code that works but is not idiomatic Rust. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
struct TupleStruct(u8, u16);
let _ = TupleStruct {
0: 1,
1: 23,
};
// should be written as
let base = TupleStruct(1, 23);
// This is OK however
let _ = TupleStruct { 0: 42, ..base };
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::init_numbered_fields)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
init_numbered_fields = "allow"One run
cargo clippy -- -A clippy::init-numbered-fieldsThe [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 tuple structs initialized with field syntax. It will however not lint if a base initializer is present. The lint will also ignore code in macros.
Why is this bad?
This may be confusing to the uninitiated and adds no benefit as opposed to tuple initializers
Example
struct TupleStruct(u8, u16);
let _ = TupleStruct {
0: 1,
1: 23,
};
// should be written as
let base = TupleStruct(1, 23);
// This is OK however
let _ = TupleStruct { 0: 42, ..base };
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- needless_returnunneeded `return` statement
- new_without_defaultChecks for public types with a pub fn new() -> Self method and no implementation of Default
- len_zeroChecks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable
- inconsistent_digit_groupingdigits grouped inconsistently by underscores
- infallible_destructuring_matchyou seem to be trying to use `match` to destructure a single infallible pattern
- inherent_to_stringimplementation of inherent method `to_string(&self) -> String` for type `main::A`
- into_iter_on_refChecks for into_iter calls on references which should be replaced by iter or iter_mut
- io_other_errorthis can be `std::io::Error::other(_)`
- is_digit_ascii_radixuse of `char::is_digit` with literal radix of 10
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::needless_returnunneeded `return` statementRead it next →Did this answer your question?