Skip to content

Conversation

@jglodek
Copy link

@jglodek jglodek commented Oct 9, 2025

What

Fixes SCL-21577: Wrong "Unnecessary parentheses" warning shown in type pattern with function literal

Problem

IntelliJ was incorrectly flagging parentheses around function types in type patterns as unnecessary:

null match {
  case _: (String => String) =>  // Parentheses ARE needed but were flagged as unnecessary
  case _ =>
}

Without the parentheses, the code would be parsed differently and wouldn't compile.

Solution

Extended the NeededParenthesisAroundFunctionalType check in ParenthesizedElement.scala to recognize that parentheses are necessary when:

  • The parent is ScParameterType (already handled)
  • OR the parent is ScTypePattern (newly added)

Testing

  • Added test case testSCL21577_FunctionTypeInPattern in ScalaUnnecessaryParenthesesInspectionTest_Scala3
  • Test verifies that parentheses around function types in type patterns are NOT flagged as unnecessary
  • Test fails before the fix, passes after

Files Changed

  • scala/scala-impl/src/org/jetbrains/plugins/scala/extensions/ParenthesizedElement.scala
  • scala/scala-impl/test/org/jetbrains/plugins/scala/codeInspection/parentheses/ScalaUnnecessaryParenthesesInspectionTest.scala

…n type patterns #SCL-21577

Parentheses around function types in type patterns are necessary, e.g.:
  case _: (String => String) =>

Extended NeededParenthesisAroundFunctionalType to also check for ScTypePattern parent,
not just ScParameterType.

Added test case testSCL21577_FunctionTypeInPattern.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant