Skip to content

Commit cbfeed1

Browse files
committed
Do not rewrite { (a: Int) => ... } to { a: Int => ... }
The latter syntax is removed from Dotty due to its irregularity: scala> def foo : Int => Int = { a: Int => a } 1 |def foo : Int => Int = { a: Int => a } | ^^^^^^ |parentheses are required around the parameter of a lambda |This construct can be rewritten automatically under -language:Scala2 -rewrite.
1 parent 3d8a076 commit cbfeed1

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

scala/scala-impl/src/org/jetbrains/plugins/scala/codeInspection/parentheses/ScalaUnnecessaryParenthesesInspectionBase.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,12 @@ abstract class ScalaUnnecessaryParenthesesInspectionBase
3636
def setSettings(settings: UnnecessaryParenthesesSettings): Unit
3737

3838
private def hasSingleParenthesizedParam(f: ScFunctionExpr): Boolean = {
39-
// If an anonymous function (x: T) => e with a single typed parameter appears as the result expression of a block, it can be abbreviated to x: T => e.
40-
def isBlockResultExpr: Boolean = f.getParent match {
41-
case block: ScBlockExpr if block.resultExpression.contains(f) => true
42-
case _ => false
43-
}
4439
// In the case of a single untyped formal parameter, (x) => e can be abbreviated to x => e
4540
def hasNoParamType = f.parameters.headOption.exists(_.typeElement.isEmpty)
4641

4742
def hasParenthesizedClause = f.params.clauses.headOption.exists(isParenthesised)
4843

49-
f.parameters.size == 1 && hasParenthesizedClause && (hasNoParamType || isBlockResultExpr)
44+
f.parameters.size == 1 && hasParenthesizedClause && hasNoParamType
5045
}
5146

5247
private def isParenthesesRedundant(elem: ScParenthesizedElement, settings: UnnecessaryParenthesesSettings): Boolean = {

scala/scala-impl/test/org/jetbrains/plugins/scala/codeInspection/parentheses/ScalaUnnecessaryParenthesesInspectionTest.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,13 @@ class ScalaUnnecessaryParenthesesInspectionTest extends ScalaQuickFixTestBase {
356356

357357
def test_lambdaParam(): Unit = {
358358
val r1 = s"Seq(1) map { $START(i)$END => i + 1 }"
359-
val r2 = s"Seq(1) map { $START(i: Int)$END => i + 1 }"
359+
val r2 = s"Seq(1) map { $START(i)$END => i + 1 }"
360360
val r3 = s"Seq(1) map ($START(i)$END => i + 1)"
361361
val required = "Seq(1) map ((i: Int) => i + 1)"
362362

363363
checkTextHasNoErrors(required)
364364
checkTextHasErrors(r1)
365-
checkTextHasErrors(r2)
365+
checkTextHasNoErrors(r2)
366366
checkTextHasErrors(r3)
367367

368368
withSettings(ignoreAroundFunctionExprParam) {
@@ -372,9 +372,9 @@ class ScalaUnnecessaryParenthesesInspectionTest extends ScalaQuickFixTestBase {
372372
checkTextHasNoErrors(r3)
373373
}
374374

375-
val text = s"Seq(1) map { (${CARET_MARKER}i: Int) => i + 1 }"
376-
val result = s"Seq(1) map { i: Int => i + 1 }"
377-
val hint = hintBeginning + " (i: Int)"
375+
val text = s"Seq(1) map { (${CARET_MARKER}i) => i + 1 }"
376+
val result = s"Seq(1) map { i => i + 1 }"
377+
val hint = hintBeginning + " (i)"
378378
testQuickFix(text, result, hint)
379379
}
380380

0 commit comments

Comments
 (0)