Skip to content

Commit

Permalink
Parens for params
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Mar 15, 2023
1 parent 15538c7 commit 2064c89
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Expand Up @@ -777,8 +777,16 @@ self =>

/** Convert tree to formal parameter list. */
def convertToParams(tree: Tree): List[ValDef] = tree match {
case Parens(ts) => ts map convertToParam
case _ => List(convertToParam(tree))
case Parens(ts) => ts.map(convertToParam)
case Typed(Ident(_), _) =>
val msg = "parentheses are required around the parameter of a lambda"
val silencing = s"$msg (Or use '-Wconf:msg=$msg:s' to silence this warning)"
if (currentRun.isScala3 && settings.Wsource.value)
syntaxError(tree.pos.point, silencing)
else if (currentRun.isScala3 || settings.Wsource.value)
deprecationWarning(tree.pos.point, silencing, "2.13.11")
List(convertToParam(tree))
case _ => List(convertToParam(tree))
}

/** Convert tree to formal parameter. */
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/scala/tools/nsc/settings/Warnings.scala
Expand Up @@ -27,6 +27,8 @@ trait Warnings {
// Warning semantics.
val fatalWarnings = BooleanSetting("-Werror", "Fail the compilation if there are any warnings.") withAbbreviation "-Xfatal-warnings"

val Wsource = BooleanSetting("-Wsource", "Increase warnings/errors due to -Xsource.")

private val WconfDefault = List("cat=deprecation:ws", "cat=feature:ws", "cat=optimizer:ws")
// Note: user-defined settings are added on the right, but the value is reversed before
// it's parsed, so that later defined settings take precedence.
Expand Down
4 changes: 4 additions & 0 deletions test/files/neg/parens-for-params-3.check
@@ -0,0 +1,4 @@
parens-for-params-3.scala:5: error: parentheses are required around the parameter of a lambda (Or use '-Wconf:msg=parentheses are required around the parameter of a lambda:s' to silence this warning)
x: Int => x * 2
^
1 error
10 changes: 10 additions & 0 deletions test/files/neg/parens-for-params-3.scala
@@ -0,0 +1,10 @@
// scalac: -Werror -Wsource -Xsource:3

class C {
def f = {
x: Int => x * 2
}
def g = {
(x: Int) => x * 2
}
}
6 changes: 6 additions & 0 deletions test/files/neg/parens-for-params.check
@@ -0,0 +1,6 @@
parens-for-params.scala:5: warning: parentheses are required around the parameter of a lambda (Or use '-Wconf:msg=parentheses are required around the parameter of a lambda:s' to silence this warning)
x: Int => x * 2
^
error: No warnings can be incurred under -Werror.
1 warning
1 error
10 changes: 10 additions & 0 deletions test/files/neg/parens-for-params.scala
@@ -0,0 +1,10 @@
// scalac: -Werror -Wsource -Xlint

class C {
def f = {
x: Int => x * 2
}
def g = {
(x: Int) => x * 2
}
}

0 comments on commit 2064c89

Please sign in to comment.