Skip to content

Commit

Permalink
[flake8-simplify] Mark fixes as unsafe (SIM201, SIM202) (#15626)
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo authored Jan 21, 2025
1 parent 13a6b56 commit fce4adf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
14 changes: 12 additions & 2 deletions crates/ruff_linter/src/rules/flake8_simplify/rules/ast_unary_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ use crate::checkers::ast::Checker;
/// 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
/// - [Python documentation: Comparisons](https://docs.python.org/3/reference/expressions.html#comparisons)
#[derive(ViolationMetadata)]
Expand Down Expand Up @@ -62,6 +67,11 @@ impl AlwaysFixableViolation for NegateEqualOp {
/// 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
/// - [Python documentation: Comparisons](https://docs.python.org/3/reference/expressions.html#comparisons)
#[derive(ViolationMetadata)]
Expand Down Expand Up @@ -181,7 +191,7 @@ pub(crate) fn negation_with_equal_op(
comparators: comparators.clone(),
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()),
expr.range(),
)));
Expand Down Expand Up @@ -236,7 +246,7 @@ pub(crate) fn negation_with_not_equal_op(
comparators: comparators.clone(),
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()),
expr.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
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`
|
Expand All @@ -11,7 +10,7 @@ SIM201.py:2:4: SIM201 [*] Use `a != b` instead of `not a == b`
|
= help: Replace with `!=` operator

Safe fix
Unsafe fix
1 1 | # SIM201
2 |-if not a == b:
2 |+if a != b:
Expand All @@ -28,7 +27,7 @@ SIM201.py:6:4: SIM201 [*] Use `a != b + c` instead of `not a == b + c`
|
= help: Replace with `!=` operator

Safe fix
Unsafe fix
3 3 | pass
4 4 |
5 5 | # SIM201
Expand All @@ -47,7 +46,7 @@ SIM201.py:10:4: SIM201 [*] Use `a + b != c` instead of `not a + b == c`
|
= help: Replace with `!=` operator

Safe fix
Unsafe fix
7 7 | pass
8 8 |
9 9 | # SIM201
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
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`
|
Expand All @@ -11,7 +10,7 @@ SIM202.py:2:4: SIM202 [*] Use `a == b` instead of `not a != b`
|
= help: Replace with `==` operator

Safe fix
Unsafe fix
1 1 | # SIM202
2 |-if not a != b:
2 |+if a == b:
Expand All @@ -28,7 +27,7 @@ SIM202.py:6:4: SIM202 [*] Use `a == b + c` instead of `not a != b + c`
|
= help: Replace with `==` operator

Safe fix
Unsafe fix
3 3 | pass
4 4 |
5 5 | # SIM202
Expand All @@ -47,7 +46,7 @@ SIM202.py:10:4: SIM202 [*] Use `a + b == c` instead of `not a + b != c`
|
= help: Replace with `==` operator

Safe fix
Unsafe fix
7 7 | pass
8 8 |
9 9 | # SIM202
Expand Down

0 comments on commit fce4adf

Please sign in to comment.