Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF059_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {

let name = binding.name(checker.source());
let renamed = format!("_{name}");

let scope = checker.semantic().scopes.get(binding.scope)?;

if scope
.bindings()
.any(|(_, id)| checker.semantic().binding(id).name(checker.source()) == renamed)
{
return None;
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we could use the following here (which is what we use in other places to test shadowing)

if ShadowedKind::new(binding, new_name, checker).shadows_any() {
return;
}


if checker.settings.dummy_variable_rgx.is_match(&renamed) {
let edit = Edit::range_replacement(renamed, binding.range());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading