[`flake8-simplify`] Mark fixes as unsafe (`SIM201`, `SIM202`) (#15626)

This commit is contained in:
InSync 2025-01-22 00:17:48 +07:00 committed by GitHub
parent 13a6b5600b
commit fce4adfd41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 10 deletions

View File

@ -25,6 +25,11 @@ use crate::checkers::ast::Checker;
/// a != b /// a != b
/// ``` /// ```
/// ///
/// ## Fix safety
/// The fix is marked as unsafe, as it might change the behaviour
/// if `a` and/or `b` overrides `__eq__`/`__ne__`
/// in such a manner that they don't return booleans.
///
/// ## References /// ## References
/// - [Python documentation: Comparisons](https://docs.python.org/3/reference/expressions.html#comparisons) /// - [Python documentation: Comparisons](https://docs.python.org/3/reference/expressions.html#comparisons)
#[derive(ViolationMetadata)] #[derive(ViolationMetadata)]
@ -62,6 +67,11 @@ impl AlwaysFixableViolation for NegateEqualOp {
/// a == b /// a == b
/// ``` /// ```
/// ///
/// ## Fix safety
/// The fix is marked as unsafe, as it might change the behaviour
/// if `a` and/or `b` overrides `__ne__`/`__eq__`
/// in such a manner that they don't return booleans.
///
/// ## References /// ## References
/// - [Python documentation: Comparisons](https://docs.python.org/3/reference/expressions.html#comparisons) /// - [Python documentation: Comparisons](https://docs.python.org/3/reference/expressions.html#comparisons)
#[derive(ViolationMetadata)] #[derive(ViolationMetadata)]
@ -181,7 +191,7 @@ pub(crate) fn negation_with_equal_op(
comparators: comparators.clone(), comparators: comparators.clone(),
range: TextRange::default(), range: TextRange::default(),
}; };
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
checker.generator().expr(&node.into()), checker.generator().expr(&node.into()),
expr.range(), expr.range(),
))); )));
@ -236,7 +246,7 @@ pub(crate) fn negation_with_not_equal_op(
comparators: comparators.clone(), comparators: comparators.clone(),
range: TextRange::default(), range: TextRange::default(),
}; };
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
checker.generator().expr(&node.into()), checker.generator().expr(&node.into()),
expr.range(), expr.range(),
))); )));

View File

@ -1,6 +1,5 @@
--- ---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
snapshot_kind: text
--- ---
SIM201.py:2:4: SIM201 [*] Use `a != b` instead of `not a == b` SIM201.py:2:4: SIM201 [*] Use `a != b` instead of `not a == b`
| |
@ -11,7 +10,7 @@ SIM201.py:2:4: SIM201 [*] Use `a != b` instead of `not a == b`
| |
= help: Replace with `!=` operator = help: Replace with `!=` operator
Safe fix Unsafe fix
1 1 | # SIM201 1 1 | # SIM201
2 |-if not a == b: 2 |-if not a == b:
2 |+if a != b: 2 |+if a != b:
@ -28,7 +27,7 @@ SIM201.py:6:4: SIM201 [*] Use `a != b + c` instead of `not a == b + c`
| |
= help: Replace with `!=` operator = help: Replace with `!=` operator
Safe fix Unsafe fix
3 3 | pass 3 3 | pass
4 4 | 4 4 |
5 5 | # SIM201 5 5 | # SIM201
@ -47,7 +46,7 @@ SIM201.py:10:4: SIM201 [*] Use `a + b != c` instead of `not a + b == c`
| |
= help: Replace with `!=` operator = help: Replace with `!=` operator
Safe fix Unsafe fix
7 7 | pass 7 7 | pass
8 8 | 8 8 |
9 9 | # SIM201 9 9 | # SIM201

View File

@ -1,6 +1,5 @@
--- ---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
snapshot_kind: text
--- ---
SIM202.py:2:4: SIM202 [*] Use `a == b` instead of `not a != b` SIM202.py:2:4: SIM202 [*] Use `a == b` instead of `not a != b`
| |
@ -11,7 +10,7 @@ SIM202.py:2:4: SIM202 [*] Use `a == b` instead of `not a != b`
| |
= help: Replace with `==` operator = help: Replace with `==` operator
Safe fix Unsafe fix
1 1 | # SIM202 1 1 | # SIM202
2 |-if not a != b: 2 |-if not a != b:
2 |+if a == b: 2 |+if a == b:
@ -28,7 +27,7 @@ SIM202.py:6:4: SIM202 [*] Use `a == b + c` instead of `not a != b + c`
| |
= help: Replace with `==` operator = help: Replace with `==` operator
Safe fix Unsafe fix
3 3 | pass 3 3 | pass
4 4 | 4 4 |
5 5 | # SIM202 5 5 | # SIM202
@ -47,7 +46,7 @@ SIM202.py:10:4: SIM202 [*] Use `a + b == c` instead of `not a + b != c`
| |
= help: Replace with `==` operator = help: Replace with `==` operator
Safe fix Unsafe fix
7 7 | pass 7 7 | pass
8 8 | 8 8 |
9 9 | # SIM202 9 9 | # SIM202