mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
[ruff
] Implemented used-dummy-variable
(RUF052
) (#14611)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
a69dfd4a74
commit
bf0fd04e4e
12 changed files with 778 additions and 8 deletions
106
crates/ruff_linter/resources/test/fixtures/ruff/RUF052.py
vendored
Normal file
106
crates/ruff_linter/resources/test/fixtures/ruff/RUF052.py
vendored
Normal file
|
@ -0,0 +1,106 @@
|
|||
# Correct
|
||||
|
||||
for _ in range(5):
|
||||
pass
|
||||
|
||||
_valid_type = int
|
||||
|
||||
_valid_var_1: _valid_type
|
||||
|
||||
_valid_var_1 = 1
|
||||
|
||||
_valid_var_2 = 2
|
||||
|
||||
_valid_var_3 = _valid_var_1 + _valid_var_2
|
||||
|
||||
def _valid_fun():
|
||||
pass
|
||||
|
||||
_valid_fun()
|
||||
|
||||
def fun(arg):
|
||||
_valid_unused_var = arg
|
||||
pass
|
||||
|
||||
_x = "global"
|
||||
|
||||
def fun():
|
||||
global _x
|
||||
return _x
|
||||
|
||||
def fun():
|
||||
__dunder__ = "dunder variable"
|
||||
return __dunder__
|
||||
|
||||
def fun():
|
||||
global _x
|
||||
_x = "reassigned global"
|
||||
return _x
|
||||
|
||||
class _ValidClass:
|
||||
pass
|
||||
|
||||
_ValidClass()
|
||||
|
||||
class ClassOk:
|
||||
_valid_private_cls_attr = 1
|
||||
|
||||
print(_valid_private_cls_attr)
|
||||
|
||||
def __init__(self):
|
||||
self._valid_private_ins_attr = 2
|
||||
print(self._valid_private_ins_attr)
|
||||
|
||||
def _valid_method(self):
|
||||
return self._valid_private_ins_attr
|
||||
|
||||
def method(arg):
|
||||
_valid_unused_var = arg
|
||||
return
|
||||
|
||||
def fun(x):
|
||||
_ = 1
|
||||
__ = 2
|
||||
___ = 3
|
||||
if x == 1:
|
||||
return _
|
||||
if x == 2:
|
||||
return __
|
||||
if x == 3:
|
||||
return ___
|
||||
return x
|
||||
|
||||
# Incorrect
|
||||
|
||||
class Class_:
|
||||
def fun(self):
|
||||
_var = "method variable" # [RUF052]
|
||||
return _var
|
||||
|
||||
def fun(_var): # [RUF052]
|
||||
return _var
|
||||
|
||||
def fun():
|
||||
_list = "built-in" # [RUF052]
|
||||
return _list
|
||||
|
||||
x = "global"
|
||||
|
||||
def fun():
|
||||
global x
|
||||
_x = "shadows global" # [RUF052]
|
||||
return _x
|
||||
|
||||
def foo():
|
||||
x = "outer"
|
||||
def bar():
|
||||
nonlocal x
|
||||
_x = "shadows nonlocal" # [RUF052]
|
||||
return _x
|
||||
bar()
|
||||
return x
|
||||
|
||||
def fun():
|
||||
x = "local"
|
||||
_x = "shadows local" # [RUF052]
|
||||
return _x
|
Loading…
Add table
Add a link
Reference in a new issue