From db6956dd09352d0af68e86a7ded90b7bf1c2b1a4 Mon Sep 17 00:00:00 2001 From: "Andrew S. Brown" Date: Sun, 27 Dec 2020 18:30:54 -0800 Subject: [PATCH] Assume single whitespace was intended when multiple leading whitespaces are encountered --- pkg/golinters/nolintlint/nolintlint.go | 17 ++++++++++------- pkg/golinters/nolintlint/nolintlint_test.go | 12 ++++++------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/pkg/golinters/nolintlint/nolintlint.go b/pkg/golinters/nolintlint/nolintlint.go index 4a1d8d61cb36..3c66a91791fa 100644 --- a/pkg/golinters/nolintlint/nolintlint.go +++ b/pkg/golinters/nolintlint/nolintlint.go @@ -181,18 +181,21 @@ func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) { // check for, report and eliminate leading spaces so we can check for other issues if len(leadingSpace) > 0 { + removeWhitespace := &result.Replacement{ + Inline: &result.InlineFix{ + StartCol: pos.Column + 1, + Length: len(leadingSpace), + NewString: "", + }, + } if (l.needs & NeedsMachineOnly) != 0 { issue := NotMachine{BaseIssue: base} - issue.BaseIssue.replacement = &result.Replacement{ - Inline: &result.InlineFix{ - StartCol: pos.Column - 1, - Length: len(leadingSpace) + 2, - NewString: "//", - }, - } + issue.BaseIssue.replacement = removeWhitespace issues = append(issues, issue) } else if len(leadingSpace) > 1 { issue := ExtraLeadingSpace{BaseIssue: base} + issue.BaseIssue.replacement = removeWhitespace + issue.BaseIssue.replacement.Inline.NewString = " " // assume a single space was intended issues = append(issues, issue) } } diff --git a/pkg/golinters/nolintlint/nolintlint_test.go b/pkg/golinters/nolintlint/nolintlint_test.go index 373da1332078..ad9554540584 100644 --- a/pkg/golinters/nolintlint/nolintlint_test.go +++ b/pkg/golinters/nolintlint/nolintlint_test.go @@ -102,9 +102,9 @@ func foo() { "directive `// nolint` should be written without leading space as `//nolint` at testing.go:5:9", &result.Replacement{ Inline: &result.InlineFix{ - StartCol: 8, - Length: 3, - NewString: "//", + StartCol: 10, + Length: 1, + NewString: "", }, }, }, @@ -124,9 +124,9 @@ func foo() { "directive `// nolint` should not have more than one leading space at testing.go:5:9", &result.Replacement{ Inline: &result.InlineFix{ - StartCol: 8, - Length: 4, - NewString: "//", + StartCol: 10, + Length: 2, + NewString: " ", }, }, },