Skip to content

Warn if match in block is not used for PartialFunction #23002

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

som-snytt
Copy link
Contributor

Fixes #21649

Addresses the immediate user issue. (Confusion about compiler behavior.)

Follow-up is improve doc for PartialFunction and improve spec for pattern-matching anonymous function to include non-trivial selector expression.

Actually, first follow-up will be to address:

scala> def f: PartialFunction[(Int, Int), Int] = (x, y) => (2*x, y) match { case (x, y) if x < 5 => x + y }
1 warning found
-- [E210] Syntax Warning: ------------------------------------------------------
1 |def f: PartialFunction[(Int, Int), Int] = (x, y) => (2*x, y) match { case (x, y) if x < 5 => x + y }
  |                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |match expression in result of block will not be used to synthesize partial function
  |
  | longer explanation available when compiling with `-explain`
def f: PartialFunction[(Int, Int), Int]

oh that's because of how parameters are untupled, no doubt. Compare

scala> def f: PartialFunction[(Int, Int), Int] = p => (2*p._1, p._2) match { case (x, y) if x < 5 => x + y }
def f: PartialFunction[(Int, Int), Int]

@som-snytt
Copy link
Contributor Author

footnote the syntax was aligned intentionally 8a91774 which is where I picked up the phrase non-trivial selector expression.

@som-snytt som-snytt marked this pull request as ready for review April 15, 2025 20:55
@Gedochao Gedochao requested a review from noti0na1 April 24, 2025 09:11
Copy link
Member

@noti0na1 noti0na1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@noti0na1 noti0na1 changed the title Warn if match in block is not used for PF Warn if match in block is not used for PartialFunction Apr 24, 2025
@noti0na1 noti0na1 assigned som-snytt and unassigned noti0na1 Apr 24, 2025
@som-snytt som-snytt force-pushed the issue/21649-warn-pf branch from 1dc06ff to 754b9f5 Compare April 24, 2025 14:37
@som-snytt
Copy link
Contributor Author

Rebased for error ID conflict...

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.

Adding a useless assignment statement alters program behavior.
2 participants