Skip to content

Commit bc945a1

Browse files
jfrabautejirfag
authored andcommitted
Improve lll parsing for very long lines
lll is using scanner.Scan() to read the file line by line. scanner.Scan() might fail if the line is longer than bufio.MaxScanTokenSize In the case where the specified maxLineLen is smaller than bufio.MaxScanTokenSize we can return this line as a long line instead of returning an error. The reason for this change is that this case might happen with autogenerated files The go-bindata tool for instance might generate a file with a very long line. In this case, as it's a auto generated file, the warning returned by lll will be ignored. But if we return a linter error here, and this error happens for an autogenerated file the error will be discarded (fine), but all the subsequent errors for lll will be discarded for other files and we'll miss legit error.
1 parent b1948fd commit bc945a1

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

pkg/golinters/lll.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,29 @@ func (lint Lll) getIssuesForFile(filename string, maxLineLen int, tabSpaces stri
5353
}
5454

5555
if err := scanner.Err(); err != nil {
56-
return nil, fmt.Errorf("can't scan file %s: %s", filename, err)
56+
if err == bufio.ErrTooLong && maxLineLen < bufio.MaxScanTokenSize {
57+
// scanner.Scan() might fail if the line is longer than bufio.MaxScanTokenSize
58+
// In the case where the specified maxLineLen is smaller than bufio.MaxScanTokenSize
59+
// we can return this line as a long line instead of returning an error.
60+
// The reason for this change is that this case might happen with autogenerated files
61+
// The go-bindata tool for instance might generate a file with a very long line.
62+
// In this case, as it's a auto generated file, the warning returned by lll will
63+
// be ignored.
64+
// But if we return a linter error here, and this error happens for an autogenerated
65+
// file the error will be discarded (fine), but all the subsequent errors for lll will
66+
// be discarded for other files and we'll miss legit error.
67+
res = append(res, result.Issue{
68+
Pos: token.Position{
69+
Filename: filename,
70+
Line: lineNumber,
71+
Column: 1,
72+
},
73+
Text: fmt.Sprintf("line is more than %d characters", bufio.MaxScanTokenSize),
74+
FromLinter: lint.Name(),
75+
})
76+
} else {
77+
return nil, fmt.Errorf("can't scan file %s: %s", filename, err)
78+
}
5779
}
5880

5981
return res, nil

0 commit comments

Comments
 (0)