[ruff] skip fix for RUF059 if dummy name is already bound (unused-unpacked-variable) (#18509)

This commit is contained in:
chiri 2025-06-11 08:58:05 +03:00 committed by GitHub
parent a2de81cb27
commit dc322d23dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 1 deletions

View file

@ -94,3 +94,9 @@ def f():
(exponential := (exponential * base_multiplier) % 3): i + 1 for i in range(2)
}
return hash_map
# see: https://github.com/astral-sh/ruff/issues/18507
def f(_x):
x, = "1"
print(_x)

View file

@ -3,6 +3,7 @@ use ruff_python_semantic::Binding;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
use crate::renamer::ShadowedKind;
use crate::{Edit, Fix, FixAvailability, Violation};
/// ## What it does
@ -63,6 +64,11 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {
let name = binding.name(checker.source());
let renamed = format!("_{name}");
if ShadowedKind::new(binding, &renamed, checker).shadows_any() {
return None;
}
if checker.settings.dummy_variable_rgx.is_match(&renamed) {
let edit = Edit::range_replacement(renamed, binding.range());

View file

@ -197,4 +197,14 @@ RUF059_0.py:86:29: RUF059 [*] Unpacked variable `that` is never used
86 |+ open("") as ((this, _that)),
87 87 | ):
88 88 | print("hello")
89 89 |
89 89 |
RUF059_0.py:101:5: RUF059 Unpacked variable `x` is never used
|
99 | # see: https://github.com/astral-sh/ruff/issues/18507
100 | def f(_x):
101 | x, = "1"
| ^ RUF059
102 | print(_x)
|
= help: Prefix it with an underscore or any other dummy variable pattern