Skip to content

Add error_format_without_sources lint#16772

Open
sruggier wants to merge 1 commit into
rust-lang:masterfrom
sruggier:pr/add-error_format_without_sources-lint
Open

Add error_format_without_sources lint#16772
sruggier wants to merge 1 commit into
rust-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

github-actions Bot commented Mar 27, 2026

Lintcheck changes for 049b98b

Lint Added Removed Changed
clippy::error_format_without_sources 72 0 0

This comment will be updated if you push new changes

@sruggier sruggier marked this pull request as ready for review March 31, 2026 15:33
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 31, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 31, 2026

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@sruggier sruggier force-pushed the pr/add-error_format_without_sources-lint branch from 3f0913a to 049b98b Compare May 11, 2026 21:56
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 11, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@sruggier
Copy link
Copy Markdown
Author

@rustbot reroll

@rustbot rustbot assigned samueltardieu and unassigned llogiq May 11, 2026
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 S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants