Skip to content

Conversation

clr-cera
Copy link

This PR creates the code action to discarding an unused variable.

References Issue #4950

As suggested the code action is currently set as preferred.

@clr-cera
Copy link
Author

Hi, I am having two problems with this pr!

First, is the code action always applied after formatting? When the variable is assigned through a pattern, the code action must also remove the ' as ' before the variable name, and I haven't figured out how to find this span.

Also, the tests for the code action are failing, although they produce the right result, how can I fix that?

Accepted the snapshots, and the LabelShorthand edit now adds a ' ' to
comply with formatting
@GearsDatapacks
Copy link
Member

GearsDatapacks commented Sep 12, 2025

  1. No, the user isn't guaranteed to have formatted their code before running a code action. What we usually do when a certain new piece of location information is required is to add it to the AST. However in this case, I should think it would be possible to remove the code from the end of the inner pattern to the end of the as pattern, both of which you should already have the locations for.
  2. That's because we use snapshot testing! The way it works is that first you run the tests to generate the output, then you manually review it to determine whether it is correct or not (use cargo insta review). After that, the correct value is stored and the test will only fail if it differs from the accepted snapshot. See docs/compiler/README.md for more information

@clr-cera
Copy link
Author

  1. No, the user isn't guaranteed to have formatted their code before running a code action. What we usually do when a certain new piece of location information is required is to add it to the AST. However in this case, I should think it would be possible to remove the code from the end of the inner pattern to the end of the as pattern, both of which you should already have the locations for.

    1. That's because we use snapshot testing! The way it works is that first you run the tests to generate the output, then you manually review it to determine whether it is correct or not (use cargo insta review). After that, the correct value is stored and the test will only fail if it differs from the accepted snapshot. See docs/compiler/README.md for more information

Thank you! Now I understood the snapshot testing, and wow, it works so well!
I will try to find the locations in the AST

@clr-cera
Copy link
Author

clr-cera commented Sep 12, 2025

I couldn't find the assign pattern related to the unused variable warning, I tried to visit all assign patterns locations and check if the variable location start is inside, but this returned no patterns, and I did not think of any other way.

Is there a way to, given a location, find the location of a string before it?

I will try to fix it later, thanks for helping!

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.

2 participants