more builtin name checks when autofixing (#2430)

This commit is contained in:
Florian Best 2023-02-01 14:16:47 +01:00 committed by GitHub
parent 1ea88ea56b
commit 9d8c6ba671
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 134 additions and 11 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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,

View File

@ -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: ~

View File

@ -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: ~

View File

@ -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: ~