-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify SIM201, SIM202, SIM208 (#1666)
Flake8 simplify #998 SIM201, SIM202 and SIM208 is done here with fixes. Note: SIM203 == E713 Co-authored-by: Charlie Marsh <[email protected]>
- Loading branch information
1 parent
0a940b3
commit 1392170
Showing
15 changed files
with
408 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
if not a == b: # SIM201 | ||
pass | ||
|
||
if not a == (b + c): # SIM201 | ||
pass | ||
|
||
if not (a + b) == c: # SIM201 | ||
pass | ||
|
||
if not a != b: # OK | ||
pass | ||
|
||
if a == b: # OK | ||
pass | ||
|
||
if not a == b: # OK | ||
raise ValueError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
if not a != b: # SIM202 | ||
pass | ||
|
||
if not a != (b + c): # SIM202 | ||
pass | ||
|
||
if not (a + b) != c: # SIM202 | ||
pass | ||
|
||
if not a == b: # OK | ||
pass | ||
|
||
if a != b: # OK | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
if not (not a): # SIM208 | ||
pass | ||
|
||
if not (not (a == b)): # SIM208 | ||
pass | ||
|
||
if not a: # OK | ||
pass | ||
|
||
if not a == b: # OK | ||
pass | ||
|
||
if not a != b: # OK | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -950,6 +950,10 @@ | |
"SIM117", | ||
"SIM118", | ||
"SIM2", | ||
"SIM20", | ||
"SIM201", | ||
"SIM202", | ||
"SIM208", | ||
"SIM22", | ||
"SIM220", | ||
"SIM221", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
use rustpython_ast::{Cmpop, Expr, ExprKind, Stmt, StmtKind, 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, CheckKind}; | ||
|
||
fn is_exception_check(stmt: &Stmt) -> bool { | ||
let StmtKind::If {test: _, body, orelse: _} = &stmt.node else { | ||
return false; | ||
}; | ||
if body.len() != 1 { | ||
return false; | ||
} | ||
if matches!(body[0].node, StmtKind::Raise { .. }) { | ||
return true; | ||
} | ||
false | ||
} | ||
|
||
/// SIM201 | ||
pub fn negation_with_equal_op(checker: &mut Checker, expr: &Expr, op: &Unaryop, operand: &Expr) { | ||
if !matches!(op, Unaryop::Not) { | ||
return; | ||
} | ||
let ExprKind::Compare{ left, ops, comparators} = &operand.node else { | ||
return; | ||
}; | ||
if !matches!(&ops[..], [Cmpop::Eq]) { | ||
return; | ||
} | ||
if is_exception_check(checker.current_stmt()) { | ||
return; | ||
} | ||
|
||
let mut check = Check::new( | ||
CheckKind::NegateEqualOp( | ||
unparse_expr(left, checker.style), | ||
unparse_expr(&comparators[0], checker.style), | ||
), | ||
Range::from_located(operand), | ||
); | ||
if checker.patch(check.kind.code()) { | ||
check.amend(Fix::replacement( | ||
unparse_expr( | ||
&create_expr(ExprKind::Compare { | ||
left: left.clone(), | ||
ops: vec![Cmpop::NotEq], | ||
comparators: comparators.clone(), | ||
}), | ||
checker.style, | ||
), | ||
expr.location, | ||
expr.end_location.unwrap(), | ||
)); | ||
} | ||
checker.add_check(check); | ||
} | ||
|
||
/// SIM202 | ||
pub fn negation_with_not_equal_op( | ||
checker: &mut Checker, | ||
expr: &Expr, | ||
op: &Unaryop, | ||
operand: &Expr, | ||
) { | ||
if !matches!(op, Unaryop::Not) { | ||
return; | ||
} | ||
let ExprKind::Compare{ left, ops, comparators} = &operand.node else { | ||
return; | ||
}; | ||
if !matches!(&ops[..], [Cmpop::NotEq]) { | ||
return; | ||
} | ||
if is_exception_check(checker.current_stmt()) { | ||
return; | ||
} | ||
|
||
let mut check = Check::new( | ||
CheckKind::NegateNotEqualOp( | ||
unparse_expr(left, checker.style), | ||
unparse_expr(&comparators[0], checker.style), | ||
), | ||
Range::from_located(operand), | ||
); | ||
if checker.patch(check.kind.code()) { | ||
check.amend(Fix::replacement( | ||
unparse_expr( | ||
&create_expr(ExprKind::Compare { | ||
left: left.clone(), | ||
ops: vec![Cmpop::Eq], | ||
comparators: comparators.clone(), | ||
}), | ||
checker.style, | ||
), | ||
expr.location, | ||
expr.end_location.unwrap(), | ||
)); | ||
} | ||
checker.add_check(check); | ||
} | ||
|
||
/// SIM208 | ||
pub fn double_negation(checker: &mut Checker, expr: &Expr, op: &Unaryop, operand: &Expr) { | ||
if !matches!(op, Unaryop::Not) { | ||
return; | ||
} | ||
let ExprKind::UnaryOp { op: operand_op, operand } = &operand.node else { | ||
return; | ||
}; | ||
if !matches!(operand_op, Unaryop::Not) { | ||
return; | ||
} | ||
|
||
let mut check = Check::new( | ||
CheckKind::DoubleNegation(operand.to_string()), | ||
Range::from_located(operand), | ||
); | ||
if checker.patch(check.kind.code()) { | ||
check.amend(Fix::replacement( | ||
unparse_expr(operand, checker.style), | ||
expr.location, | ||
expr.end_location.unwrap(), | ||
)); | ||
} | ||
checker.add_check(check); | ||
} |
62 changes: 62 additions & 0 deletions
62
src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM201_SIM201.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
source: src/flake8_simplify/mod.rs | ||
expression: checks | ||
--- | ||
- kind: | ||
NegateEqualOp: | ||
- a | ||
- b | ||
location: | ||
row: 1 | ||
column: 7 | ||
end_location: | ||
row: 1 | ||
column: 13 | ||
fix: | ||
content: a != b | ||
location: | ||
row: 1 | ||
column: 3 | ||
end_location: | ||
row: 1 | ||
column: 13 | ||
parent: ~ | ||
- kind: | ||
NegateEqualOp: | ||
- a | ||
- b + c | ||
location: | ||
row: 4 | ||
column: 7 | ||
end_location: | ||
row: 4 | ||
column: 19 | ||
fix: | ||
content: a != b + c | ||
location: | ||
row: 4 | ||
column: 3 | ||
end_location: | ||
row: 4 | ||
column: 19 | ||
parent: ~ | ||
- kind: | ||
NegateEqualOp: | ||
- a + b | ||
- c | ||
location: | ||
row: 7 | ||
column: 7 | ||
end_location: | ||
row: 7 | ||
column: 19 | ||
fix: | ||
content: a + b != c | ||
location: | ||
row: 7 | ||
column: 3 | ||
end_location: | ||
row: 7 | ||
column: 19 | ||
parent: ~ | ||
|
6 changes: 6 additions & 0 deletions
6
src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM201_SIM201_2.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: src/flake8_simplify/mod.rs | ||
expression: checks | ||
--- | ||
[] | ||
|
62 changes: 62 additions & 0 deletions
62
src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM202_SIM202.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
source: src/flake8_simplify/mod.rs | ||
expression: checks | ||
--- | ||
- kind: | ||
NegateNotEqualOp: | ||
- a | ||
- b | ||
location: | ||
row: 1 | ||
column: 7 | ||
end_location: | ||
row: 1 | ||
column: 13 | ||
fix: | ||
content: a == b | ||
location: | ||
row: 1 | ||
column: 3 | ||
end_location: | ||
row: 1 | ||
column: 13 | ||
parent: ~ | ||
- kind: | ||
NegateNotEqualOp: | ||
- a | ||
- b + c | ||
location: | ||
row: 4 | ||
column: 7 | ||
end_location: | ||
row: 4 | ||
column: 19 | ||
fix: | ||
content: a == b + c | ||
location: | ||
row: 4 | ||
column: 3 | ||
end_location: | ||
row: 4 | ||
column: 19 | ||
parent: ~ | ||
- kind: | ||
NegateNotEqualOp: | ||
- a + b | ||
- c | ||
location: | ||
row: 7 | ||
column: 7 | ||
end_location: | ||
row: 7 | ||
column: 19 | ||
fix: | ||
content: a + b == c | ||
location: | ||
row: 7 | ||
column: 3 | ||
end_location: | ||
row: 7 | ||
column: 19 | ||
parent: ~ | ||
|
Oops, something went wrong.