mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
[ruff
] skip fix for RUF059
if dummy name is already bound (unused-unpacked-variable) (#18509)
This commit is contained in:
parent
a2de81cb27
commit
dc322d23dd
3 changed files with 23 additions and 1 deletions
|
@ -94,3 +94,9 @@ def f():
|
||||||
(exponential := (exponential * base_multiplier) % 3): i + 1 for i in range(2)
|
(exponential := (exponential * base_multiplier) % 3): i + 1 for i in range(2)
|
||||||
}
|
}
|
||||||
return hash_map
|
return hash_map
|
||||||
|
|
||||||
|
|
||||||
|
# see: https://github.com/astral-sh/ruff/issues/18507
|
||||||
|
def f(_x):
|
||||||
|
x, = "1"
|
||||||
|
print(_x)
|
||||||
|
|
|
@ -3,6 +3,7 @@ use ruff_python_semantic::Binding;
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
use crate::renamer::ShadowedKind;
|
||||||
use crate::{Edit, Fix, FixAvailability, Violation};
|
use crate::{Edit, Fix, FixAvailability, Violation};
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
|
@ -63,6 +64,11 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {
|
||||||
|
|
||||||
let name = binding.name(checker.source());
|
let name = binding.name(checker.source());
|
||||||
let renamed = format!("_{name}");
|
let renamed = format!("_{name}");
|
||||||
|
|
||||||
|
if ShadowedKind::new(binding, &renamed, checker).shadows_any() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
if checker.settings.dummy_variable_rgx.is_match(&renamed) {
|
if checker.settings.dummy_variable_rgx.is_match(&renamed) {
|
||||||
let edit = Edit::range_replacement(renamed, binding.range());
|
let edit = Edit::range_replacement(renamed, binding.range());
|
||||||
|
|
||||||
|
|
|
@ -197,4 +197,14 @@ RUF059_0.py:86:29: RUF059 [*] Unpacked variable `that` is never used
|
||||||
86 |+ open("") as ((this, _that)),
|
86 |+ open("") as ((this, _that)),
|
||||||
87 87 | ):
|
87 87 | ):
|
||||||
88 88 | print("hello")
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue