Skip to content

Add error_format_without_sources lint#16772

Draft
sruggier wants to merge 1 commit intorust-lang:masterfrom
sruggier:pr/add-error_format_without_sources-lint
Draft

Add error_format_without_sources lint#16772
sruggier wants to merge 1 commit intorust-lang:masterfrom
sruggier:pr/add-error_format_without_sources-lint

Conversation

@sruggier
Copy link
Copy Markdown

@sruggier sruggier commented Mar 27, 2026

This PR adds a new lint that triggers whenever a formatting operation (Display or Debug) is directly invoked on an instance of an std::error::Error type that may provide cause information. It ignores Error types that don't provide a non-default implementation of the source function.

To catch bad uses of logging related macros, I chose to make the lint trigger on code within proc macros. Because of that, the implementation isn't able to use FormatArgsStorage unless FormatArgsCollector is updated to collect instances from inside proc macro expansions.

I'm opening this in a draft state to see if there's openness to updating FormatArgsCollector, and solicit feedback in general. For what it's worth, I've used the lint in a large codebase, and it works quite well, so I think it's worth adding in some form.

Fixes #10076.

Review checklist:

  • Followed lint naming conventions
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally (dogfood fails because of a few warnings from the new lint)
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

My own checklist of possible improvements:

  • Use a more elegant name
  • Trigger on generic code
  • Trigger on &dyn Error, Box<&dyn Error>, etc.
  • Add a pair of configuration options allow use of Display or Debug on specific types
  • Add separate configuration options to conditionally allow based on whether alternate flag is used (to implement within proc macros, requires either FormatArgsStorage, or parsing of encoded template byte array, which is painful)
  • Add a configuration option listing specific proc macros to ignore, as an escape hatch for bad code emitted from proc macros
  • options to configure suggestions, to enable automated fixes for large codebases
  • ignore empty enum variants, because they cannot have a linked source (possibly too magical, and would require some heroics, if possible at all)

changelog: [error_format_without_sources]: new lint added

@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Mar 27, 2026
@github-actions
Copy link
Copy Markdown

Lintcheck changes for 3f0913a

Lint Added Removed Changed
clippy::error_format_without_sources 72 0 0

This comment will be updated if you push new changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use of Display impl of an implementor of std::error::Error

2 participants