Skip to content

Commit

Permalink
Avoid index-out-of-bands panic for positional placeholders (#4872)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored and konstin committed Jun 13, 2023
1 parent 013747a commit bd8e1db
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions crates/ruff/resources/test/fixtures/pyflakes/F523.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# With modified indexes
"{1}{2}".format(1, 2, 3) # F523, # F524
"{1}{3}".format(1, 2, 3, 4) # F523, # F524
"{1} {8}".format(0, 1) # F523, # F524

# Not fixable
(''
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/resources/test/fixtures/pyflakes/F524.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
"{0} {bar}".format(1) # F524
"{0} {bar}".format() # F524
"{bar} {0}".format() # F524
"{1} {8}".format(0, 1)
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pyflakes/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn update_field_types(format_string: &FormatString, index_map: &[usize]) -> Stri
let new_field_name = FieldName::parse(field_name).unwrap();
let mut new_field_name_string = match new_field_name.field_type {
FieldType::Auto => String::new(),
FieldType::Index(i) => index_map[i].to_string(),
FieldType::Index(i) => index_map.get(i).unwrap_or(&i).to_string(),
FieldType::Keyword(keyword) => keyword,
};
for field_name_part in &new_field_name.parts {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ F523.py:27:1: F523 [*] `.format` call has unused arguments at position(s): 0
28 | "{1}{2}".format(1, 2, 3) # F523, # F524
| ^^^^^^^^^^^^^^^^^^^^^^^^ F523
29 | "{1}{3}".format(1, 2, 3, 4) # F523, # F524
30 | "{1} {8}".format(0, 1) # F523, # F524
|
= help: Remove extra positional arguments at position(s): 0

Expand All @@ -261,17 +262,16 @@ F523.py:27:1: F523 [*] `.format` call has unused arguments at position(s): 0
27 |-"{1}{2}".format(1, 2, 3) # F523, # F524
27 |+"{0}{1}".format(2, 3) # F523, # F524
28 28 | "{1}{3}".format(1, 2, 3, 4) # F523, # F524
29 29 |
30 30 | # Not fixable
29 29 | "{1} {8}".format(0, 1) # F523, # F524
30 30 |

F523.py:28:1: F523 [*] `.format` call has unused arguments at position(s): 0, 2
|
28 | # With modified indexes
29 | "{1}{2}".format(1, 2, 3) # F523, # F524
30 | "{1}{3}".format(1, 2, 3, 4) # F523, # F524
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ F523
31 |
32 | # Not fixable
31 | "{1} {8}".format(0, 1) # F523, # F524
|
= help: Remove extra positional arguments at position(s): 0, 2

Expand All @@ -281,16 +281,37 @@ F523.py:28:1: F523 [*] `.format` call has unused arguments at position(s): 0, 2
27 27 | "{1}{2}".format(1, 2, 3) # F523, # F524
28 |-"{1}{3}".format(1, 2, 3, 4) # F523, # F524
28 |+"{0}{1}".format(2, 4) # F523, # F524
29 29 |
30 30 | # Not fixable
31 31 | (''
29 29 | "{1} {8}".format(0, 1) # F523, # F524
30 30 |
31 31 | # Not fixable

F523.py:31:2: F523 `.format` call has unused arguments at position(s): 0
F523.py:29:1: F523 [*] `.format` call has unused arguments at position(s): 0
|
31 | # Not fixable
32 | (''
29 | "{1}{2}".format(1, 2, 3) # F523, # F524
30 | "{1}{3}".format(1, 2, 3, 4) # F523, # F524
31 | "{1} {8}".format(0, 1) # F523, # F524
| ^^^^^^^^^^^^^^^^^^^^^^ F523
32 |
33 | # Not fixable
|
= help: Remove extra positional arguments at position(s): 0

Suggested fix
26 26 | # With modified indexes
27 27 | "{1}{2}".format(1, 2, 3) # F523, # F524
28 28 | "{1}{3}".format(1, 2, 3, 4) # F523, # F524
29 |-"{1} {8}".format(0, 1) # F523, # F524
29 |+"{0} {8}".format(1) # F523, # F524
30 30 |
31 31 | # Not fixable
32 32 | (''

F523.py:32:2: F523 `.format` call has unused arguments at position(s): 0
|
32 | # Not fixable
33 | (''
| __^
33 | | .format(2))
34 | | .format(2))
| |__________^ F523
|
= help: Remove extra positional arguments at position(s): 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ F524.py:5:1: F524 `.format` call is missing argument(s) for placeholder(s): 0, b
7 | "{0} {bar}".format() # F524
| ^^^^^^^^^^^^^^^^^^^^ F524
8 | "{bar} {0}".format() # F524
9 | "{1} {8}".format(0, 1)
|

F524.py:6:1: F524 `.format` call is missing argument(s) for placeholder(s): 0, bar
Expand All @@ -53,6 +54,15 @@ F524.py:6:1: F524 `.format` call is missing argument(s) for placeholder(s): 0, b
7 | "{0} {bar}".format() # F524
8 | "{bar} {0}".format() # F524
| ^^^^^^^^^^^^^^^^^^^^ F524
9 | "{1} {8}".format(0, 1)
|

F524.py:7:1: F524 `.format` call is missing argument(s) for placeholder(s): 8
|
7 | "{0} {bar}".format() # F524
8 | "{bar} {0}".format() # F524
9 | "{1} {8}".format(0, 1)
| ^^^^^^^^^^^^^^^^^^^^^^ F524
|


0 comments on commit bd8e1db

Please sign in to comment.