Fix unused-binding-detection in case patterns #5265
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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.