mirror of https://github.com/astral-sh/ruff
more builtin name checks when autofixing (#2430)
This commit is contained in:
parent
1ea88ea56b
commit
9d8c6ba671
|
|
@ -21,3 +21,10 @@ if isinstance(a, int) and isinstance(b, bool) or isinstance(a, float):
|
|||
|
||||
if isinstance(a, bool) or isinstance(b, str):
|
||||
pass
|
||||
|
||||
def f():
|
||||
# OK
|
||||
def isinstance(a, b):
|
||||
return False
|
||||
if isinstance(a, int) or isinstance(a, float):
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -117,6 +117,26 @@ def f():
|
|||
return False
|
||||
|
||||
|
||||
def f():
|
||||
def any(exp):
|
||||
pass
|
||||
|
||||
for x in iterable:
|
||||
if check(x):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def f():
|
||||
def all(exp):
|
||||
pass
|
||||
|
||||
for x in iterable:
|
||||
if check(x):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def f():
|
||||
x = 1
|
||||
|
||||
|
|
@ -125,3 +145,13 @@ def f():
|
|||
if check(x):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def f():
|
||||
x = 1
|
||||
|
||||
# SIM111
|
||||
for x in iterable:
|
||||
if check(x):
|
||||
return False
|
||||
return True
|
||||
|
|
|
|||
|
|
@ -115,3 +115,43 @@ def f():
|
|||
else:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def f():
|
||||
def any(exp):
|
||||
pass
|
||||
|
||||
for x in iterable:
|
||||
if check(x):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def f():
|
||||
def all(exp):
|
||||
pass
|
||||
|
||||
for x in iterable:
|
||||
if check(x):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def f():
|
||||
x = 1
|
||||
|
||||
# SIM110
|
||||
for x in iterable:
|
||||
if check(x):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def f():
|
||||
x = 1
|
||||
|
||||
# SIM111
|
||||
for x in iterable:
|
||||
if check(x):
|
||||
return False
|
||||
return True
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ a = True if b + c else False # SIM210
|
|||
|
||||
a = False if b else True # OK
|
||||
|
||||
def bool():
|
||||
def f():
|
||||
# OK
|
||||
def bool():
|
||||
return False
|
||||
|
||||
a = True if b else False # OK
|
||||
a = True if b else False
|
||||
|
|
|
|||
|
|
@ -47,6 +47,9 @@ pub fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) {
|
|||
if func_name != "isinstance" {
|
||||
continue;
|
||||
}
|
||||
if !checker.is_builtin("isinstance") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Collect the name of the argument.
|
||||
let ExprKind::Name { id: arg_name, .. } = &args[0].node else {
|
||||
|
|
|
|||
|
|
@ -202,7 +202,7 @@ pub fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt, sibling:
|
|||
},
|
||||
Range::from_located(stmt),
|
||||
);
|
||||
if checker.patch(diagnostic.kind.rule()) {
|
||||
if checker.patch(diagnostic.kind.rule()) && checker.is_builtin("any") {
|
||||
diagnostic.amend(Fix::replacement(
|
||||
contents,
|
||||
stmt.location,
|
||||
|
|
@ -249,7 +249,7 @@ pub fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt, sibling:
|
|||
},
|
||||
Range::from_located(stmt),
|
||||
);
|
||||
if checker.patch(diagnostic.kind.rule()) {
|
||||
if checker.patch(diagnostic.kind.rule()) && checker.is_builtin("all") {
|
||||
diagnostic.amend(Fix::replacement(
|
||||
contents,
|
||||
stmt.location,
|
||||
|
|
|
|||
|
|
@ -68,14 +68,25 @@ expression: diagnostics
|
|||
end_location:
|
||||
row: 126
|
||||
column: 23
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ConvertLoopToAny:
|
||||
any: return any(check(x) for x in iterable)
|
||||
location:
|
||||
row: 144
|
||||
column: 4
|
||||
end_location:
|
||||
row: 146
|
||||
column: 23
|
||||
fix:
|
||||
content:
|
||||
- return any(check(x) for x in iterable)
|
||||
location:
|
||||
row: 124
|
||||
row: 144
|
||||
column: 4
|
||||
end_location:
|
||||
row: 127
|
||||
row: 147
|
||||
column: 16
|
||||
parent: ~
|
||||
|
||||
|
|
|
|||
|
|
@ -78,4 +78,34 @@ expression: diagnostics
|
|||
row: 87
|
||||
column: 19
|
||||
parent: ~
|
||||
- kind:
|
||||
ConvertLoopToAll:
|
||||
all: return all(not check(x) for x in iterable)
|
||||
location:
|
||||
row: 134
|
||||
column: 4
|
||||
end_location:
|
||||
row: 136
|
||||
column: 24
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ConvertLoopToAll:
|
||||
all: return all(not check(x) for x in iterable)
|
||||
location:
|
||||
row: 154
|
||||
column: 4
|
||||
end_location:
|
||||
row: 156
|
||||
column: 24
|
||||
fix:
|
||||
content:
|
||||
- return all(not check(x) for x in iterable)
|
||||
location:
|
||||
row: 154
|
||||
column: 4
|
||||
end_location:
|
||||
row: 157
|
||||
column: 15
|
||||
parent: ~
|
||||
|
||||
|
|
|
|||
|
|
@ -63,11 +63,11 @@ expression: diagnostics
|
|||
IfExprWithTrueFalse:
|
||||
expr: b
|
||||
location:
|
||||
row: 12
|
||||
column: 4
|
||||
row: 14
|
||||
column: 8
|
||||
end_location:
|
||||
row: 12
|
||||
column: 24
|
||||
row: 14
|
||||
column: 28
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue