Skip to content

Commit 3797ed9

Browse files
reugnldez
andauthored
nolintlint: remove empty line in unused directive replacement (#4973)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 726b815 commit 3797ed9

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

pkg/golinters/nolintlint/internal/nolintlint.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,19 @@ func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) {
252252

253253
// when detecting unused directives, we send all the directives through and filter them out in the nolint processor
254254
if (l.needs & NeedsUnused) != 0 {
255-
removeNolintCompletely := &result.Replacement{
256-
Inline: &result.InlineFix{
257-
StartCol: pos.Column - 1,
255+
removeNolintCompletely := &result.Replacement{}
256+
257+
startCol := pos.Column - 1
258+
259+
if startCol == 0 {
260+
// if the directive starts from a new line, remove the line
261+
removeNolintCompletely.NeedOnlyDelete = true
262+
} else {
263+
removeNolintCompletely.Inline = &result.InlineFix{
264+
StartCol: startCol,
258265
Length: end.Column - pos.Column,
259266
NewString: "",
260-
},
267+
}
261268
}
262269

263270
if len(linters) == 0 {

pkg/golinters/nolintlint/internal/nolintlint_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ func foo() {
188188
},
189189
},
190190
},
191+
{
192+
desc: "needs unused with one specific linter in a new line generates replacement",
193+
needs: NeedsUnused,
194+
contents: `
195+
package bar
196+
197+
//nolint:somelinter
198+
func foo() {
199+
bad()
200+
}`,
201+
expected: []issueWithReplacement{
202+
{
203+
issue: "directive `//nolint:somelinter` is unused for linter \"somelinter\" at testing.go:4:1",
204+
replacement: &result.Replacement{
205+
NeedOnlyDelete: true,
206+
},
207+
},
208+
},
209+
},
191210
{
192211
desc: "needs unused with multiple specific linters does not generate replacements",
193212
needs: NeedsUnused,

0 commit comments

Comments
 (0)