Skip to content
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

Fix unused-binding-detection in case patterns #5265

Merged
merged 1 commit into from
Aug 2, 2024

Conversation

ChrisPenner
Copy link
Contributor

@ChrisPenner ChrisPenner commented Aug 2, 2024

Fixes #5247

Overview

Previously the LSP would warn you of "unused bindings" if the binding was unused in EITHER the guard OR the body, and also didn't correctly handle cases where you had several guards attached to a single pattern.

E.g. before:

image

Now, it doesn't produce an error in that case at all.

This fixes that.

There was also an oversight in the pattern parser which would give a "Constructor not found" error if you tried to use something like Some _blah; so I added a case there to treat under-score prefixed pattern variables just like other pattern variables, (but they're special in the unused bindings checker)

Implementation notes

The current format of match cases in the ABT makes this a bit annoying to handle properly, but basically I now batch up all the match-cases with an identical pattern (which hopefully correspond to a single binding block) then batch up all the var usages amongst them and subtract ALL those usages from the unused bindings.

Test coverage

Added some additional case-match test cases.

Loose ends

The annotations for pattern match bindings are a bit imprecise, so for now it will simply highlight the whole pattern section if there's an unused binding there, but the error mentions the var by name so it's not a big issue.
I can probably fix this with a bunch of elbow grease, but for now it doesn't seem worth the effort tbh.

die hq s = case L.payload hq of
-- if token not hash qualified or uppercase,
-- fail w/out consuming it to allow backtracking
HQ.NameOnly n
| Set.null s
&& isLower n ->
&& (isLower n || isIgnored n) ->
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously we'd fall through to failCommitted on a _foo pattern variable, when really we should treat it just like a regular pattern var.

@ChrisPenner ChrisPenner force-pushed the lsp/fix-unused-bindings-in-cases branch from 84fceec to 3e87dc3 Compare August 2, 2024 17:57
Copy link
Contributor

@ceedubs ceedubs left a comment

Choose a reason for hiding this comment

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

Just gave this a spin and it's working great for me!

@ChrisPenner
Copy link
Contributor Author

Nix build is failing, looks like it's just:

No space left on device

So I'll go ahead and merge.

@ChrisPenner ChrisPenner merged commit c32bb93 into trunk Aug 2, 2024
34 of 35 checks passed
@ChrisPenner ChrisPenner deleted the lsp/fix-unused-bindings-in-cases branch August 2, 2024 19:59
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.

LSP: unused binding false positive on match guard
3 participants