Clippy lints / Pedantic
Clippy lint · Pedantic · checked with Clippy 0.1.99
clippy::missing_fields_in_debug: manual `Debug` impl does not include all fields
What it meansChecks for manual core::fmt::Debug implementations that do not use all fields. It is in the pedantic group and off by default.
What you see
warning: manual `Debug` impl does not include all fields --> main.rs:9:1 | 9 | / impl fmt::Debug for Foo { 10 | | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { 11 | | formatter 12 | | .debug_struct("Foo") ... | 16 | | } | |_^ | note: this field is unused --> main.rs:7:5 | 7 | hidden_data: i32 | ^^^^^^^^^^^^^^^^ = help: consider including all fields in this `Debug` impl = help: consider calling `.finish_non_exhaustive()` if you intend to ignore fields = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#missing_fields_in_debug = note: `-W clippy::missing-fields-in-debug` implied by `-W clippy::pedantic`
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?
A common mistake is to forget to update manual Debug implementations when adding a new field to a struct or a new variant to an enum. At the same time, it also acts as a style lint to suggest using core::fmt::DebugStruct::finish_non_exhaustive for the times when the user intentionally wants to leave out certain fields (e.g. to hide implementation details).
Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
use std::fmt;
struct Foo {
data: String,
// implementation detail
hidden_data: i32
}
impl fmt::Debug for Foo {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Foo")
.field("data", &self.data)
.finish()
}
}
Write this instead
use std::fmt;
struct Foo {
data: String,
// implementation detail
hidden_data: i32
}
impl fmt::Debug for Foo {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Foo")
.field("data", &self.data)
.finish_non_exhaustive()
}
}
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]
missing_fields_in_debug = "warn"In code
#![warn(clippy::missing_fields_in_debug)]One run
cargo clippy -- -W clippy::missing-fields-in-debugThe [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 manual core::fmt::Debug implementations that do not use all fields.
Why is this bad?
A common mistake is to forget to update manual Debug implementations when adding a new field
to a struct or a new variant to an enum.
At the same time, it also acts as a style lint to suggest using core::fmt::DebugStruct::finish_non_exhaustive
for the times when the user intentionally wants to leave out certain fields (e.g. to hide implementation details).
Known problems
This lint works based on the DebugStruct helper types provided by the Formatter,
so this won’t detect Debug impls that use the write! macro.
Oftentimes there is more logic to a Debug impl if it uses write! macro, so it tries
to be on the conservative side and not lint in those cases in an attempt to prevent false positives.
This lint also does not look through function calls, so calling a function does not consider fields
used inside of that function as used by the Debug impl.
Lastly, it also ignores tuple structs as their DebugTuple formatter does not have a finish_non_exhaustive
method, as well as enums because their exhaustiveness is already checked by the compiler when matching on the enum,
making it much less likely to accidentally forget to update the Debug impl when adding a new variant.
Example
use std::fmt;
struct Foo {
data: String,
// implementation detail
hidden_data: i32
}
impl fmt::Debug for Foo {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Foo")
.field("data", &self.data)
.finish()
}
}
Use instead:
use std::fmt;
struct Foo {
data: String,
// implementation detail
hidden_data: i32
}
impl fmt::Debug for Foo {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Foo")
.field("data", &self.data)
.finish_non_exhaustive()
}
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_linesChecks for functions with a large amount of lines
- missing_errors_docChecks the doc comments of publicly visible functions that return a Result type and warns if there is no # Errors section
- must_use_candidateChecks for public functions that have no #[must_use] attribute, but return something not already marked must-use, have no mutable arg and mutate no statics
- maybe_infinite_iterChecks for iteration that may be infinite
- mismatching_type_param_order`Foo` has a similarly named generic type parameter `B` in its declaration, but in a different order
- missing_panics_docChecks the doc comments of publicly visible functions that may panic and warns if there is no # Panics section
- mut_mutmultiple successive mutable borrows
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_linesChecks for functions with a large amount of linesRead it next →Did this answer your question?