/
bool_ops.rs
159 lines (147 loc) · 4.46 KB
/
bool_ops.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use rustpython_ast::{Boolop, Constant, Expr, ExprKind, Unaryop};
use crate::ast::types::Range;
use crate::autofix::Fix;
use crate::checkers::ast::Checker;
use crate::registry::{Check, CheckCode, CheckKind};
fn is_same_expr<'a>(a: &'a Expr, b: &'a Expr) -> Option<&'a str> {
if let (ExprKind::Name { id: a, .. }, ExprKind::Name { id: b, .. }) = (&a.node, &b.node) {
if a == b {
return Some(a);
}
}
None
}
/// SIM220
pub fn a_and_not_a(checker: &mut Checker, expr: &Expr) {
let ExprKind::BoolOp { op: Boolop::And, values, } = &expr.node else {
return;
};
if values.len() < 2 {
return;
}
// Collect all negated and non-negated expressions.
let mut negated_expr = vec![];
let mut non_negated_expr = vec![];
for expr in values {
if let ExprKind::UnaryOp {
op: Unaryop::Not,
operand,
} = &expr.node
{
negated_expr.push(operand);
} else {
non_negated_expr.push(expr);
}
}
if negated_expr.is_empty() {
return;
}
for negate_expr in negated_expr {
for non_negate_expr in &non_negated_expr {
if let Some(id) = is_same_expr(negate_expr, non_negate_expr) {
let mut check = Check::new(
CheckKind::AAndNotA(id.to_string()),
Range::from_located(expr),
);
if checker.patch(&CheckCode::SIM220) {
check.amend(Fix::replacement(
"False".to_string(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
}
}
}
/// SIM221
pub fn a_or_not_a(checker: &mut Checker, expr: &Expr) {
let ExprKind::BoolOp { op: Boolop::Or, values, } = &expr.node else {
return;
};
if values.len() < 2 {
return;
}
// Collect all negated and non-negated expressions.
let mut negated_expr = vec![];
let mut non_negated_expr = vec![];
for expr in values {
if let ExprKind::UnaryOp {
op: Unaryop::Not,
operand,
} = &expr.node
{
negated_expr.push(operand);
} else {
non_negated_expr.push(expr);
}
}
if negated_expr.is_empty() {
return;
}
for negate_expr in negated_expr {
for non_negate_expr in &non_negated_expr {
if let Some(id) = is_same_expr(negate_expr, non_negate_expr) {
let mut check = Check::new(
CheckKind::AOrNotA(id.to_string()),
Range::from_located(expr),
);
if checker.patch(&CheckCode::SIM220) {
check.amend(Fix::replacement(
"True".to_string(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
}
}
}
/// SIM222
pub fn or_true(checker: &mut Checker, expr: &Expr) {
let ExprKind::BoolOp { op: Boolop::Or, values, } = &expr.node else {
return;
};
for value in values {
if let ExprKind::Constant {
value: Constant::Bool(true),
..
} = &value.node
{
let mut check = Check::new(CheckKind::OrTrue, Range::from_located(value));
if checker.patch(&CheckCode::SIM223) {
check.amend(Fix::replacement(
"True".to_string(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
}
}
/// SIM223
pub fn and_false(checker: &mut Checker, expr: &Expr) {
let ExprKind::BoolOp { op: Boolop::And, values, } = &expr.node else {
return;
};
for value in values {
if let ExprKind::Constant {
value: Constant::Bool(false),
..
} = &value.node
{
let mut check = Check::new(CheckKind::AndFalse, Range::from_located(value));
if checker.patch(&CheckCode::SIM223) {
check.amend(Fix::replacement(
"False".to_string(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
}
}