Skip to content

Commit

Permalink
Optionally warn if params should have parens
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Mar 16, 2023
1 parent 36da1b3 commit 99bfa0f
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 8 deletions.
13 changes: 11 additions & 2 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Expand Up @@ -777,8 +777,17 @@ 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 wrn = sm"""|$msg
|Use '-Wconf:msg=lambda-parens:s' to silence this warning."""
if (currentRun.isScala3 && settings.Wsource.value)
syntaxError(tree.pos.point, msg)
else if (currentRun.isScala3 || settings.Wsource.value)
deprecationWarning(tree.pos.point, wrn, "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
12 changes: 6 additions & 6 deletions src/compiler/scala/tools/nsc/typechecker/Contexts.scala
Expand Up @@ -1372,17 +1372,17 @@ trait Contexts { self: Analyzer =>
val parent = currentClass.parentSymbols.find(_.isNonBottomSubClass(inherited1.owner)).getOrElse(NoSymbol)
val inherit = if (parent.exists && parent != inherited1.owner) s", inherited through parent $parent" else ""
val message =
s"""it is both defined in the enclosing ${outer1.owner} and inherited in the enclosing $classDesc as $inherited1 (defined in ${inherited1.ownsString}$inherit)
|In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope.
|Such references are ambiguous in Scala 3. To continue using the inherited symbol, write `this.${outer1.name}`.""".stripMargin
sm"""|it is both defined in the enclosing ${outer1.owner} and inherited in the enclosing $classDesc as $inherited1 (defined in ${inherited1.ownsString}$inherit)
|In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope.
|Such references are ambiguous in Scala 3. To continue using the inherited symbol, write `this.${outer1.name}`."""
if (currentRun.isScala3)
Some(LookupAmbiguous(message))
else {
// passing the message to `typedIdent` as attachment, we don't have the position here to report the warning
inherited.updateAttachment(LookupAmbiguityWarning(
s"""reference to ${outer1.name} is ambiguous;
|$message
|Or use `-Wconf:msg=legacy-binding:s` to silence this warning.""".stripMargin))
sm"""|reference to ${outer1.name} is ambiguous;
|$message
|Or use `-Wconf:msg=legacy-binding:s` to silence this warning."""))
None
}
}
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
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
}
}
7 changes: 7 additions & 0 deletions test/files/neg/parens-for-params.check
@@ -0,0 +1,7 @@
parens-for-params.scala:5: warning: parentheses are required around the parameter of a lambda
Use '-Wconf:msg=lambda-parens: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
}
}
11 changes: 11 additions & 0 deletions test/files/pos/parens-for-params-silent-3.scala
@@ -0,0 +1,11 @@

// scalac: -Werror -Wconf:msg=lambda-parens:s -Xsource:3

class C {
def f = {
x: Int => x * 2
}
def g = {
(x: Int) => x * 2
}
}
10 changes: 10 additions & 0 deletions test/files/pos/parens-for-params-silent.scala
@@ -0,0 +1,10 @@
// scalac: -Werror -Wsource -Wconf:msg=lambda-parens:s

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

0 comments on commit 99bfa0f

Please sign in to comment.