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

flake8_simplify : SIM210, SIM211, SIM212 #1717

Merged
merged 9 commits into from
Jan 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,9 @@ For more, see [flake8-simplify](https://pypi.org/project/flake8-simplify/0.19.3/
| SIM201 | NegateEqualOp | Use `left != right` instead of `not left == right` | 🛠 |
| SIM202 | NegateNotEqualOp | Use `left == right` instead of `not left != right` | 🛠 |
| SIM208 | DoubleNegation | Use `expr` instead of `not (not expr)` | 🛠 |
| SIM210 | IfExprWithTrueFalse | Use `bool(expr)` instead of `True if expr else False` | 🛠 |
| SIM211 | IfExprWithFalseTrue | Use `not expr` instead of `False if expr else True` | 🛠 |
| SIM212 | IfExprWithTwistedArms | Use `b if b else a` instead of `a if not b else b` | 🛠 |
| SIM220 | AAndNotA | Use `False` instead of `... and not ...` | 🛠 |
| SIM221 | AOrNotA | Use `True` instead of `... or not ...` | 🛠 |
| SIM222 | OrTrue | Use `True` instead of `... or True` | 🛠 |
Expand Down
7 changes: 7 additions & 0 deletions resources/test/fixtures/flake8_simplify/SIM210.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a = True if b else False # SIM210

a = True if b != c else False # SIM210

a = True if b + c else False # SIM210

a = False if b else True # OK
7 changes: 7 additions & 0 deletions resources/test/fixtures/flake8_simplify/SIM211.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a = False if b else True # SIM211

a = False if b != c else True # SIM211

a = False if b + c else True # SIM211

a = True if b else False # OK
7 changes: 7 additions & 0 deletions resources/test/fixtures/flake8_simplify/SIM212.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
c = b if not a else a # SIM212

c = b + c if not a else a # SIM212

c = b if not x else a # OK

c = a if a else b # OK
4 changes: 4 additions & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,10 @@
"SIM201",
"SIM202",
"SIM208",
"SIM21",
"SIM210",
"SIM211",
"SIM212",
"SIM22",
"SIM220",
"SIM221",
Expand Down
15 changes: 15 additions & 0 deletions src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2716,6 +2716,21 @@ where
}
self.push_scope(Scope::new(ScopeKind::Lambda(Lambda { args, body })));
}
ExprKind::IfExp { test, body, orelse } => {
if self.settings.enabled.contains(&CheckCode::SIM210) {
flake8_simplify::plugins::explicit_true_false_in_ifexpr(
self, expr, test, body, orelse,
)
}
if self.settings.enabled.contains(&CheckCode::SIM211) {
flake8_simplify::plugins::explicit_false_true_in_ifexpr(
self, expr, test, body, orelse,
)
}
if self.settings.enabled.contains(&CheckCode::SIM212) {
flake8_simplify::plugins::twisted_arms_in_ifexpr(self, expr, test, body, orelse)
}
}
ExprKind::ListComp { elt, generators } | ExprKind::SetComp { elt, generators } => {
if self.settings.enabled.contains(&CheckCode::C416) {
if let Some(check) = flake8_comprehensions::checks::unnecessary_comprehension(
Expand Down
3 changes: 3 additions & 0 deletions src/flake8_simplify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ mod tests {
#[test_case(CheckCode::SIM201, Path::new("SIM201.py"); "SIM201")]
#[test_case(CheckCode::SIM202, Path::new("SIM202.py"); "SIM202")]
#[test_case(CheckCode::SIM208, Path::new("SIM208.py"); "SIM208")]
#[test_case(CheckCode::SIM210, Path::new("SIM210.py"); "SIM210")]
#[test_case(CheckCode::SIM211, Path::new("SIM211.py"); "SIM211")]
#[test_case(CheckCode::SIM212, Path::new("SIM212.py"); "SIM212")]
#[test_case(CheckCode::SIM118, Path::new("SIM118.py"); "SIM118")]
#[test_case(CheckCode::SIM220, Path::new("SIM220.py"); "SIM220")]
#[test_case(CheckCode::SIM221, Path::new("SIM221.py"); "SIM221")]
Expand Down
144 changes: 144 additions & 0 deletions src/flake8_simplify/plugins/ast_ifexp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Unaryop};

use crate::ast::helpers::{create_expr, unparse_expr};
use crate::ast::types::Range;
use crate::autofix::Fix;
use crate::checkers::ast::Checker;
use crate::registry::Check;
use crate::violations;

/// SIM210
pub fn explicit_true_false_in_ifexpr(
checker: &mut Checker,
expr: &Expr,
test: &Expr,
body: &Expr,
orelse: &Expr,
) {
let ExprKind::Constant { value, .. } = &body.node else {
return;
};
if !matches!(value, Constant::Bool(true)) {
return;
}
let ExprKind::Constant { value, .. } = &orelse.node else {
return;
};
if !matches!(value, Constant::Bool(false)) {
return;
}

let mut check = Check::new(
violations::IfExprWithTrueFalse(unparse_expr(test, checker.style)),
Range::from_located(expr),
);
if checker.patch(check.kind.code()) {
check.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![create_expr(test.node.clone())],
keywords: vec![],
}),
checker.style,
),
expr.location,
expr.end_location.unwrap(),
));
}
checker.checks.push(check);
}

