Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore fix if "bool" is not builtin #2429

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions resources/test/fixtures/flake8_simplify/SIM103.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,13 @@ def f():
return True
else:
return True


def f():
# OK
def bool():
return False
if a:
return True
else:
return False
5 changes: 5 additions & 0 deletions resources/test/fixtures/flake8_simplify/SIM210.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
a = True if b + c else False # SIM210

a = False if b else True # OK

def bool():
return False

a = True if b else False # OK
48 changes: 29 additions & 19 deletions src/rules/flake8_simplify/rules/ast_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,36 @@ pub fn return_bool_condition_directly(checker: &mut Checker, stmt: &Stmt) {
&& matches!(else_return, Bool::False)
&& !has_comments(stmt, checker.locator)
{
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 {
func: Box::new(create_expr(ExprKind::Name {
id: "bool".to_string(),
ctx: ExprContext::Load,
})),
args: vec![(**test).clone()],
keywords: vec![],
}))),
}),
if matches!(test.node, ExprKind::Compare { .. }) {
diagnostic.amend(Fix::replacement(
unparse_stmt(
&create_stmt(StmtKind::Return {
value: Some(test.clone()),
}),
checker.stylist,
),
stmt.location,
stmt.end_location.unwrap(),
));
} else if checker.is_builtin("bool") {
diagnostic.amend(Fix::replacement(
unparse_stmt(
&create_stmt(StmtKind::Return {
value: Some(Box::new(create_expr(ExprKind::Call {
func: Box::new(create_expr(ExprKind::Name {
id: "bool".to_string(),
ctx: ExprContext::Load,
})),
args: vec![(**test).clone()],
keywords: vec![],
}))),
}),
checker.stylist,
),
stmt.location,
stmt.end_location.unwrap(),
));
};
diagnostic.amend(Fix::replacement(
unparse_stmt(&return_stmt, checker.stylist),
stmt.location,
stmt.end_location.unwrap(),
));
}
checker.diagnostics.push(diagnostic);
}
Expand Down
37 changes: 22 additions & 15 deletions src/rules/flake8_simplify/rules/ast_ifexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,29 @@ pub fn explicit_true_false_in_ifexpr(
Range::from_located(expr),
);
if checker.patch(diagnostic.kind.rule()) {
let bool_expr = match test.node {
ExprKind::Compare { .. } => test.clone(),
_ => create_expr(ExprKind::Call {
func: Box::new(create_expr(ExprKind::Name {
id: "bool".to_string(),
ctx: ExprContext::Load,
})),
args: vec![test.clone()],
keywords: vec![],
}),
if matches!(test.node, ExprKind::Compare { .. }) {
diagnostic.amend(Fix::replacement(
unparse_expr(&test.clone(), checker.stylist),
expr.location,
expr.end_location.unwrap(),
));
} else if checker.is_builtin("bool") {
diagnostic.amend(Fix::replacement(
unparse_expr(
&create_expr(ExprKind::Call {
func: Box::new(create_expr(ExprKind::Name {
id: "bool".to_string(),
ctx: ExprContext::Load,
})),
args: vec![test.clone()],
keywords: vec![],
}),
checker.stylist,
),
expr.location,
expr.end_location.unwrap(),
));
};
diagnostic.amend(Fix::replacement(
unparse_expr(&bool_expr, checker.stylist),
expr.location,
expr.end_location.unwrap(),
));
}
checker.diagnostics.push(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,15 @@ expression: diagnostics
column: 19
fix: ~
parent: ~
- kind:
ReturnBoolConditionDirectly:
cond: a
location:
row: 83
column: 4
end_location:
row: 86
column: 20
fix: ~
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,15 @@ expression: diagnostics
row: 5
column: 28
parent: ~
- kind:
IfExprWithTrueFalse:
expr: b
location:
row: 12
column: 4
end_location:
row: 12
column: 24
fix: ~
parent: ~