Fix closure with block body followed by method call (issue #4050)#6900
Open
tao501 wants to merge 1 commit into
Open
Fix closure with block body followed by method call (issue #4050)#6900tao501 wants to merge 1 commit into
tao501 wants to merge 1 commit into
Conversation
538e2f9 to
5192d69
Compare
…4050) In src/chains.rs should_add_parens(), closures with block bodies were not getting parentheses when followed by method calls, causing rustfmt to change program semantics. This adds ast::ExprKind::Block(..) => true to the closure match to ensure block-bodied closures get proper parenthesization.
f8d7e01 to
ae40c17
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
Fix for rustfmt issue #4050: "Rustfmt changes meaning of closure, also not idempotent"
Problem
When a closure with a block body is followed by a method call (e.g.,
|| { ... }().to_string()), rustfmt incorrectly formats it as:This changes the program's semantics from
((|| {...}())..to_string())to|| {({...}()..to_string());}.Root Cause
In
src/chains.rs, theshould_add_parens()function returnsfalsefor closures with block bodies, so the chain formatter doesn't add the necessary parentheses around the closure call before the method call.Fix
Added
ast::ExprKind::Block(..) => trueto the closure match inshould_add_parens(), ensuring that closures with block bodies get parenthesized when followed by method calls.This produces the correct output:
Closes #4050