Skip to content

Commit

Permalink
sqf: fix lint s08 on escaped %% in format strings (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabstMirror authored Nov 9, 2024
1 parent 9f5d763 commit ab357f8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion libs/sqf/src/analyze/lints/s08_format_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ fn get_format_problem(input: &str, extra_args: usize) -> Option<String> {
let mut token_active = false;
let mut token_start = 0;
for (i, c) in format.chars().enumerate() {
let outside_token = !token_active || i > token_start;
if token_active && !c.is_ascii_digit() {
token_active = false;
if i > token_start {
Expand All @@ -133,7 +134,7 @@ fn get_format_problem(input: &str, extra_args: usize) -> Option<String> {
));
}
}
if !token_active && c == '%' {
if !token_active && c == '%' && outside_token {
token_active = true;
token_start = i + 1;
}
Expand Down
9 changes: 9 additions & 0 deletions libs/sqf/tests/lints/s08_format_args.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ format ["%1", 1, 2, 3]; // unused args
format ["%1%2", 1]; // undefined tokens
format ["%5", 1, 2 ,3 ,4, 5]; // skipped tokens
formatText ["me too %1"];

format ["%1%%", 100];
format ["%%%1%%", 100];
format ["%%%%%%%%%%%%%%%%"];
format ["this code is 99% bug free"]; // non-escaped
format ["%1%"]; // non-escaped (prioity over unused)
format ["%%1", 1]; // unused args
format ["%%%1", 1];
format ["%%%1%%%2 %% %%%3%%%%", 1, 2, 3];
21 changes: 21 additions & 0 deletions libs/sqf/tests/snapshots/lints__simple_s08_format_args.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,24 @@ expression: lint(stringify! (s08_format_args))
│
9 │ formatText ["me too %1"];
│ ^^^^^^^^^^^^^^^^^^^^^^^ format string: undefined tokens [used "%1", passed 0]


error[L-S08]: format string: non-escaped "%" [at index 16]
┌─ s08_format_args.sqf:14:1
│
14 │ format ["this code is 99% bug free"]; // non-escaped
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ format string: non-escaped "%" [at index 16]


error[L-S08]: format string: non-escaped "%" [at index 3]
┌─ s08_format_args.sqf:15:1
│
15 │ format ["%1%"]; // non-escaped (prioity over unused)
│ ^^^^^^^^^^^^^ format string: non-escaped "%" [at index 3]


error[L-S08]: format string: unused args [used "%0", passed 1]
┌─ s08_format_args.sqf:16:1
│
16 │ format ["%%1", 1]; // unused args
│ ^^^^^^^^^^^^^^^^ format string: unused args [used "%0", passed 1]

0 comments on commit ab357f8

Please sign in to comment.