Skip to content

Lint missing fields in manual Debug implementations #10429

Closed
@i509VCB

Description

@i509VCB

What it does

This lint finds fields that implement Debug but are not printed in a struct's Debug implementation.

This lint would ignore fields in types that do not implement Debug.

Lint Name

debug_missing_field

Category

style, pedantic

Advantage

  • When adding a new field to a type that implements Debug manually, you may forget to update the Debug implementation. This lint would ensure new fields are linted if not printed in the debug implementation.

Drawbacks

  • Both tuple and struct debug implementations need to be considered.
  • Enums are a little more complicated to lint, since you would need check the fields of each variant.
  • The lint may have false positives if the Debug implementation uses manual write! invocations instead of debug_struct and debug_tuple.

Example

pub struct Example {
    non_debug: NonDebug,
    debug: u32,
    new: u16,
}

impl Debug for Example {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("Example").field("debug", &self.debug).finish_non_exhaustive()
    }
}

Could be written as:

pub struct Example {
    non_debug: NonDebug,
    debug: u32,
    new: u16,
}

impl Debug for Example {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("Example").field("debug", &self.debug).field("new", &self.new).finish_non_exhaustive()
    }
}

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions