Skip to content

impl remove_unused_variable #2207#3288

Open
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:2207-unused-variable
Open

impl remove_unused_variable #2207#3288
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:2207-unused-variable

Conversation

@asukaminato0721
Copy link
Copy Markdown
Contributor

@asukaminato0721 asukaminato0721 commented May 2, 2026

Summary

part of #2207

adds Remove unused variable x for simple unused local assignments

Test Plan

add test

@meta-cla meta-cla Bot added the cla signed label May 2, 2026
@github-actions github-actions Bot added the size/l label May 2, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 2, 2026

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@asukaminato0721 asukaminato0721 marked this pull request as ready for review May 2, 2026 01:35
Copilot AI review requested due to automatic review settings May 2, 2026 01:35
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_variable quick-fix that removes the corresponding assignment statement and inserts pass when needed.
  • Wires the new quick-fix into Transaction::local_quickfix_code_actions_sorted based on bindings.unused_variables().
  • Adds LSP code-action tests covering basic removal, pass insertion, 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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants