Skip to content
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
1 change: 0 additions & 1 deletion crates/ruff_linter/src/rules/flake8_simplify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ mod tests {
Ok(())
}

#[test_case(Rule::NeedlessBool, Path::new("SIM103.py"))]
#[test_case(Rule::IfElseBlockInsteadOfIfExp, Path::new("SIM108.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
Expand Down
25 changes: 10 additions & 15 deletions crates/ruff_linter/src/rules/flake8_simplify/rules/needless_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::fix::snippet::SourceCodeSnippet;
///
/// ## Why is this bad?
/// `if` statements that return `True` for a truthy condition and `False` for
/// a falsey condition can be replaced with boolean casts.
/// a falsy condition can be replaced with boolean casts.
///
/// ## Example
/// Given:
Expand Down Expand Up @@ -42,10 +42,6 @@ use crate::fix::snippet::SourceCodeSnippet;
/// return x > 0
/// ```
///
/// ## Preview
/// In preview, double negations such as `not a != b`, `not a not in b`, `not a is not b`
/// will be simplified to `a == b`, `a in b` and `a is b`, respectively.
///
/// ## References
/// - [Python documentation: Truth Value Testing](https://docs.python.org/3/library/stdtypes.html#truth-value-testing)
#[derive(ViolationMetadata)]
Expand Down Expand Up @@ -222,16 +218,15 @@ pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
left,
comparators,
..
}) if checker.settings.preview.is_enabled()
&& matches!(
ops.as_ref(),
[ast::CmpOp::Eq
| ast::CmpOp::NotEq
| ast::CmpOp::In
| ast::CmpOp::NotIn
| ast::CmpOp::Is
| ast::CmpOp::IsNot]
) =>
}) if matches!(
ops.as_ref(),
[ast::CmpOp::Eq
| ast::CmpOp::NotEq
| ast::CmpOp::In
| ast::CmpOp::NotIn
| ast::CmpOp::Is
| ast::CmpOp::IsNot]
) =>
{
let ([op], [right]) = (ops.as_ref(), comparators.as_ref()) else {
unreachable!("Single comparison with multiple comparators");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,15 @@ SIM103.py:123:5: SIM103 [*] Return the condition `not 10 < a` directly
127 125 |
128 126 | def f():

SIM103.py:129:5: SIM103 [*] Return the condition `not 10 in a` directly
SIM103.py:129:5: SIM103 [*] Return the condition `10 not in a` directly
|
128 | def f():
129 | / if 10 in a:
130 | | return False
131 | | return True
| |_______________^ SIM103
|
= help: Replace with `return not 10 in a`
= help: Replace with `return 10 not in a`

ℹ Unsafe fix
126 126 |
Expand All @@ -269,20 +269,20 @@ SIM103.py:129:5: SIM103 [*] Return the condition `not 10 in a` directly
129 |- if 10 in a:
130 |- return False
131 |- return True
129 |+ return not 10 in a
129 |+ return 10 not in a
132 130 |
133 131 |
134 132 | def f():

SIM103.py:135:5: SIM103 [*] Return the condition `not 10 not in a` directly
SIM103.py:135:5: SIM103 [*] Return the condition `10 in a` directly
|
134 | def f():
135 | / if 10 not in a:
136 | | return False
137 | | return True
| |_______________^ SIM103
|
= help: Replace with `return not 10 not in a`
= help: Replace with `return 10 in a`

ℹ Unsafe fix
132 132 |
Expand All @@ -291,20 +291,20 @@ SIM103.py:135:5: SIM103 [*] Return the condition `not 10 not in a` directly
135 |- if 10 not in a:
136 |- return False
137 |- return True
135 |+ return not 10 not in a
135 |+ return 10 in a
138 136 |
139 137 |
140 138 | def f():

SIM103.py:141:5: SIM103 [*] Return the condition `not a is 10` directly
SIM103.py:141:5: SIM103 [*] Return the condition `a is not 10` directly
|
140 | def f():
141 | / if a is 10:
142 | | return False
143 | | return True
| |_______________^ SIM103
|
= help: Replace with `return not a is 10`
= help: Replace with `return a is not 10`

ℹ Unsafe fix
138 138 |
Expand All @@ -313,20 +313,20 @@ SIM103.py:141:5: SIM103 [*] Return the condition `not a is 10` directly
141 |- if a is 10:
142 |- return False
143 |- return True
141 |+ return not a is 10
141 |+ return a is not 10
144 142 |
145 143 |
146 144 | def f():

SIM103.py:147:5: SIM103 [*] Return the condition `not a is not 10` directly
SIM103.py:147:5: SIM103 [*] Return the condition `a is 10` directly
|
146 | def f():
147 | / if a is not 10:
148 | | return False
149 | | return True
| |_______________^ SIM103
|
= help: Replace with `return not a is not 10`
= help: Replace with `return a is 10`

ℹ Unsafe fix
144 144 |
Expand All @@ -335,20 +335,20 @@ SIM103.py:147:5: SIM103 [*] Return the condition `not a is not 10` directly
147 |- if a is not 10:
148 |- return False
149 |- return True
147 |+ return not a is not 10
147 |+ return a is 10
150 148 |
151 149 |
152 150 | def f():

SIM103.py:153:5: SIM103 [*] Return the condition `not a == 10` directly
SIM103.py:153:5: SIM103 [*] Return the condition `a != 10` directly
|
152 | def f():
153 | / if a == 10:
154 | | return False
155 | | return True
| |_______________^ SIM103
|
= help: Replace with `return not a == 10`
= help: Replace with `return a != 10`

ℹ Unsafe fix
150 150 |
Expand All @@ -357,20 +357,20 @@ SIM103.py:153:5: SIM103 [*] Return the condition `not a == 10` directly
153 |- if a == 10:
154 |- return False
155 |- return True
153 |+ return not a == 10
153 |+ return a != 10
156 154 |
157 155 |
158 156 | def f():

SIM103.py:159:5: SIM103 [*] Return the condition `not a != 10` directly
SIM103.py:159:5: SIM103 [*] Return the condition `a == 10` directly
|
158 | def f():
159 | / if a != 10:
160 | | return False
161 | | return True
| |_______________^ SIM103
|
= help: Replace with `return not a != 10`
= help: Replace with `return a == 10`

ℹ Unsafe fix
156 156 |
Expand All @@ -379,4 +379,4 @@ SIM103.py:159:5: SIM103 [*] Return the condition `not a != 10` directly
159 |- if a != 10:
160 |- return False
161 |- return True
159 |+ return not a != 10
159 |+ return a == 10
Loading
Loading