Skip to content

Commit 99e4566

Browse files
authored
Mark FURB118 fix as unsafe (#13613)
Closes #13421
1 parent 7ad07c2 commit 99e4566

File tree

2 files changed

+49
-43
lines changed

2 files changed

+49
-43
lines changed

crates/ruff_linter/src/rules/refurb/rules/reimplemented_operator.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ use crate::checkers::ast::Checker;
1717
use crate::importer::{ImportRequest, Importer};
1818

1919
/// ## What it does
20-
/// Checks for lambda expressions and function definitions that can be replaced
21-
/// with a function from the `operator` module.
20+
/// Checks for lambda expressions and function definitions that can be replaced with a function from
21+
/// the `operator` module.
2222
///
2323
/// ## Why is this bad?
24-
/// The `operator` module provides functions that implement the same functionality
25-
/// as the corresponding operators. For example, `operator.add` is equivalent to
26-
/// `lambda x, y: x + y`. Using the functions from the `operator` module is more
27-
/// concise and communicates the intent of the code more clearly.
24+
/// The `operator` module provides functions that implement the same functionality as the
25+
/// corresponding operators. For example, `operator.add` is equivalent to `lambda x, y: x + y`.
26+
/// Using the functions from the `operator` module is more concise and communicates the intent of
27+
/// the code more clearly.
2828
///
2929
/// ## Example
3030
/// ```python
@@ -42,6 +42,12 @@ use crate::importer::{ImportRequest, Importer};
4242
/// nums = [1, 2, 3]
4343
/// total = functools.reduce(operator.add, nums)
4444
/// ```
45+
///
46+
/// ## Fix safety
47+
/// This fix is usually safe, but if the lambda is called with keyword arguments, e.g.,
48+
/// `add = lambda x, y: x + y; add(x=1, y=2)`, replacing the lambda with an operator function, e.g.,
49+
/// `operator.add`, will cause the call to raise a `TypeError`, as functions in `operator` do not allow
50+
/// keyword arguments.
4551
#[violation]
4652
pub struct ReimplementedOperator {
4753
operator: Operator,
@@ -177,7 +183,7 @@ impl FunctionLike<'_> {
177183
} else {
178184
format!("{binding}({})", operator.args.join(", "))
179185
};
180-
Ok(Some(Fix::safe_edits(
186+
Ok(Some(Fix::unsafe_edits(
181187
Edit::range_replacement(content, self.range()),
182188
[edit],
183189
)))

crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB118_FURB118.py.snap

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ FURB118.py:2:13: FURB118 [*] Use `operator.invert` instead of defining a lambda
1111
|
1212
= help: Replace with `operator.invert`
1313

14-
Safe fix
14+
Unsafe fix
1515
1 1 | # Errors.
1616
2 |-op_bitnot = lambda x: ~x
1717
2 |+import operator
@@ -31,7 +31,7 @@ FURB118.py:3:10: FURB118 [*] Use `operator.not_` instead of defining a lambda
3131
|
3232
= help: Replace with `operator.not_`
3333

34-
Safe fix
34+
Unsafe fix
3535
1 1 | # Errors.
3636
2 |+import operator
3737
2 3 | op_bitnot = lambda x: ~x
@@ -51,7 +51,7 @@ FURB118.py:4:10: FURB118 [*] Use `operator.pos` instead of defining a lambda
5151
|
5252
= help: Replace with `operator.pos`
5353

54-
Safe fix
54+
Unsafe fix
5555
1 1 | # Errors.
5656
2 |+import operator
5757
2 3 | op_bitnot = lambda x: ~x
@@ -73,7 +73,7 @@ FURB118.py:5:10: FURB118 [*] Use `operator.neg` instead of defining a lambda
7373
|
7474
= help: Replace with `operator.neg`
7575

76-
Safe fix
76+
Unsafe fix
7777
1 1 | # Errors.
7878
2 |+import operator
7979
2 3 | op_bitnot = lambda x: ~x
@@ -96,7 +96,7 @@ FURB118.py:7:10: FURB118 [*] Use `operator.add` instead of defining a lambda
9696
|
9797
= help: Replace with `operator.add`
9898

99-
Safe fix
99+
Unsafe fix
100100
1 1 | # Errors.
101101
2 |+import operator
102102
2 3 | op_bitnot = lambda x: ~x
@@ -120,7 +120,7 @@ FURB118.py:8:10: FURB118 [*] Use `operator.sub` instead of defining a lambda
120120
|
121121
= help: Replace with `operator.sub`
122122

123-
Safe fix
123+
Unsafe fix
124124
1 1 | # Errors.
125125
2 |+import operator
126126
2 3 | op_bitnot = lambda x: ~x
@@ -146,7 +146,7 @@ FURB118.py:9:11: FURB118 [*] Use `operator.mul` instead of defining a lambda
146146
|
147147
= help: Replace with `operator.mul`
148148

149-
Safe fix
149+
Unsafe fix
150150
1 1 | # Errors.
151151
2 |+import operator
152152
2 3 | op_bitnot = lambda x: ~x
@@ -173,7 +173,7 @@ FURB118.py:10:14: FURB118 [*] Use `operator.matmul` instead of defining a lambda
173173
|
174174
= help: Replace with `operator.matmul`
175175

176-
Safe fix
176+
Unsafe fix
177177
1 1 | # Errors.
178178
2 |+import operator
179179
2 3 | op_bitnot = lambda x: ~x
@@ -200,7 +200,7 @@ FURB118.py:11:14: FURB118 [*] Use `operator.truediv` instead of defining a lambd
200200
|
201201
= help: Replace with `operator.truediv`
202202

203-
Safe fix
203+
Unsafe fix
204204
1 1 | # Errors.
205205
2 |+import operator
206206
2 3 | op_bitnot = lambda x: ~x
@@ -227,7 +227,7 @@ FURB118.py:12:10: FURB118 [*] Use `operator.mod` instead of defining a lambda
227227
|
228228
= help: Replace with `operator.mod`
229229

230-
Safe fix
230+
Unsafe fix
231231
1 1 | # Errors.
232232
2 |+import operator
233233
2 3 | op_bitnot = lambda x: ~x
@@ -254,7 +254,7 @@ FURB118.py:13:10: FURB118 [*] Use `operator.pow` instead of defining a lambda
254254
|
255255
= help: Replace with `operator.pow`
256256

257-
Safe fix
257+
Unsafe fix
258258
1 1 | # Errors.
259259
2 |+import operator
260260
2 3 | op_bitnot = lambda x: ~x
@@ -281,7 +281,7 @@ FURB118.py:14:13: FURB118 [*] Use `operator.lshift` instead of defining a lambda
281281
|
282282
= help: Replace with `operator.lshift`
283283

284-
Safe fix
284+
Unsafe fix
285285
1 1 | # Errors.
286286
2 |+import operator
287287
2 3 | op_bitnot = lambda x: ~x
@@ -308,7 +308,7 @@ FURB118.py:15:13: FURB118 [*] Use `operator.rshift` instead of defining a lambda
308308
|
309309
= help: Replace with `operator.rshift`
310310

311-
Safe fix
311+
Unsafe fix
312312
1 1 | # Errors.
313313
2 |+import operator
314314
2 3 | op_bitnot = lambda x: ~x
@@ -335,7 +335,7 @@ FURB118.py:16:12: FURB118 [*] Use `operator.or_` instead of defining a lambda
335335
|
336336
= help: Replace with `operator.or_`
337337

338-
Safe fix
338+
Unsafe fix
339339
1 1 | # Errors.
340340
2 |+import operator
341341
2 3 | op_bitnot = lambda x: ~x
@@ -362,7 +362,7 @@ FURB118.py:17:10: FURB118 [*] Use `operator.xor` instead of defining a lambda
362362
|
363363
= help: Replace with `operator.xor`
364364

365-
Safe fix
365+
Unsafe fix
366366
1 1 | # Errors.
367367
2 |+import operator
368368
2 3 | op_bitnot = lambda x: ~x
@@ -388,7 +388,7 @@ FURB118.py:18:13: FURB118 [*] Use `operator.and_` instead of defining a lambda
388388
|
389389
= help: Replace with `operator.and_`
390390

391-
Safe fix
391+
Unsafe fix
392392
1 1 | # Errors.
393393
2 |+import operator
394394
2 3 | op_bitnot = lambda x: ~x
@@ -415,7 +415,7 @@ FURB118.py:19:15: FURB118 [*] Use `operator.floordiv` instead of defining a lamb
415415
|
416416
= help: Replace with `operator.floordiv`
417417

418-
Safe fix
418+
Unsafe fix
419419
1 1 | # Errors.
420420
2 |+import operator
421421
2 3 | op_bitnot = lambda x: ~x
@@ -442,7 +442,7 @@ FURB118.py:21:9: FURB118 [*] Use `operator.eq` instead of defining a lambda
442442
|
443443
= help: Replace with `operator.eq`
444444

445-
Safe fix
445+
Unsafe fix
446446
1 1 | # Errors.
447447
2 |+import operator
448448
2 3 | op_bitnot = lambda x: ~x
@@ -468,7 +468,7 @@ FURB118.py:22:9: FURB118 [*] Use `operator.ne` instead of defining a lambda
468468
|
469469
= help: Replace with `operator.ne`
470470

471-
Safe fix
471+
Unsafe fix
472472
1 1 | # Errors.
473473
2 |+import operator
474474
2 3 | op_bitnot = lambda x: ~x
@@ -495,7 +495,7 @@ FURB118.py:23:9: FURB118 [*] Use `operator.lt` instead of defining a lambda
495495
|
496496
= help: Replace with `operator.lt`
497497

498-
Safe fix
498+
Unsafe fix
499499
1 1 | # Errors.
500500
2 |+import operator
501501
2 3 | op_bitnot = lambda x: ~x
@@ -522,7 +522,7 @@ FURB118.py:24:10: FURB118 [*] Use `operator.le` instead of defining a lambda
522522
|
523523
= help: Replace with `operator.le`
524524

525-
Safe fix
525+
Unsafe fix
526526
1 1 | # Errors.
527527
2 |+import operator
528528
2 3 | op_bitnot = lambda x: ~x
@@ -549,7 +549,7 @@ FURB118.py:25:9: FURB118 [*] Use `operator.gt` instead of defining a lambda
549549
|
550550
= help: Replace with `operator.gt`
551551

552-
Safe fix
552+
Unsafe fix
553553
1 1 | # Errors.
554554
2 |+import operator
555555
2 3 | op_bitnot = lambda x: ~x
@@ -576,7 +576,7 @@ FURB118.py:26:10: FURB118 [*] Use `operator.ge` instead of defining a lambda
576576
|
577577
= help: Replace with `operator.ge`
578578

579-
Safe fix
579+
Unsafe fix
580580
1 1 | # Errors.
581581
2 |+import operator
582582
2 3 | op_bitnot = lambda x: ~x
@@ -603,7 +603,7 @@ FURB118.py:27:9: FURB118 [*] Use `operator.is_` instead of defining a lambda
603603
|
604604
= help: Replace with `operator.is_`
605605

606-
Safe fix
606+
Unsafe fix
607607
1 1 | # Errors.
608608
2 |+import operator
609609
2 3 | op_bitnot = lambda x: ~x
@@ -630,7 +630,7 @@ FURB118.py:28:12: FURB118 [*] Use `operator.is_not` instead of defining a lambda
630630
|
631631
= help: Replace with `operator.is_not`
632632

633-
Safe fix
633+
Unsafe fix
634634
1 1 | # Errors.
635635
2 |+import operator
636636
2 3 | op_bitnot = lambda x: ~x
@@ -657,7 +657,7 @@ FURB118.py:29:9: FURB118 [*] Use `operator.contains` instead of defining a lambd
657657
|
658658
= help: Replace with `operator.contains`
659659

660-
Safe fix
660+
Unsafe fix
661661
1 1 | # Errors.
662662
2 |+import operator
663663
2 3 | op_bitnot = lambda x: ~x
@@ -684,7 +684,7 @@ FURB118.py:30:17: FURB118 [*] Use `operator.itemgetter(0)` instead of defining a
684684
|
685685
= help: Replace with `operator.itemgetter(0)`
686686

687-
Safe fix
687+
Unsafe fix
688688
1 1 | # Errors.
689689
2 |+import operator
690690
2 3 | op_bitnot = lambda x: ~x
@@ -711,7 +711,7 @@ FURB118.py:31:17: FURB118 [*] Use `operator.itemgetter(0, 1, 2)` instead of defi
711711
|
712712
= help: Replace with `operator.itemgetter(0, 1, 2)`
713713

714-
Safe fix
714+
Unsafe fix
715715
1 1 | # Errors.
716716
2 |+import operator
717717
2 3 | op_bitnot = lambda x: ~x
@@ -738,7 +738,7 @@ FURB118.py:32:17: FURB118 [*] Use `operator.itemgetter(slice(1, None), 2)` inste
738738
|
739739
= help: Replace with `operator.itemgetter(slice(1, None), 2)`
740740

741-
Safe fix
741+
Unsafe fix
742742
1 1 | # Errors.
743743
2 |+import operator
744744
2 3 | op_bitnot = lambda x: ~x
@@ -765,7 +765,7 @@ FURB118.py:33:17: FURB118 [*] Use `operator.itemgetter(slice(None))` instead of
765765
|
766766
= help: Replace with `operator.itemgetter(slice(None))`
767767

768-
Safe fix
768+
Unsafe fix
769769
1 1 | # Errors.
770770
2 |+import operator
771771
2 3 | op_bitnot = lambda x: ~x
@@ -791,7 +791,7 @@ FURB118.py:34:17: FURB118 [*] Use `operator.itemgetter((0, 1))` instead of defin
791791
|
792792
= help: Replace with `operator.itemgetter((0, 1))`
793793

794-
Safe fix
794+
Unsafe fix
795795
1 1 | # Errors.
796796
2 |+import operator
797797
2 3 | op_bitnot = lambda x: ~x
@@ -816,7 +816,7 @@ FURB118.py:35:17: FURB118 [*] Use `operator.itemgetter((0, 1))` instead of defin
816816
|
817817
= help: Replace with `operator.itemgetter((0, 1))`
818818

819-
Safe fix
819+
Unsafe fix
820820
1 1 | # Errors.
821821
2 |+import operator
822822
2 3 | op_bitnot = lambda x: ~x
@@ -857,7 +857,7 @@ FURB118.py:88:17: FURB118 [*] Use `operator.itemgetter((slice(None), 1))` instea
857857
|
858858
= help: Replace with `operator.itemgetter((slice(None), 1))`
859859

860-
Safe fix
860+
Unsafe fix
861861
1 1 | # Errors.
862862
2 |+import operator
863863
2 3 | op_bitnot = lambda x: ~x
@@ -884,7 +884,7 @@ FURB118.py:89:17: FURB118 [*] Use `operator.itemgetter((1, slice(None)))` instea
884884
|
885885
= help: Replace with `operator.itemgetter((1, slice(None)))`
886886

887-
Safe fix
887+
Unsafe fix
888888
1 1 | # Errors.
889889
2 |+import operator
890890
2 3 | op_bitnot = lambda x: ~x
@@ -910,7 +910,7 @@ FURB118.py:92:17: FURB118 [*] Use `operator.itemgetter((1, slice(None)))` instea
910910
|
911911
= help: Replace with `operator.itemgetter((1, slice(None)))`
912912

913-
Safe fix
913+
Unsafe fix
914914
1 1 | # Errors.
915915
2 |+import operator
916916
2 3 | op_bitnot = lambda x: ~x
@@ -934,7 +934,7 @@ FURB118.py:95:17: FURB118 [*] Use `operator.itemgetter((1, 2))` instead
934934
|
935935
= help: Replace with `operator.itemgetter((1, 2))`
936936

937-
Safe fix
937+
Unsafe fix
938938
1 1 | # Errors.
939939
2 |+import operator
940940
2 3 | op_bitnot = lambda x: ~x

0 commit comments

Comments
 (0)