mirror of https://github.com/astral-sh/ruff
feat: let SIM103 return expressions without bool() wrapping (#2410)
This commit is contained in:
parent
d601abe01b
commit
1dd9ccf7f6
|
|
@ -6,6 +6,14 @@ def f():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
# SIM103
|
||||||
|
if a == b:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def f():
|
def f():
|
||||||
# SIM103
|
# SIM103
|
||||||
if a:
|
if a:
|
||||||
|
|
|
||||||
|
|
@ -184,7 +184,11 @@ pub fn return_bool_condition_directly(checker: &mut Checker, stmt: &Stmt) {
|
||||||
&& matches!(else_return, Bool::False)
|
&& matches!(else_return, Bool::False)
|
||||||
&& !has_comments(stmt, checker.locator)
|
&& !has_comments(stmt, checker.locator)
|
||||||
{
|
{
|
||||||
let return_stmt = create_stmt(StmtKind::Return {
|
let return_stmt = match test.node {
|
||||||
|
ExprKind::Compare { .. } => create_stmt(StmtKind::Return {
|
||||||
|
value: Some(test.clone()),
|
||||||
|
}),
|
||||||
|
_ => create_stmt(StmtKind::Return {
|
||||||
value: Some(Box::new(create_expr(ExprKind::Call {
|
value: Some(Box::new(create_expr(ExprKind::Call {
|
||||||
func: Box::new(create_expr(ExprKind::Name {
|
func: Box::new(create_expr(ExprKind::Name {
|
||||||
id: "bool".to_string(),
|
id: "bool".to_string(),
|
||||||
|
|
@ -193,7 +197,8 @@ pub fn return_bool_condition_directly(checker: &mut Checker, stmt: &Stmt) {
|
||||||
args: vec![(**test).clone()],
|
args: vec![(**test).clone()],
|
||||||
keywords: vec![],
|
keywords: vec![],
|
||||||
}))),
|
}))),
|
||||||
});
|
}),
|
||||||
|
};
|
||||||
diagnostic.amend(Fix::replacement(
|
diagnostic.amend(Fix::replacement(
|
||||||
unparse_stmt(&return_stmt, checker.stylist),
|
unparse_stmt(&return_stmt, checker.stylist),
|
||||||
stmt.location,
|
stmt.location,
|
||||||
|
|
|
||||||
|
|
@ -23,50 +23,69 @@ expression: diagnostics
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
ReturnBoolConditionDirectly:
|
ReturnBoolConditionDirectly:
|
||||||
cond: b
|
cond: a == b
|
||||||
location:
|
location:
|
||||||
row: 13
|
row: 11
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 16
|
row: 14
|
||||||
column: 20
|
column: 20
|
||||||
fix:
|
fix:
|
||||||
content:
|
content:
|
||||||
- return bool(b)
|
- return a == b
|
||||||
location:
|
location:
|
||||||
row: 13
|
row: 11
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 16
|
row: 14
|
||||||
column: 20
|
column: 20
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
ReturnBoolConditionDirectly:
|
ReturnBoolConditionDirectly:
|
||||||
cond: b
|
cond: b
|
||||||
location:
|
location:
|
||||||
|
row: 21
|
||||||
|
column: 4
|
||||||
|
end_location:
|
||||||
row: 24
|
row: 24
|
||||||
|
column: 20
|
||||||
|
fix:
|
||||||
|
content:
|
||||||
|
- return bool(b)
|
||||||
|
location:
|
||||||
|
row: 21
|
||||||
|
column: 4
|
||||||
|
end_location:
|
||||||
|
row: 24
|
||||||
|
column: 20
|
||||||
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
ReturnBoolConditionDirectly:
|
||||||
|
cond: b
|
||||||
|
location:
|
||||||
|
row: 32
|
||||||
column: 8
|
column: 8
|
||||||
end_location:
|
end_location:
|
||||||
row: 27
|
row: 35
|
||||||
column: 24
|
column: 24
|
||||||
fix:
|
fix:
|
||||||
content:
|
content:
|
||||||
- return bool(b)
|
- return bool(b)
|
||||||
location:
|
location:
|
||||||
row: 24
|
row: 32
|
||||||
column: 8
|
column: 8
|
||||||
end_location:
|
end_location:
|
||||||
row: 27
|
row: 35
|
||||||
column: 24
|
column: 24
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
ReturnBoolConditionDirectly:
|
ReturnBoolConditionDirectly:
|
||||||
cond: a
|
cond: a
|
||||||
location:
|
location:
|
||||||
row: 49
|
row: 57
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 52
|
row: 60
|
||||||
column: 19
|
column: 19
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue