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):
|
if isinstance(a, bool) or isinstance(b, str):
|
||||||
pass
|
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
|
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():
|
def f():
|
||||||
x = 1
|
x = 1
|
||||||
|
|
||||||
|
|
@ -125,3 +145,13 @@ def f():
|
||||||
if check(x):
|
if check(x):
|
||||||
return True
|
return True
|
||||||
return False
|
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:
|
else:
|
||||||
return True
|
return True
|
||||||
return False
|
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
|
a = False if b else True # OK
|
||||||
|
|
||||||
def bool():
|
def f():
|
||||||
return False
|
# 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" {
|
if func_name != "isinstance" {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if !checker.is_builtin("isinstance") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Collect the name of the argument.
|
// Collect the name of the argument.
|
||||||
let ExprKind::Name { id: arg_name, .. } = &args[0].node else {
|
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),
|
Range::from_located(stmt),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) && checker.is_builtin("any") {
|
||||||
diagnostic.amend(Fix::replacement(
|
diagnostic.amend(Fix::replacement(
|
||||||
contents,
|
contents,
|
||||||
stmt.location,
|
stmt.location,
|
||||||
|
|
@ -249,7 +249,7 @@ pub fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt, sibling:
|
||||||
},
|
},
|
||||||
Range::from_located(stmt),
|
Range::from_located(stmt),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) && checker.is_builtin("all") {
|
||||||
diagnostic.amend(Fix::replacement(
|
diagnostic.amend(Fix::replacement(
|
||||||
contents,
|
contents,
|
||||||
stmt.location,
|
stmt.location,
|
||||||
|
|
|
||||||
|
|
@ -68,14 +68,25 @@ expression: diagnostics
|
||||||
end_location:
|
end_location:
|
||||||
row: 126
|
row: 126
|
||||||
column: 23
|
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:
|
fix:
|
||||||
content:
|
content:
|
||||||
- return any(check(x) for x in iterable)
|
- return any(check(x) for x in iterable)
|
||||||
location:
|
location:
|
||||||
row: 124
|
row: 144
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 127
|
row: 147
|
||||||
column: 16
|
column: 16
|
||||||
parent: ~
|
parent: ~
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,4 +78,34 @@ expression: diagnostics
|
||||||
row: 87
|
row: 87
|
||||||
column: 19
|
column: 19
|
||||||
parent: ~
|
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:
|
IfExprWithTrueFalse:
|
||||||
expr: b
|
expr: b
|
||||||
location:
|
location:
|
||||||
row: 12
|
row: 14
|
||||||
column: 4
|
column: 8
|
||||||
end_location:
|
end_location:
|
||||||
row: 12
|
row: 14
|
||||||
column: 24
|
column: 28
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue