Skip to content

Commit 38ad8af

Browse files
committed
Fix printf parsing of %#v and %+v
1 parent 4b89282 commit 38ad8af

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

errorlint/printf.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,26 @@ func (pp *printfParser) parseVerb() (*verb, error) {
5353
}
5454

5555
index := -1
56-
switch pp.peek() {
57-
case '%':
58-
pp.next()
59-
return pp.parseVerb()
60-
case '+', '#':
61-
pp.next()
62-
fallthrough
63-
case '[':
64-
var err error
65-
index, err = pp.parseIndex()
66-
if err != nil {
67-
return nil, err
56+
for {
57+
switch pp.peek() {
58+
case '%':
59+
pp.next()
60+
return pp.parseVerb()
61+
case '+', '#':
62+
pp.next()
63+
continue
64+
case '[':
65+
var err error
66+
index, err = pp.parseIndex()
67+
if err != nil {
68+
return nil, err
69+
}
70+
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
71+
pp.parsePrecision()
72+
case 0:
73+
return nil, io.EOF
6874
}
69-
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
70-
pp.parsePrecision()
71-
case 0:
72-
return nil, io.EOF
75+
break
7376
}
7477

7578
format := pp.next()

errorlint/printf_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ func TestPrintfParser(t *testing.T) {
4949
{format: "v", index: 1},
5050
},
5151
},
52+
{
53+
format: "%#v %+v",
54+
verbs: []verb{
55+
{format: "v", index: -1},
56+
{format: "v", index: -1},
57+
},
58+
},
5259
}
5360

5461
for _, tc := range testCases {

0 commit comments

Comments
 (0)