[Repo Assist] Stay multiline when list element is a tuple with lambda as last field (#3278)#3280
Closed
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
…3278) When a list is collapsed to a single-line form (e.g. [a; b; c]), the items are separated by ";" which in this context means list separator. However, if a list element is a tuple whose last expression is a lambda or if/then/else, the lambda body will capture the ";" and subsequent list items as a sequence expression, changing the semantics of the code. Example: // Input (2-element list): let x = [ 1, fun () -> 1; 1, fun () -> 1 ] // The lambda body captures "; 1, fun () -> 1" -> list has 1 element! Fix: extend the alwaysMultiline check in genArrayOrList to detect when any non-last element is a tuple whose last sub-expression is a lambda or if/then/else. In that case, keep the list multiline so each element is on its own line (where whitespace-sensitivity prevents the capture). This follows the "stay multiline" design principle (Option B in #3279). Fixes #3278 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Gonna solve this in a larger refactor to have a more overall consistent story. |
28 tasks
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.
Closes #3278
🤖 This PR was created by Repo Assist, an automated AI assistant. Please review carefully.
Problem
When a list is collapsed to a single-line form (elements separated by
;), if any list element is a tuple whose last element is a lambda or if/then/else, the lambda body captures the;separator and subsequent list elements as a sequence expression — silently changing the semantics of the code.Example:
Verified:
Root Cause
In
genArrayOrList, thealwaysMultilineguard forces multiline layout when any element isif/then/else, or all elements are lambdas/ite. But1, fun () -> 1is anExpr.Tuple, not a bare lambda, so neither guard fires. The list passes the "fits on one line?" check and collapses — producing semantically incorrect code.Fix
Extend the
alwaysMultilinecheck with awouldSwallowNextListItemhelper: if any non-last element is a tuple whose last expression is a lambda or if/then/else, force multiline. In the multiline form, elements are on separate lines where whitespace-sensitivity scopes the lambda body correctly.This follows the "stay multiline" design principle (Option B in RFC #3279), which is the approach the community overwhelmingly voted for (16:1 as of the time of this PR).
The fix is minimal and surgical — it only affects lists where a non-last element would cause a semantic change when collapsed.
Test Status
dotnet build fantomas.sln— succeededdotnet test src/Fantomas.Core.Tests/— 2738 passed, 0 failed, 7 skippedlambda in non-last tuple position in list stays multiline to preserve semantics, 3278