Skip to content

Commit

Permalink
fix: use first issue without inline on mergeLineIssues on multiplie i…
Browse files Browse the repository at this point in the history
…ssues (#3316)

Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
peakle and ldez committed Mar 7, 2024
1 parent 4fea092 commit 1b0dbb0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pkg/result/processors/fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,22 @@ func (f Fixer) mergeLineIssues(lineNum int, lineIssues []result.Issue, origFileL

// check issues first
for ind := range lineIssues {
i := &lineIssues[ind]
if i.LineRange != nil {
li := &lineIssues[ind]

if li.LineRange != nil {
f.log.Infof("Line %d has multiple issues but at least one of them is ranged: %#v", lineNum, lineIssues)
return &lineIssues[0]
}

r := i.Replacement
if r.Inline == nil || len(r.NewLines) != 0 || r.NeedOnlyDelete {
inline := li.Replacement.Inline

if inline == nil || len(li.Replacement.NewLines) != 0 || li.Replacement.NeedOnlyDelete {
f.log.Infof("Line %d has multiple issues but at least one of them isn't inline: %#v", lineNum, lineIssues)
return &lineIssues[0]
return li
}

if r.Inline.StartCol < 0 || r.Inline.Length <= 0 || r.Inline.StartCol+r.Inline.Length > len(origLine) {
f.log.Warnf("Line %d (%q) has invalid inline fix: %#v, %#v", lineNum, origLine, i, r.Inline)
if inline.StartCol < 0 || inline.Length <= 0 || inline.StartCol+inline.Length > len(origLine) {
f.log.Warnf("Line %d (%q) has invalid inline fix: %#v, %#v", lineNum, origLine, li, inline)
return nil
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/testdata/configs/multiple-issues-fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters-settings:
gofumpt:
extra-rules: true

11 changes: 11 additions & 0 deletions test/testdata/fix/in/multiple-issues-fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//golangcitest:args -Egocritic,gofumpt
//golangcitest:config_path testdata/configs/multiple-issues-fix.yml
//golangcitest:expected_exitcode 0
package p

import "fmt"

func main() {
//standard greeting
fmt.Println("hello world")
}
11 changes: 11 additions & 0 deletions test/testdata/fix/out/multiple-issues-fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//golangcitest:args -Egocritic,gofumpt
//golangcitest:config_path testdata/configs/multiple-issues-fix.yml
//golangcitest:expected_exitcode 0
package p

import "fmt"

func main() {
// standard greeting
fmt.Println("hello world")
}

0 comments on commit 1b0dbb0

Please sign in to comment.