mirror of https://github.com/astral-sh/ruff
[`flake8-simplify`] Stabilize implicit-`else` simplifications in `needless-bool` (`SIM103`) (#12048)
## Summary See: https://github.com/astral-sh/ruff/pull/10414. This is a good and intuitive change; we just put it in preview because it expanded scope a bit.
This commit is contained in:
parent
fb1d7610ac
commit
6f2e024cc6
|
|
@ -56,7 +56,6 @@ mod tests {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[test_case(Rule::NeedlessBool, Path::new("SIM103.py"))]
|
||||
#[test_case(Rule::YodaConditions, Path::new("SIM300.py"))]
|
||||
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
|
||||
let snapshot = format!(
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ use crate::fix::snippet::SourceCodeSnippet;
|
|||
/// a falsey condition can be replaced with boolean casts.
|
||||
///
|
||||
/// ## Example
|
||||
/// Given:
|
||||
/// ```python
|
||||
/// if x > 0:
|
||||
/// return True
|
||||
|
|
@ -28,17 +29,20 @@ use crate::fix::snippet::SourceCodeSnippet;
|
|||
/// return x > 0
|
||||
/// ```
|
||||
///
|
||||
/// In [preview], this rule will also flag implicit `else` cases, as in:
|
||||
/// Or, given:
|
||||
/// ```python
|
||||
/// if x > 0:
|
||||
/// return True
|
||||
/// return False
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// return x > 0
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [Python documentation: Truth Value Testing](https://docs.python.org/3/library/stdtypes.html#truth-value-testing)
|
||||
///
|
||||
/// [preview]: https://docs.astral.sh/ruff/preview/
|
||||
#[violation]
|
||||
pub struct NeedlessBool {
|
||||
condition: Option<SourceCodeSnippet>,
|
||||
|
|
@ -128,7 +132,7 @@ pub(crate) fn needless_bool(checker: &mut Checker, stmt: &Stmt) {
|
|||
// return True
|
||||
// return False
|
||||
// ```
|
||||
[] if checker.settings.preview.is_enabled() => {
|
||||
[] => {
|
||||
// Fetching the next sibling is expensive, so do some validation early.
|
||||
if is_one_line_return_bool(if_body).is_none() {
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -168,3 +168,94 @@ SIM103.py:91:5: SIM103 [*] Return the condition `not (keys is not None and notic
|
|||
95 92 |
|
||||
96 93 |
|
||||
97 94 | ###
|
||||
|
||||
SIM103.py:104:5: SIM103 [*] Return the condition `bool(a)` directly
|
||||
|
|
||||
102 | def f():
|
||||
103 | # SIM103
|
||||
104 | if a:
|
||||
| _____^
|
||||
105 | | return True
|
||||
106 | | return False
|
||||
| |________________^ SIM103
|
||||
|
|
||||
= help: Replace with `return bool(a)`
|
||||
|
||||
ℹ Unsafe fix
|
||||
101 101 |
|
||||
102 102 | def f():
|
||||
103 103 | # SIM103
|
||||
104 |- if a:
|
||||
105 |- return True
|
||||
106 |- return False
|
||||
104 |+ return bool(a)
|
||||
107 105 |
|
||||
108 106 |
|
||||
109 107 | def f():
|
||||
|
||||
SIM103.py:111:5: SIM103 [*] Return the condition `not a` directly
|
||||
|
|
||||
109 | def f():
|
||||
110 | # SIM103
|
||||
111 | if a:
|
||||
| _____^
|
||||
112 | | return False
|
||||
113 | | return True
|
||||
| |_______________^ SIM103
|
||||
|
|
||||
= help: Replace with `return not a`
|
||||
|
||||
ℹ Unsafe fix
|
||||
108 108 |
|
||||
109 109 | def f():
|
||||
110 110 | # SIM103
|
||||
111 |- if a:
|
||||
112 |- return False
|
||||
113 |- return True
|
||||
111 |+ return not a
|
||||
114 112 |
|
||||
115 113 |
|
||||
116 114 | def f():
|
||||
|
||||
SIM103.py:117:5: SIM103 [*] Return the condition `10 < a` directly
|
||||
|
|
||||
116 | def f():
|
||||
117 | if not 10 < a:
|
||||
| _____^
|
||||
118 | | return False
|
||||
119 | | return True
|
||||
| |_______________^ SIM103
|
||||
|
|
||||
= help: Replace with `return 10 < a`
|
||||
|
||||
ℹ Unsafe fix
|
||||
114 114 |
|
||||
115 115 |
|
||||
116 116 | def f():
|
||||
117 |- if not 10 < a:
|
||||
118 |- return False
|
||||
119 |- return True
|
||||
117 |+ return 10 < a
|
||||
120 118 |
|
||||
121 119 |
|
||||
122 120 | def f():
|
||||
|
||||
SIM103.py:123:5: SIM103 [*] Return the condition `not 10 < a` directly
|
||||
|
|
||||
122 | def f():
|
||||
123 | if 10 < a:
|
||||
| _____^
|
||||
124 | | return False
|
||||
125 | | return True
|
||||
| |_______________^ SIM103
|
||||
|
|
||||
= help: Replace with `return not 10 < a`
|
||||
|
||||
ℹ Unsafe fix
|
||||
120 120 |
|
||||
121 121 |
|
||||
122 122 | def f():
|
||||
123 |- if 10 < a:
|
||||
124 |- return False
|
||||
125 |- return True
|
||||
123 |+ return not 10 < a
|
||||
|
|
|
|||
|
|
@ -1,261 +0,0 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
|
||||
---
|
||||
SIM103.py:3:5: SIM103 [*] Return the condition `bool(a)` directly
|
||||
|
|
||||
1 | def f():
|
||||
2 | # SIM103
|
||||
3 | if a:
|
||||
| _____^
|
||||
4 | | return True
|
||||
5 | | else:
|
||||
6 | | return False
|
||||
| |____________________^ SIM103
|
||||
|
|
||||
= help: Replace with `return bool(a)`
|
||||
|
||||
ℹ Unsafe fix
|
||||
1 1 | def f():
|
||||
2 2 | # SIM103
|
||||
3 |- if a:
|
||||
4 |- return True
|
||||
5 |- else:
|
||||
6 |- return False
|
||||
3 |+ return bool(a)
|
||||
7 4 |
|
||||
8 5 |
|
||||
9 6 | def f():
|
||||
|
||||
SIM103.py:11:5: SIM103 [*] Return the condition `a == b` directly
|
||||
|
|
||||
9 | def f():
|
||||
10 | # SIM103
|
||||
11 | if a == b:
|
||||
| _____^
|
||||
12 | | return True
|
||||
13 | | else:
|
||||
14 | | return False
|
||||
| |____________________^ SIM103
|
||||
|
|
||||
= help: Replace with `return a == b`
|
||||
|
||||
ℹ Unsafe fix
|
||||
8 8 |
|
||||
9 9 | def f():
|
||||
10 10 | # SIM103
|
||||
11 |- if a == b:
|
||||
12 |- return True
|
||||
13 |- else:
|
||||
14 |- return False
|
||||
11 |+ return a == b
|
||||
15 12 |
|
||||
16 13 |
|
||||
17 14 | def f():
|
||||
|
||||
SIM103.py:21:5: SIM103 [*] Return the condition `bool(b)` directly
|
||||
|
|
||||
19 | if a:
|
||||
20 | return 1
|
||||
21 | elif b:
|
||||
| _____^
|
||||
22 | | return True
|
||||
23 | | else:
|
||||
24 | | return False
|
||||
| |____________________^ SIM103
|
||||
|
|
||||
= help: Replace with `return bool(b)`
|
||||
|
||||
ℹ Unsafe fix
|
||||
18 18 | # SIM103
|
||||
19 19 | if a:
|
||||
20 20 | return 1
|
||||
21 |- elif b:
|
||||
22 |- return True
|
||||
23 |- else:
|
||||
24 |- return False
|
||||
21 |+ return bool(b)
|
||||
25 22 |
|
||||
26 23 |
|
||||
27 24 | def f():
|
||||
|
||||
SIM103.py:32:9: SIM103 [*] Return the condition `bool(b)` directly
|
||||
|
|
||||
30 | return 1
|
||||
31 | else:
|
||||
32 | if b:
|
||||
| _________^
|
||||
33 | | return True
|
||||
34 | | else:
|
||||
35 | | return False
|
||||
| |________________________^ SIM103
|
||||
|
|
||||
= help: Replace with `return bool(b)`
|
||||
|
||||
ℹ Unsafe fix
|
||||
29 29 | if a:
|
||||
30 30 | return 1
|
||||
31 31 | else:
|
||||
32 |- if b:
|
||||
33 |- return True
|
||||
34 |- else:
|
||||
35 |- return False
|
||||
32 |+ return bool(b)
|
||||
36 33 |
|
||||
37 34 |
|
||||
38 35 | def f():
|
||||
|
||||
SIM103.py:57:5: SIM103 [*] Return the condition `not a` directly
|
||||
|
|
||||
55 | def f():
|
||||
56 | # SIM103
|
||||
57 | if a:
|
||||
| _____^
|
||||
58 | | return False
|
||||
59 | | else:
|
||||
60 | | return True
|
||||
| |___________________^ SIM103
|
||||
|
|
||||
= help: Replace with `return not a`
|
||||
|
||||
ℹ Unsafe fix
|
||||
54 54 |
|
||||
55 55 | def f():
|
||||
56 56 | # SIM103
|
||||
57 |- if a:
|
||||
58 |- return False
|
||||
59 |- else:
|
||||
60 |- return True
|
||||
57 |+ return not a
|
||||
61 58 |
|
||||
62 59 |
|
||||
63 60 | def f():
|
||||
|
||||
SIM103.py:83:5: SIM103 Return the condition directly
|
||||
|
|
||||
81 | def bool():
|
||||
82 | return False
|
||||
83 | if a:
|
||||
| _____^
|
||||
84 | | return True
|
||||
85 | | else:
|
||||
86 | | return False
|
||||
| |____________________^ SIM103
|
||||
|
|
||||
= help: Inline condition
|
||||
|
||||
SIM103.py:91:5: SIM103 [*] Return the condition `not (keys is not None and notice.key not in keys)` directly
|
||||
|
|
||||
89 | def f():
|
||||
90 | # SIM103
|
||||
91 | if keys is not None and notice.key not in keys:
|
||||
| _____^
|
||||
92 | | return False
|
||||
93 | | else:
|
||||
94 | | return True
|
||||
| |___________________^ SIM103
|
||||
|
|
||||
= help: Replace with `return not (keys is not None and notice.key not in keys)`
|
||||
|
||||
ℹ Unsafe fix
|
||||
88 88 |
|
||||
89 89 | def f():
|
||||
90 90 | # SIM103
|
||||
91 |- if keys is not None and notice.key not in keys:
|
||||
92 |- return False
|
||||
93 |- else:
|
||||
94 |- return True
|
||||
91 |+ return not (keys is not None and notice.key not in keys)
|
||||
95 92 |
|
||||
96 93 |
|
||||
97 94 | ###
|
||||
|
||||
SIM103.py:104:5: SIM103 [*] Return the condition `bool(a)` directly
|
||||
|
|
||||
102 | def f():
|
||||
103 | # SIM103
|
||||
104 | if a:
|
||||
| _____^
|
||||
105 | | return True
|
||||
106 | | return False
|
||||
| |________________^ SIM103
|
||||
|
|
||||
= help: Replace with `return bool(a)`
|
||||
|
||||
ℹ Unsafe fix
|
||||
101 101 |
|
||||
102 102 | def f():
|
||||
103 103 | # SIM103
|
||||
104 |- if a:
|
||||
105 |- return True
|
||||
106 |- return False
|
||||
104 |+ return bool(a)
|
||||
107 105 |
|
||||
108 106 |
|
||||
109 107 | def f():
|
||||
|
||||
SIM103.py:111:5: SIM103 [*] Return the condition `not a` directly
|
||||
|
|
||||
109 | def f():
|
||||
110 | # SIM103
|
||||
111 | if a:
|
||||
| _____^
|
||||
112 | | return False
|
||||
113 | | return True
|
||||
| |_______________^ SIM103
|
||||
|
|
||||
= help: Replace with `return not a`
|
||||
|
||||
ℹ Unsafe fix
|
||||
108 108 |
|
||||
109 109 | def f():
|
||||
110 110 | # SIM103
|
||||
111 |- if a:
|
||||
112 |- return False
|
||||
113 |- return True
|
||||
111 |+ return not a
|
||||
114 112 |
|
||||
115 113 |
|
||||
116 114 | def f():
|
||||
|
||||
SIM103.py:117:5: SIM103 [*] Return the condition `10 < a` directly
|
||||
|
|
||||
116 | def f():
|
||||
117 | if not 10 < a:
|
||||
| _____^
|
||||
118 | | return False
|
||||
119 | | return True
|
||||
| |_______________^ SIM103
|
||||
|
|
||||
= help: Replace with `return 10 < a`
|
||||
|
||||
ℹ Unsafe fix
|
||||
114 114 |
|
||||
115 115 |
|
||||
116 116 | def f():
|
||||
117 |- if not 10 < a:
|
||||
118 |- return False
|
||||
119 |- return True
|
||||
117 |+ return 10 < a
|
||||
120 118 |
|
||||
121 119 |
|
||||
122 120 | def f():
|
||||
|
||||
SIM103.py:123:5: SIM103 [*] Return the condition `not 10 < a` directly
|
||||
|
|
||||
122 | def f():
|
||||
123 | if 10 < a:
|
||||
| _____^
|
||||
124 | | return False
|
||||
125 | | return True
|
||||
| |_______________^ SIM103
|
||||
|
|
||||
= help: Replace with `return not 10 < a`
|
||||
|
||||
ℹ Unsafe fix
|
||||
120 120 |
|
||||
121 121 |
|
||||
122 122 | def f():
|
||||
123 |- if 10 < a:
|
||||
124 |- return False
|
||||
125 |- return True
|
||||
123 |+ return not 10 < a
|
||||
Loading…
Reference in New Issue