Skip to content

Lint missing fields in manual Debug implementations #10429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
i509VCB opened this issue Mar 1, 2023 · 1 comment · Fixed by #10616
Closed

Lint missing fields in manual Debug implementations #10429

i509VCB opened this issue Mar 1, 2023 · 1 comment · Fixed by #10616
Assignees
Labels
A-lint Area: New lints

Comments

@i509VCB
Copy link
Contributor

i509VCB commented Mar 1, 2023

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()
    }
}
@i509VCB i509VCB added the A-lint Area: New lints label Mar 1, 2023
@y21
Copy link
Member

y21 commented Apr 9, 2023

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants