From bc945a157b87b3c5f6db03e416daa90c894521d6 Mon Sep 17 00:00:00 2001 From: Fabrice Rabaute Date: Wed, 1 Aug 2018 15:54:59 -0700 Subject: [PATCH] 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. --- pkg/golinters/lll.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/golinters/lll.go b/pkg/golinters/lll.go index 80ebd5c9..0a8ad046 100644 --- a/pkg/golinters/lll.go +++ b/pkg/golinters/lll.go @@ -53,7 +53,29 @@ func (lint Lll) getIssuesForFile(filename string, maxLineLen int, tabSpaces stri } if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("can't scan file %s: %s", filename, err) + if err == bufio.ErrTooLong && maxLineLen < bufio.MaxScanTokenSize { + // 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. + res = append(res, result.Issue{ + Pos: token.Position{ + Filename: filename, + Line: lineNumber, + Column: 1, + }, + Text: fmt.Sprintf("line is more than %d characters", bufio.MaxScanTokenSize), + FromLinter: lint.Name(), + }) + } else { + return nil, fmt.Errorf("can't scan file %s: %s", filename, err) + } } return res, nil