Fix: unused_variables warnings were silently suppressed#448
Open
usmanovbf wants to merge 1 commit intodtolnay:masterfrom
Open
Fix: unused_variables warnings were silently suppressed#448usmanovbf wants to merge 1 commit intodtolnay:masterfrom
unused_variables warnings were silently suppressed#448usmanovbf wants to merge 1 commit intodtolnay:masterfrom
Conversation
Author
|
@dtolnay there are test in tests/test_display.rs, e.g. thiserror/tests/test_display.rs Lines 28 to 37 in 99e8a6c I propose to introduce some migrational period for developers who use thiserror to get rid of the unused variable errors from their code, e.g. for 6 months. And for now it should be enabled by special feature, e.g. features = ["unused_variables"]If you agree, I will supress the errors in tests/test_display.rs by enabling this feature by default in order to make tests green and throwing a warning with the migration period disclaimer for developers |
…sed_variables suppression
76ad105 to
c3fbdfb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes #446, resolving the issue where
unused_variableswarnings were silently suppressed for fields in structs and enums derivingthiserror::Error.The Problem
Previously, the
Displaytrait implementation generated by the macro unpacked all fields of a given variant and applied a global#[allow(unused_variables)]directive to the entirematchblock. While this successfully prevented compiler warnings for fields not referenced in the#[error("...")]format string, it had the unintended side effect of masking legitimate dead code and unused variable warnings within the user's own data structures.The Solution
Instead of relying on a blanket
allowdirective, this PR implements selective variable binding:impl/src/fmt.rs), the macro now scans the format string and its arguments to track exactly which fields are referenced (including.fieldand.0syntax). These are collected in a newused_field_namesset.matcharm patterns (impl/src/expand.rs), fields that are present inused_field_namesare bound to their normal identifiers. Fields that are not referenced in the format string are bound to the wildcard pattern (e.g.,field_name: _for named fields, or simply_for unnamed fields)._, theunused_variablesexception has been safely removed from the#[allow(...)]attribute over thematchblock.As a result, the Rust compiler's standard static analysis is restored for the user's types.
Testing
Added a UI test (
tests/ui/unused_variables.rs) with#![deny(unused_variables)]. This ensures viatrybuildthat the compiler correctly emits errors for unused fields, safeguarding against future regressions of this behavior.Thank you for your time reviewing this!