Skip to content

Do not rewrite { (a: Int) => ... } to { a: Int => ... } #488

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,12 @@ abstract class ScalaUnnecessaryParenthesesInspectionBase
def setSettings(settings: UnnecessaryParenthesesSettings): Unit

private def hasSingleParenthesizedParam(f: ScFunctionExpr): Boolean = {
// 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.
def isBlockResultExpr: Boolean = f.getParent match {
case block: ScBlockExpr if block.resultExpression.contains(f) => true
case _ => false
}
// In the case of a single untyped formal parameter, (x) => e can be abbreviated to x => e
def hasNoParamType = f.parameters.headOption.exists(_.typeElement.isEmpty)

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

f.parameters.size == 1 && hasParenthesizedClause && (hasNoParamType || isBlockResultExpr)
f.parameters.size == 1 && hasParenthesizedClause && hasNoParamType
}

private def isParenthesesRedundant(elem: ScParenthesizedElement, settings: UnnecessaryParenthesesSettings): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class ScalaUnnecessaryParenthesesInspectionTest extends ScalaQuickFixTestBase {

checkTextHasNoErrors(required)
checkTextHasErrors(r1)
checkTextHasErrors(r2)
checkTextHasNoErrors(r2)
checkTextHasErrors(r3)

withSettings(ignoreAroundFunctionExprParam) {
Expand All @@ -372,9 +372,9 @@ class ScalaUnnecessaryParenthesesInspectionTest extends ScalaQuickFixTestBase {
checkTextHasNoErrors(r3)
}

val text = s"Seq(1) map { (${CARET_MARKER}i: Int) => i + 1 }"
val result = s"Seq(1) map { i: Int => i + 1 }"
val hint = hintBeginning + " (i: Int)"
val text = s"Seq(1) map { (${CARET_MARKER}i) => i + 1 }"
val result = s"Seq(1) map { i => i + 1 }"
val hint = hintBeginning + " (i)"
testQuickFix(text, result, hint)
}

Expand Down