Skip to content

Commit 62270fe

Browse files
committed
scalafmt: enable RedundantBraces rewrite
1 parent d76cb7c commit 62270fe

File tree

83 files changed

+678
-918
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+678
-918
lines changed

.scalafmt.conf

+5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ newlines {
1919
}
2020
rewrite {
2121
rules = [
22+
RedundantBraces,
2223
RedundantParens,
2324
SortModifiers,
2425
]
26+
redundantBraces {
27+
ifElseExpressions = true
28+
stringInterpolation = true
29+
}
2530
sortModifiers.preset = styleGuide
2631
}
2732
# Disabled in default since this operation is potentially

build.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ val ReleaseCandidate = s"($V-RC\\d+).*".r
235235
val Milestone = s"($V-M\\d+).*".r
236236

237237
lazy val stableVersion = Def
238-
.setting { (ThisBuild / version).value.replaceAll("\\+.*", "") }
238+
.setting((ThisBuild / version).value.replaceAll("\\+.*", ""))
239239

240240
lazy val buildInfoSettings: Seq[Def.Setting[_]] = Seq(
241241
buildInfoKeys := Seq[BuildInfoKey](

project/Mima.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import com.typesafe.tools.mima.core._
22

33
object Mima {
4-
val ignoredABIProblems: Seq[ProblemFilter] = {
4+
val ignoredABIProblems: Seq[ProblemFilter] =
55
// After v0.5, start running mima checks in CI and document breaking changes here.
66
// See https://github.com/typesafehub/migration-manager/wiki/sbt-plugin#basic-usage
77
Seq(
@@ -12,5 +12,4 @@ object Mima {
1212
ProblemFilters.exclude[Problem]("org.scalafmt.rewrite.*"),
1313
ProblemFilters.exclude[Problem]("org.scalafmt.util.*")
1414
)
15-
}
1615
}

readme/src/main/scala/org/scalafmt/readme/Readme.scala

+12-19
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ object Readme {
6565
hl.scala(result)
6666
}
6767

68-
def config(frags: Frag*) = { cliFlags(frags.render) }
68+
def config(frags: Frag*) = cliFlags(frags.render)
6969
def cliFlags(flags: String) = {
7070
Config.fromHoconString(flags).get
7171
hl.scala(flags)
@@ -118,18 +118,14 @@ object Readme {
118118
def configurationBlock(
119119
style: ScalafmtConfig,
120120
collapsed: Boolean = false
121-
): TypedTag[String] = {
122-
div(
123-
span(
124-
"Show/hide configuration used for this example",
125-
`class` := "scalafmt-configuration-toggle"
126-
),
127-
pre(changedConfig(style)),
128-
`class` := {
129-
"scalafmt-configuration" + (if (collapsed) " collapsed" else "")
130-
}
131-
)
132-
}
121+
): TypedTag[String] = div(
122+
span(
123+
"Show/hide configuration used for this example",
124+
`class` := "scalafmt-configuration-toggle"
125+
),
126+
pre(changedConfig(style)),
127+
`class` := "scalafmt-configuration" + (if (collapsed) " collapsed" else "")
128+
)
133129

134130
def fullWidthDemo(style: ScalafmtConfig)(code: String): TypedTag[String] = {
135131
val formatted = Scalafmt.format(code, style).get
@@ -148,9 +144,8 @@ object Readme {
148144
div(sideBySide(code, formatted), configurationBlock(style))
149145
}
150146

151-
def example(code: String): TypedTag[String] = {
147+
def example(code: String): TypedTag[String] =
152148
example(code, ScalafmtConfig.default40)
153-
}
154149

155150
def exampleAlign(code: String): TypedTag[String] = {
156151
val formatted = Scalafmt.format(
@@ -270,9 +265,8 @@ object Readme {
270265
def lastUpdated = new SimpleDateFormat("MMM d, y")
271266
.format(new Date(Versions.timestamp.toLong))
272267

273-
def format(code: String): TypedTag[String] = {
268+
def format(code: String): TypedTag[String] =
274269
format(ScalafmtConfig.default)(code)
275-
}
276270

277271
val alignNone = ScalafmtConfig.default.copy(align = Align.none)
278272
val alignSome = ScalafmtConfig.default.copy(align = Align.some)
@@ -283,9 +277,8 @@ object Readme {
283277
.copy(align = Align.default.copy(arrowEnumeratorGenerator = true))
284278
val alignModuleId = ScalafmtConfig.defaultWithAlign
285279

286-
def format(style: ScalafmtConfig)(code: String): TypedTag[String] = {
280+
def format(style: ScalafmtConfig)(code: String): TypedTag[String] =
287281
example(code, style.copy(runner = ScalafmtRunner.sbt))
288-
}
289282

290283
def example(code: String, style: ScalafmtConfig): TypedTag[String] = {
291284
val formatted = Scalafmt.format(code, style).get

scalafmt-benchmarks/src/main/scala-2.12/benchmarks/MacroBenchmark.scala

+9-15
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ abstract class MacroBenchmark(parallel: Boolean, maxFiles: Int)
2727
override def toString = s"${this.getClass.getName}(parallel=$parallel)"
2828

2929
@Setup
30-
def setup(): Unit = {
31-
files = {
32-
val x = Corpus.files(Corpus.fastparse.copy(
33-
// TODO(olafur) remove once testkit 1.7 is out
34-
url = Corpus.fastparse.url.replace("olafurpg", "scalameta")
35-
)).filter { f => f.projectUrl.contains("scala-js") }.take(maxFiles)
36-
.map(_.read).toBuffer
37-
if (parallel) x.par else x
38-
}
30+
def setup(): Unit = files = {
31+
val x = Corpus.files(Corpus.fastparse.copy(
32+
// TODO(olafur) remove once testkit 1.7 is out
33+
url = Corpus.fastparse.url.replace("olafurpg", "scalameta")
34+
)).filter(f => f.projectUrl.contains("scala-js")).take(maxFiles).map(_.read)
35+
.toBuffer
36+
if (parallel) x.par else x
3937
}
4038

4139
def testMe(): Unit = {
@@ -44,14 +42,10 @@ abstract class MacroBenchmark(parallel: Boolean, maxFiles: Int)
4442
}
4543

4644
@Benchmark
47-
def scalafmt(): Unit = {
48-
files.foreach { file => Try(Scalafmt.format(file)) }
49-
}
45+
def scalafmt(): Unit = files.foreach(file => Try(Scalafmt.format(file)))
5046

5147
@Benchmark
52-
def scalafmt_rewrite(): Unit = {
53-
files.foreach { file => Try(formatRewrite(file)) }
54-
}
48+
def scalafmt_rewrite(): Unit = files.foreach(file => Try(formatRewrite(file)))
5549

5650
}
5751

scalafmt-benchmarks/src/main/scala/org/scalafmt/benchmarks/FormatBenchmark.scala

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import org.scalafmt.config.{RewriteSettings, ScalafmtConfig}
55
import org.scalafmt.rewrite.{RedundantBraces, SortImports}
66

77
trait FormatBenchmark {
8-
def formatRewrite(code: String): String = {
9-
Scalafmt.formatCode(
10-
code,
11-
baseStyle = ScalafmtConfig.default
12-
.copy(rewrite = RewriteSettings(rules = Seq(SortImports, RedundantBraces)))
13-
).get
14-
}
8+
def formatRewrite(code: String): String = Scalafmt.formatCode(
9+
code,
10+
baseStyle = ScalafmtConfig.default
11+
.copy(rewrite = RewriteSettings(rules = Seq(SortImports, RedundantBraces)))
12+
).get
1513
}

scalafmt-benchmarks/src/main/scala/org/scalafmt/benchmarks/MicroBenchmark.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class MicroBenchmark(path: String*) extends FormatBenchmark {
2626
var code: String = _
2727

2828
@Setup
29-
def setup(): Unit = { code = FileOps.readFile(getPath) }
29+
def setup(): Unit = code = FileOps.readFile(getPath)
3030

3131
def getPath: Path = {
3232
val filename = FileOps.getFile(Seq("src", "resources") ++ path)
@@ -42,10 +42,10 @@ abstract class MicroBenchmark(path: String*) extends FormatBenchmark {
4242
}
4343

4444
@Benchmark
45-
def scalafmt(): String = { Scalafmt.format(code).get }
45+
def scalafmt(): String = Scalafmt.format(code).get
4646

4747
@Benchmark
48-
def scalafmt_rewrite(): String = { formatRewrite(code) }
48+
def scalafmt_rewrite(): String = formatRewrite(code)
4949

5050
def testMe(): Unit = {
5151
setup()

scalafmt-cli/src/main/scala/org/scalafmt/cli/Cli.scala

+13-20
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,16 @@ object Cli {
3434
nGContext.exit(exit.code)
3535
}
3636

37-
private def throwIfError(exit: ExitCode): Unit = {
38-
if (exit != ExitCode.Ok) {
39-
throw new RuntimeException(exit.toString) with NoStackTrace
40-
}
41-
}
37+
private def throwIfError(exit: ExitCode): Unit = if (exit != ExitCode.Ok)
38+
throw new RuntimeException(exit.toString) with NoStackTrace
4239

4340
def main(args: Array[String]): Unit = {
4441
val exit = mainWithOptions(args, CliOptions.default)
4542
sys.exit(exit.code)
4643
}
4744

48-
def exceptionThrowingMain(args: Array[String]): Unit = {
45+
def exceptionThrowingMain(args: Array[String]): Unit =
4946
exceptionThrowingMainWithOptions(args, CliOptions.default)
50-
}
5147

5248
def exceptionThrowingMainWithOptions(
5349
args: Array[String],
@@ -57,12 +53,11 @@ object Cli {
5753
throwIfError(exit)
5854
}
5955

60-
def mainWithOptions(args: Array[String], options: CliOptions): ExitCode = {
56+
def mainWithOptions(args: Array[String], options: CliOptions): ExitCode =
6157
getConfig(args, options) match {
6258
case Some(x) => run(x)
6359
case None => ExitCode.CommandLineArgumentError
6460
}
65-
}
6661

6762
def getConfig(args: Array[String], init: CliOptions): Option[CliOptions] = {
6863
val expandedArguments = expandArguments(args)
@@ -81,14 +76,13 @@ object Cli {
8176
builder.result()
8277
}
8378

84-
private[cli] def run(options: CliOptions): ExitCode = {
79+
private[cli] def run(options: CliOptions): ExitCode =
8580
findRunner(options) match {
8681
case Left(message) =>
8782
options.common.err.println(message)
8883
ExitCode.UnsupportedVersion
8984
case Right(runner) => runWithRunner(options, runner)
9085
}
91-
}
9286

9387
private val isNativeImage: Boolean = "true" ==
9488
System.getProperty("scalafmt.native-image", "false")
@@ -125,7 +119,7 @@ object Cli {
125119
|""".stripMargin
126120
)
127121
} {
128-
case Left(error) => Left(s"error: invalid configuration: ${error}")
122+
case Left(error) => Left(s"error: invalid configuration: $error")
129123
case Right(`stableVersion`) =>
130124
options.common.debug.println(s"Using core runner [$stableVersion]")
131125
Right(ScalafmtCoreRunner)
@@ -162,20 +156,19 @@ object Cli {
162156

163157
val exit = runner.run(options, termDisplayMessage)
164158

165-
if (options.writeMode == WriteMode.Test) {
166-
if (exit.isOk) {
167-
options.common.out.println("All files are formatted with scalafmt :)")
168-
} else if (exit.is(ExitCode.TestError)) {
159+
if (options.writeMode == WriteMode.Test)
160+
if (exit.isOk) options.common.out
161+
.println("All files are formatted with scalafmt :)")
162+
else if (exit.is(ExitCode.TestError)) {
169163
options.common.out.println("error: --test failed")
170164
options.onTestFailure.foreach(options.common.out.println)
171-
} else { options.common.out.println(s"error: $exit") }
172-
}
165+
} else options.common.out.println(s"error: $exit")
173166
if (
174167
options.writeMode == WriteMode.Test && !options.fatalWarnings &&
175168
exit.is(ExitCode.ParseError)
176-
) {
169+
)
177170
// Ignore parse errors etc.
178171
ExitCode.Ok
179-
} else { exit }
172+
else exit
180173
}
181174
}

scalafmt-cli/src/main/scala/org/scalafmt/cli/CliArgParser.scala

+5-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import scopt.OptionParser
88

99
object CliArgParser {
1010

11-
implicit val readPath: scopt.Read[Path] = scopt.Read.reads { Paths.get(_) }
11+
implicit val readPath: scopt.Read[Path] = scopt.Read.reads(Paths.get(_))
1212

1313
val usageExamples: String =
1414
"""|scalafmt # Format all files in the current project, configuration is determined in this order:
@@ -40,9 +40,8 @@ object CliArgParser {
4040
c
4141
}
4242

43-
private def readConfig(contents: String, c: CliOptions): CliOptions = {
44-
c.copy(configStr = Some(contents))
45-
}
43+
private def readConfig(contents: String, c: CliOptions): CliOptions = c
44+
.copy(configStr = Some(contents))
4645

4746
head("scalafmt", Versions.nightly)
4847
opt[Unit]('h', "help").action(printAndExit(includeUsage = true))
@@ -114,7 +113,7 @@ object CliArgParser {
114113
s"""|Format files listed in `git diff` against given git ref.
115114
|Deprecated: use --mode diff-ref=<ref> instead""".stripMargin
116115
)
117-
opt[Unit]("build-info").action((_, _) => { println(buildInfo); sys.exit })
116+
opt[Unit]("build-info").action { (_, _) => println(buildInfo); sys.exit }
118117
.text("prints build information")
119118
opt[Unit]("quiet").action((_, c) => c.copy(quiet = true))
120119
.text("don't print out stuff to console.")
@@ -148,7 +147,7 @@ object CliArgParser {
148147
|build time: ${new Date(Versions.timestamp.toLong)}""".stripMargin
149148

150149
private def writeMode(c: CliOptions, writeMode: WriteMode): CliOptions = c
151-
.writeModeOpt.fold { c.copy(writeModeOpt = Some(writeMode)) } { x =>
150+
.writeModeOpt.fold(c.copy(writeModeOpt = Some(writeMode))) { x =>
152151
if (x != writeMode)
153152
throw new Conflict(s"writeMode changing from $x to $writeMode")
154153
c

scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ case class CliOptions(
132132

133133
private[cli] def getProposedConfigFile: Path = canonicalConfigFile
134134
.flatMap(_.toOption)
135-
.getOrElse { gitOps.getProposedConfigFile(cwd, config).path }
135+
.getOrElse(gitOps.getProposedConfigFile(cwd, config).path)
136136

137137
/** Parse the scalafmt configuration and try to encode it to `ScalafmtConfig`.
138138
* If `--config-str` is specified, this will parse the configuration string

scalafmt-cli/src/main/scala/org/scalafmt/cli/ExitCode.scala

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,20 @@ object ExitCode {
3232
: ExitCode = generateExitStatus
3333
// format: on
3434
lazy val all: List[ExitCode] = allInternal.toList
35-
private def codeToName(code: Int): String = {
35+
private def codeToName(code: Int): String =
3636
if (code == 0) Ok.name
3737
else {
3838
val names = all
3939
.collect { case exit if (exit.code & code) != 0 => exit.name }
4040
names.mkString("+")
4141
}
42-
}
43-
def apply(code: Int): ExitCode = {
42+
def apply(code: Int): ExitCode =
4443
if (cache.contains(code)) cache.get(code)
4544
else {
4645
val result = new ExitCode(code, codeToName(code)) {}
4746
cache.put(code, result)
4847
result
4948
}
50-
}
5149

5250
def merge(exit1: ExitCode, exit2: ExitCode): ExitCode =
5351
apply(exit1.code | exit2.code)

scalafmt-cli/src/main/scala/org/scalafmt/cli/InputMethod.scala

+4-7
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ sealed abstract class InputMethod {
4343
object InputMethod {
4444

4545
object StdinCode {
46-
def apply(assumeFilename: String, inputStream: InputStream): StdinCode = {
46+
def apply(assumeFilename: String, inputStream: InputStream): StdinCode =
4747
StdinCode
4848
.apply(assumeFilename, Source.fromInputStream(inputStream).mkString)
49-
}
5049
}
5150
case class StdinCode(filename: String, input: String) extends InputMethod {
5251
override def path: Path = Paths.get(filename)
@@ -92,10 +91,8 @@ object InputMethod {
9291
val b = jList(revised, original.isEmpty || noEol(original.last))
9392
val diff = difflib.DiffUtils.diff(a, b)
9493
if (diff.getDeltas.isEmpty) ""
95-
else {
96-
difflib.DiffUtils
97-
.generateUnifiedDiff(s"a$filename", s"b$filename", a, diff, 1)
98-
.iterator().asScala.mkString("\n")
99-
}
94+
else difflib.DiffUtils
95+
.generateUnifiedDiff(s"a$filename", s"b$filename", a, diff, 1).iterator()
96+
.asScala.mkString("\n")
10097
}
10198
}

0 commit comments

Comments
 (0)