From d01272c8affe4f03b2eb72d9d7ebfcbf1331dccc Mon Sep 17 00:00:00 2001 From: yrizhkov Date: Tue, 3 Sep 2024 22:50:05 +0300 Subject: [PATCH 1/2] fix(nolintlint): remove empty line in unused directive replacement --- .../nolintlint/internal/nolintlint.go | 5 +++++ .../nolintlint/internal/nolintlint_test.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/pkg/golinters/nolintlint/internal/nolintlint.go b/pkg/golinters/nolintlint/internal/nolintlint.go index 5fed41cfdfbb..c0134fd84fc2 100644 --- a/pkg/golinters/nolintlint/internal/nolintlint.go +++ b/pkg/golinters/nolintlint/internal/nolintlint.go @@ -259,6 +259,11 @@ func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) { NewString: "", }, } + // if the directive starts from a new line, remove the line + if removeNolintCompletely.Inline.StartCol == 0 { + removeNolintCompletely.NeedOnlyDelete = true + removeNolintCompletely.Inline = nil + } if len(linters) == 0 { issue := UnusedCandidate{BaseIssue: base} diff --git a/pkg/golinters/nolintlint/internal/nolintlint_test.go b/pkg/golinters/nolintlint/internal/nolintlint_test.go index 0fff159285bd..ddc5bde7b1ea 100644 --- a/pkg/golinters/nolintlint/internal/nolintlint_test.go +++ b/pkg/golinters/nolintlint/internal/nolintlint_test.go @@ -188,6 +188,25 @@ func foo() { }, }, }, + { + desc: "needs unused with one specific linter in a new line generates replacement", + needs: NeedsUnused, + contents: ` +package bar + +//nolint:somelinter +func foo() { + bad() +}`, + expected: []issueWithReplacement{ + { + issue: "directive `//nolint:somelinter` is unused for linter \"somelinter\" at testing.go:4:1", + replacement: &result.Replacement{ + NeedOnlyDelete: true, + }, + }, + }, + }, { desc: "needs unused with multiple specific linters does not generate replacements", needs: NeedsUnused, From f1d1bb50f8aee61dabd5f94ab0214d774cc029ea Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 4 Sep 2024 13:42:36 +0200 Subject: [PATCH 2/2] review --- .../nolintlint/internal/nolintlint.go | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/golinters/nolintlint/internal/nolintlint.go b/pkg/golinters/nolintlint/internal/nolintlint.go index c0134fd84fc2..08dd743783c3 100644 --- a/pkg/golinters/nolintlint/internal/nolintlint.go +++ b/pkg/golinters/nolintlint/internal/nolintlint.go @@ -252,17 +252,19 @@ func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) { // when detecting unused directives, we send all the directives through and filter them out in the nolint processor if (l.needs & NeedsUnused) != 0 { - removeNolintCompletely := &result.Replacement{ - Inline: &result.InlineFix{ - StartCol: pos.Column - 1, + removeNolintCompletely := &result.Replacement{} + + startCol := pos.Column - 1 + + if startCol == 0 { + // if the directive starts from a new line, remove the line + removeNolintCompletely.NeedOnlyDelete = true + } else { + removeNolintCompletely.Inline = &result.InlineFix{ + StartCol: startCol, Length: end.Column - pos.Column, NewString: "", - }, - } - // if the directive starts from a new line, remove the line - if removeNolintCompletely.Inline.StartCol == 0 { - removeNolintCompletely.NeedOnlyDelete = true - removeNolintCompletely.Inline = nil + } } if len(linters) == 0 {