nolintlint: remove empty line in unused directive replacement (#4973) · golangci/golangci-lint@3797ed9 (original) (raw)

File tree

2 files changed

lines changed

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -252,12 +252,19 @@ func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) {
252 252
253 253 // when detecting unused directives, we send all the directives through and filter them out in the nolint processor
254 254 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,
258 265 Length: end.Column - pos.Column,
259 266 NewString: "",
260 - },
267 + }
261 268 }
262 269
263 270 if len(linters) == 0 {
Original file line number Diff line number Diff line change
@@ -188,6 +188,25 @@ func foo() {
188 188 },
189 189 },
190 190 },
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 + },
191 210 {
192 211 desc: "needs unused with multiple specific linters does not generate replacements",
193 212 needs: NeedsUnused,