Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idempotency and correctness problem when using multiple if statements as list comprehension #2055

Closed
1 of 3 tasks
OkkeHendriks opened this issue Feb 1, 2022 · 1 comment · Fixed by #2061
Closed
1 of 3 tasks

Comments

@OkkeHendriks
Copy link
Contributor

OkkeHendriks commented Feb 1, 2022

Issue created from fantomas-online

Original code

let wrong = [
        if false then 1
        if true then 2
    ]

When executed:

> let wrong = [
        if false then 1
        if true then 2
    ];;
val wrong: int list = [2]

Formatted code

let wrong = [ if false then 1; if true then 2 ]

When executed:

> let wrong = [ if false then 1; if true then 2 ];;
val wrong: int list = []

Reformatted code

let wrong =
    [ if false then
          1
          if true then 2 ]

When executed:

> let wrong =
    [ if false then
          1
          if true then 2 ];;
val wrong: int list = []

Problem description

  • Fantomas broke the correctness of my code when first formatting the code.
  • Fantomas was not able to produce the same code after reformatting the result.

Extra information

  • The formatted result breaks by code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this.

Options

Fantomas master branch at 1/1/1990

Default Fantomas configuration

@nojaf
Copy link
Contributor

nojaf commented Feb 1, 2022

Hello, thank you for reporting this issue.

I understand what is going on here, after the first format the second IfThenElse is merged into a Sequential of the elseExpr.
To resolve this, we could extend the current check in CodePrinter:

if List.exists isIfThenElseWithYieldReturn xs
|| List.forall isSynExprLambda xs then
multilineExpression ctx

Changing List.forall isSynExprLambda to also include IfThenElse.

Are you interested in submitting a PR for this?

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

Successfully merging a pull request may close this issue.

2 participants