Skip to content

Commit 138ab91

Browse files
authored
[flake8-simplify] Fix SIM905 autofix for rsplit creating a reversed list literal (astral-sh#18045)
## Summary Fixes astral-sh#18042
1 parent 550b8be commit 138ab91

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM905.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
" a*a a*a a ".split("*", -1) # [" a", "a a", "a a "]
3333
"".split() # []
34-
"""
34+
"""
3535
""".split() # []
3636
" ".split() # []
3737
"/abc/".split() # ["/abc/"]
@@ -73,7 +73,7 @@
7373

7474
# negatives
7575

76-
# invalid values should not cause panic
76+
# invalid values should not cause panic
7777
"a,b,c,d".split(maxsplit="hello")
7878
"a,b,c,d".split(maxsplit=-"hello")
7979

@@ -106,3 +106,7 @@
106106
'''itemC'''
107107
"'itemD'"
108108
""".split()
109+
110+
# https://github.com/astral-sh/ruff/issues/18042
111+
print("a,b".rsplit(","))
112+
print("a,b,c".rsplit(",", 1))

crates/ruff_linter/src/rules/flake8_simplify/rules/split_static_string.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,20 @@ fn split_sep(
186186
let list_items: Vec<&str> = if let Ok(split_n) = usize::try_from(max_split) {
187187
match direction {
188188
Direction::Left => value.splitn(split_n + 1, sep_value).collect(),
189-
Direction::Right => value.rsplitn(split_n + 1, sep_value).collect(),
189+
Direction::Right => {
190+
let mut items: Vec<&str> = value.rsplitn(split_n + 1, sep_value).collect();
191+
items.reverse();
192+
items
193+
}
190194
}
191195
} else {
192196
match direction {
193197
Direction::Left => value.split(sep_value).collect(),
194-
Direction::Right => value.rsplit(sep_value).collect(),
198+
Direction::Right => {
199+
let mut items: Vec<&str> = value.rsplit(sep_value).collect();
200+
items.reverse();
201+
items
202+
}
195203
}
196204
};
197205

crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM905_SIM905.py.snap

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ SIM905.py:32:1: SIM905 [*] Consider using a list literal instead of `str.split`
352352
32 | " a*a a*a a ".split("*", -1) # [" a", "a a", "a a "]
353353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM905
354354
33 | "".split() # []
355-
34 | """
355+
34 | """
356356
|
357357
= help: Replace with list literal
358358

@@ -363,15 +363,15 @@ SIM905.py:32:1: SIM905 [*] Consider using a list literal instead of `str.split`
363363
32 |-" a*a a*a a ".split("*", -1) # [" a", "a a", "a a "]
364364
32 |+[" a", "a a", "a a "] # [" a", "a a", "a a "]
365365
33 33 | "".split() # []
366-
34 34 | """
366+
34 34 | """
367367
35 35 | """.split() # []
368368

369369
SIM905.py:33:1: SIM905 [*] Consider using a list literal instead of `str.split`
370370
|
371371
32 | " a*a a*a a ".split("*", -1) # [" a", "a a", "a a "]
372372
33 | "".split() # []
373373
| ^^^^^^^^^^ SIM905
374-
34 | """
374+
34 | """
375375
35 | """.split() # []
376376
|
377377
= help: Replace with list literal
@@ -382,15 +382,15 @@ SIM905.py:33:1: SIM905 [*] Consider using a list literal instead of `str.split`
382382
32 32 | " a*a a*a a ".split("*", -1) # [" a", "a a", "a a "]
383383
33 |-"".split() # []
384384
33 |+[] # []
385-
34 34 | """
385+
34 34 | """
386386
35 35 | """.split() # []
387387
36 36 | " ".split() # []
388388

389389
SIM905.py:34:1: SIM905 [*] Consider using a list literal instead of `str.split`
390390
|
391391
32 | " a*a a*a a ".split("*", -1) # [" a", "a a", "a a "]
392392
33 | "".split() # []
393-
34 | / """
393+
34 | / """
394394
35 | | """.split() # []
395395
| |___________^ SIM905
396396
36 | " ".split() # []
@@ -402,7 +402,7 @@ SIM905.py:34:1: SIM905 [*] Consider using a list literal instead of `str.split`
402402
31 31 |
403403
32 32 | " a*a a*a a ".split("*", -1) # [" a", "a a", "a a "]
404404
33 33 | "".split() # []
405-
34 |-"""
405+
34 |-"""
406406
35 |-""".split() # []
407407
34 |+[] # []
408408
36 35 | " ".split() # []
@@ -411,7 +411,7 @@ SIM905.py:34:1: SIM905 [*] Consider using a list literal instead of `str.split`
411411

412412
SIM905.py:36:1: SIM905 [*] Consider using a list literal instead of `str.split`
413413
|
414-
34 | """
414+
34 | """
415415
35 | """.split() # []
416416
36 | " ".split() # []
417417
| ^^^^^^^^^^^^^^^^^ SIM905
@@ -422,7 +422,7 @@ SIM905.py:36:1: SIM905 [*] Consider using a list literal instead of `str.split`
422422

423423
Safe fix
424424
33 33 | "".split() # []
425-
34 34 | """
425+
34 34 | """
426426
35 35 | """.split() # []
427427
36 |-" ".split() # []
428428
36 |+[] # []
@@ -442,7 +442,7 @@ SIM905.py:37:1: SIM905 [*] Consider using a list literal instead of `str.split`
442442
= help: Replace with list literal
443443

444444
Safe fix
445-
34 34 | """
445+
34 34 | """
446446
35 35 | """.split() # []
447447
36 36 | " ".split() # []
448448
37 |-"/abc/".split() # ["/abc/"]
@@ -854,6 +854,8 @@ SIM905.py:103:1: SIM905 [*] Consider using a list literal instead of `str.split`
854854
107 | | "'itemD'"
855855
108 | | """.split()
856856
| |___________^ SIM905
857+
109 |
858+
110 | # https://github.com/astral-sh/ruff/issues/18042
857859
|
858860
= help: Replace with list literal
859861

@@ -868,3 +870,39 @@ SIM905.py:103:1: SIM905 [*] Consider using a list literal instead of `str.split`
868870
107 |-"'itemD'"
869871
108 |-""".split()
870872
103 |+['"itemA"', "'itemB'", "'''itemC'''", "\"'itemD'\""]
873+
109 104 |
874+
110 105 | # https://github.com/astral-sh/ruff/issues/18042
875+
111 106 | print("a,b".rsplit(","))
876+
877+
SIM905.py:111:7: SIM905 [*] Consider using a list literal instead of `str.split`
878+
|
879+
110 | # https://github.com/astral-sh/ruff/issues/18042
880+
111 | print("a,b".rsplit(","))
881+
| ^^^^^^^^^^^^^^^^^ SIM905
882+
112 | print("a,b,c".rsplit(",", 1))
883+
|
884+
= help: Replace with list literal
885+
886+
Safe fix
887+
108 108 | """.split()
888+
109 109 |
889+
110 110 | # https://github.com/astral-sh/ruff/issues/18042
890+
111 |-print("a,b".rsplit(","))
891+
111 |+print(["a", "b"])
892+
112 112 | print("a,b,c".rsplit(",", 1))
893+
894+
SIM905.py:112:7: SIM905 [*] Consider using a list literal instead of `str.split`
895+
|
896+
110 | # https://github.com/astral-sh/ruff/issues/18042
897+
111 | print("a,b".rsplit(","))
898+
112 | print("a,b,c".rsplit(",", 1))
899+
| ^^^^^^^^^^^^^^^^^^^^^^ SIM905
900+
|
901+
= help: Replace with list literal
902+
903+
Safe fix
904+
109 109 |
905+
110 110 | # https://github.com/astral-sh/ruff/issues/18042
906+
111 111 | print("a,b".rsplit(","))
907+
112 |-print("a,b,c".rsplit(",", 1))
908+
112 |+print(["a,b", "c"])

0 commit comments

Comments
 (0)