/// SIM211
pub fn explicit_false_true_in_ifexpr(
checker: &mut Checker,
expr: &Expr,
test: &Expr,
body: &Expr,
orelse: &Expr,
) {
let ExprKind::Constant { value, .. } = &body.node else {
return;
};
if !matches!(value, Constant::Bool(false)) {
return;
}
let ExprKind::Constant { value, .. } = &orelse.node else {
return;
};
if !matches!(value, Constant::Bool(true)) {
return;
}

let mut check = Check::new(
violations::IfExprWithFalseTrue(unparse_expr(test, checker.style)),
Range::from_located(expr),
);
if checker.patch(check.kind.code()) {
check.amend(Fix::replacement(
unparse_expr(
&create_expr(ExprKind::UnaryOp {
op: Unaryop::Not,
operand: Box::new(create_expr(test.node.clone())),
}),
checker.style,
),
expr.location,
expr.end_location.unwrap(),
));
}
checker.checks.push(check);
}

/// SIM212
pub fn twisted_arms_in_ifexpr(
checker: &mut Checker,
expr: &Expr,
test: &Expr,
body: &Expr,
orelse: &Expr,
) {
let ExprKind::UnaryOp { op, operand: test_operand } = &test.node else {
return;
};
if !matches!(op, Unaryop::Not) {
return;
}

// Check if the test operand and else branch use the same variable.
let ExprKind::Name { id: test_id, .. } = &test_operand.node else {
return;
};
let ExprKind::Name {id: orelse_id, ..} = &orelse.node else {
return;
};
if !test_id.eq(orelse_id) {
return;
}

let mut check = Check::new(
violations::NegateEqualOp(
unparse_expr(body, checker.style),
unparse_expr(orelse, checker.style),
),
Range::from_located(expr),
);
if checker.patch(check.kind.code()) {
check.amend(Fix::replacement(
unparse_expr(
&create_expr(ExprKind::IfExp {
test: Box::new(create_expr(orelse.node.clone())),
body: Box::new(create_expr(orelse.node.clone())),
orelse: Box::new(create_expr(body.node.clone())),
}),
checker.style,
),
expr.location,
expr.end_location.unwrap(),
));
}
checker.checks.push(check);
}
8 changes: 6 additions & 2 deletions src/flake8_simplify/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ pub use ast_bool_op::{
};
pub use ast_for::convert_loop_to_any_all;
pub use ast_if::{nested_if_statements, return_bool_condition_directly, use_ternary_operator};
pub use ast_ifexp::{
explicit_false_true_in_ifexpr, explicit_true_false_in_ifexpr, twisted_arms_in_ifexpr,
};
pub use ast_unary_op::{double_negation, negation_with_equal_op, negation_with_not_equal_op};
pub use ast_with::multiple_with_statements;
pub use key_in_dict::{key_in_dict_compare, key_in_dict_for};
pub use return_in_try_except_finally::return_in_try_except_finally;
pub use unary_ops::{double_negation, negation_with_equal_op, negation_with_not_equal_op};
pub use use_contextlib_suppress::use_contextlib_suppress;
pub use yoda_conditions::yoda_conditions;

mod ast_bool_op;
mod ast_for;
mod ast_if;
mod ast_ifexp;
mod ast_unary_op;
mod ast_with;
mod key_in_dict;
mod return_in_try_except_finally;
mod unary_ops;
mod use_contextlib_suppress;
mod yoda_conditions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
source: src/flake8_simplify/mod.rs
expression: checks
---
- kind:
IfExprWithTrueFalse: b
location:
row: 1
column: 4
end_location:
row: 1
column: 24
fix:
content: bool(b)
location:
row: 1
column: 4
end_location:
row: 1
column: 24
parent: ~
- kind:
IfExprWithTrueFalse: b != c
location:
row: 3
column: 4
end_location:
row: 3
column: 29
fix:
content: bool(b != c)
location:
row: 3
column: 4
end_location:
row: 3
column: 29
parent: ~
- kind:
IfExprWithTrueFalse: b + c
location:
row: 5
column: 4
end_location:
row: 5
column: 28
fix:
content: bool(b + c)
location:
row: 5
column: 4
end_location:
row: 5
column: 28
parent: ~

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
source: src/flake8_simplify/mod.rs
expression: checks
---
- kind:
IfExprWithFalseTrue: b
location:
row: 1
column: 4
end_location:
row: 1
column: 24
fix:
content: not b
location:
row: 1
column: 4
end_location:
row: 1
column: 24
parent: ~
- kind:
IfExprWithFalseTrue: b != c
location:
row: 3
column: 4
end_location:
row: 3
column: 29
fix:
content: not b != c
location:
row: 3
column: 4
end_location:
row: 3
column: 29
parent: ~
- kind:
IfExprWithFalseTrue: b + c
location:
row: 5
column: 4
end_location:
row: 5
column: 28
fix:
content: not b + c
location:
row: 5
column: 4
end_location:
row: 5
column: 28
parent: ~

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: src/flake8_simplify/mod.rs
expression: checks
---
- kind:
NegateEqualOp:
- b
- a
location:
row: 1
column: 4
end_location:
row: 1
column: 21
fix: ~
parent: ~
- kind:
NegateEqualOp:
- b + c
- a
location:
row: 3
column: 4
end_location:
row: 3
column: 25
fix: ~
parent: ~

Loading