diff --git a/pkg/golinters/gofmt/testdata/gofmt_too_many_empty_lines.go b/pkg/golinters/gofmt/testdata/gofmt_too_many_empty_lines.go new file mode 100644 index 000000000000..0d37ce84a44d --- /dev/null +++ b/pkg/golinters/gofmt/testdata/gofmt_too_many_empty_lines.go @@ -0,0 +1,10 @@ +//golangcitest:args -Egofmt +package testdata + +import "fmt" + +// want +4 "File is not properly formatted" +func _() { + fmt.Println("foo") +} + diff --git a/pkg/golinters/gofumpt/testdata/gofumpt_too_many_empty_lines.go b/pkg/golinters/gofumpt/testdata/gofumpt_too_many_empty_lines.go new file mode 100644 index 000000000000..7046a59bc438 --- /dev/null +++ b/pkg/golinters/gofumpt/testdata/gofumpt_too_many_empty_lines.go @@ -0,0 +1,10 @@ +//golangcitest:args -Egofumpt +package testdata + +import "fmt" + +// want +4 "File is not properly formatted" +func _() { + fmt.Println("foo") +} + diff --git a/pkg/golinters/internal/diff.go b/pkg/golinters/internal/diff.go index 458763a018e4..8e5e1f0e735e 100644 --- a/pkg/golinters/internal/diff.go +++ b/pkg/golinters/internal/diff.go @@ -251,7 +251,12 @@ func ExtractDiagnosticFromPatch( } func toDiagnostic(ft *token.File, change Change, adjLine int) analysis.Diagnostic { - start := ft.LineStart(change.From + adjLine) + from := change.From + adjLine + if from > ft.LineCount() { + from = ft.LineCount() + } + + start := ft.LineStart(from) end := goanalysis.EndOfLinePos(ft, change.To+adjLine) diff --git a/pkg/golinters/internal/diff_test.go b/pkg/golinters/internal/diff_test.go index 915832b033cf..1b8dad10db5d 100644 --- a/pkg/golinters/internal/diff_test.go +++ b/pkg/golinters/internal/diff_test.go @@ -19,6 +19,13 @@ func Test_parse(t *testing.T) { log logutils.Log expected []Change }{ + { + diff: "delete_last_line.diff", + expected: []Change{{ + From: 10, + To: 10, + }}, + }, { diff: "delete_only_first_lines.diff", expected: []Change{{ diff --git a/pkg/golinters/internal/testdata/delete_last_line.diff b/pkg/golinters/internal/testdata/delete_last_line.diff new file mode 100644 index 000000000000..55a7cb134f43 --- /dev/null +++ b/pkg/golinters/internal/testdata/delete_last_line.diff @@ -0,0 +1,9 @@ +diff --git i/main.go w/main.go +index ef3cbb7..52a7925 100644 +--- i/main.go ++++ w/main.go +@@ -7,4 +7,3 @@ import ( + func main() { + fmt.Println("hello world") + } +-