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

Improper format of try / catch without braces in Scala3 #3653

Closed
fgoret opened this issue Oct 2, 2023 · 1 comment · Fixed by #3658
Closed

Improper format of try / catch without braces in Scala3 #3653

fgoret opened this issue Oct 2, 2023 · 1 comment · Fixed by #3658

Comments

@fgoret
Copy link

fgoret commented Oct 2, 2023

Configuration (required)

Please paste the contents of your .scalafmt.conf file here:

version = 3.7.14
runner.dialect = scala3

Command-line parameters (required)

When I run scalafmt via CLI like this: scalafmt foo.scala

Steps

Given try / catch without braces

//> using scala 3.3.1
@main def foo(): Unit =
  try
    val x = 3 / 0
  catch
    case e: Throwable => println(s"Caught: ${e.getMessage}")

Problem

Scalafmt removes the newlines / indent after try and catch:

//> using scala 3.3.1
@main def foo(): Unit =
  try val x = 3 / 0
  catch case e: Throwable => println(s"Caught: ${e.getMessage}")

This doesn't compile anymore.

Expectation

I would like the formatted output to be unchanged:

//> using scala 3.3.1
@main def foo(): Unit =
  try
    val x = 3 / 0
  catch
    case e: Throwable => println(s"Caught: ${e.getMessage}")

as this compiles and run fine.

@mtomko
Copy link
Contributor

mtomko commented Jan 11, 2024

I'm still seeing this in 3.7.15 and 3.7.17. My code looks like:

//> using scala 3.3.1
@main def foo(): Unit =
  try
    val x = 3 / 0
  catch case e: Throwable => println(s"Caught: ${e.getMessage}")
  finally 
    val _ = 5

And scalafmt wants to do this:

//> using scala 3.3.1
@main def foo(): Unit =
  try
    val x = 3 / 0
  catch case e: Throwable => println(s"Caught: ${e.getMessage}")
  finally val _ = 5

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

Successfully merging a pull request may close this issue.

2 participants