impl remove_unused_variable #2207#3288
Open
asukaminato0721 wants to merge 1 commit intofacebook:mainfacebook/pyrefly:mainfrom
asukaminato0721:2207-unused-variableasukaminato0721/pyrefly:2207-unused-variableCopy head branch name to clipboard
Open
impl remove_unused_variable #2207#3288asukaminato0721 wants to merge 1 commit intofacebook:mainfacebook/pyrefly:mainfrom asukaminato0721:2207-unused-variableasukaminato0721/pyrefly:2207-unused-variableCopy head branch name to clipboard
asukaminato0721 wants to merge 1 commit intofacebook:mainfacebook/pyrefly:mainfrom
asukaminato0721:2207-unused-variableasukaminato0721/pyrefly:2207-unused-variableCopy head branch name to clipboard
Conversation
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
There was a problem hiding this comment.
Pull request overview
Adds an LSP local quick-fix for unused local variables in Pyrefly, enabling editors to offer an automated “Remove unused variable” action alongside existing quick-fixes.
Changes:
- Introduces a
remove_unused_variablequick-fix that removes the corresponding assignment statement and insertspasswhen needed. - Wires the new quick-fix into
Transaction::local_quickfix_code_actions_sortedbased onbindings.unused_variables(). - Adds LSP code-action tests covering basic removal,
passinsertion, and skipping tuple-unpacking assignments.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
pyrefly/lib/test/lsp/code_actions.rs |
Adds helper + tests validating the new “Remove unused variable” code action behavior. |
pyrefly/lib/state/lsp/quick_fixes/remove_unused_variable.rs |
Implements the new quick-fix edit generation (statement removal + optional pass). |
pyrefly/lib/state/lsp/quick_fixes/mod.rs |
Exposes the new quick-fix module. |
pyrefly/lib/state/lsp.rs |
Adds the new quick-fix to the local quick-fix code action list using unused-variable binding info. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+28
to
+36
| let context = find_unused_assignment_context(&ast.body, unused_range)?; | ||
| let removal_range = statement_removal_range(module_info.contents(), context.stmt)?; | ||
| let replacement = if needs_pass_after_removal(context.parent_body, context.stmt.range()) { | ||
| let (indent, _) = | ||
| line_indent_and_start(module_info.contents(), context.stmt.range().start())?; | ||
| format!("{indent}pass\n") | ||
| } else { | ||
| String::new() | ||
| }; |
| assert!(remove_unused_variable_action(code, code.find("unused").unwrap()).is_none()); | ||
| } | ||
|
|
||
| #[test] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
part of #2207
adds Remove unused variable x for simple unused local assignments
Test Plan
add test