Avoid index-out-of-bands panic for positional placeholders (#4872)

This commit is contained in:
Charlie Marsh 2023-06-05 14:31:47 -04:00 committed by GitHub
parent d31eb87877
commit 8a3a269eef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 12 deletions

View file

@ -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
(''

View file

@ -4,3 +4,4 @@
"{0} {bar}".format(1) # F524
"{0} {bar}".format() # F524
"{bar} {0}".format() # F524
"{1} {8}".format(0, 1)

View file

@ -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 {

View file

@ -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
@ -261,8 +262,8 @@ 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
|
@ -270,8 +271,7 @@ F523.py:28:1: F523 [*] `.format` call has unused arguments at position(s): 0, 2
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
@ -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

View file

@ -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
@ -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
|