Skip to content

Commit 2135d9a

Browse files
authored
revive: fix suggested fixes (#6295)
1 parent e2fbe0a commit 2135d9a

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

pkg/golinters/revive/revive.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,11 @@ func (w *wrapper) toIssue(pass *analysis.Pass, failure *lint.Failure) *goanalysi
155155
if failure.Filename() == f.Name() {
156156
issue.SuggestedFixes = []analysis.SuggestedFix{{
157157
TextEdits: []analysis.TextEdit{{
158-
Pos: f.LineStart(failure.Position.Start.Line),
159-
End: goanalysis.EndOfLinePos(f, failure.Position.End.Line),
160-
NewText: []byte(failure.ReplacementLine),
158+
Pos: f.LineStart(failure.Position.Start.Line),
159+
End: goanalysis.EndOfLinePos(f, failure.Position.End.Line),
160+
// ReplacementLine doesn't contain the full line (missing newline), so we have to add a newline.
161+
// Also `failure.Position.End.Offset` is at the end of the node but not the line.
162+
NewText: []byte(failure.ReplacementLine + "\n"),
161163
}},
162164
}}
163165
}

pkg/golinters/revive/testdata/fix/in/revive.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ import (
1212
func _() error {
1313
return errors.New(fmt.Sprintf("foo: %d", math.MaxInt))
1414
}
15+
16+
func _() (int, error) {
17+
c := errors.New(fmt.Sprintf("bar: %d", math.MaxInt))
18+
return 1, c
19+
}
20+
21+
func _() (int, error) {
22+
if c := errors.New(fmt.Sprintf("bar: %d", math.MaxInt)); c != nil {
23+
return 0, c
24+
}
25+
return 1, nil
26+
}

pkg/golinters/revive/testdata/fix/out/revive.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ import (
1212
func _() error {
1313
return fmt.Errorf("foo: %d", math.MaxInt)
1414
}
15+
16+
func _() (int, error) {
17+
c := fmt.Errorf("bar: %d", math.MaxInt)
18+
return 1, c
19+
}
20+
21+
func _() (int, error) {
22+
if c := fmt.Errorf("bar: %d", math.MaxInt); c != nil {
23+
return 0, c
24+
}
25+
return 1, nil
26+
}

0 commit comments

Comments
 (0)