Skip to content

Commit 5b7cebe

Browse files
committed
fix(runner): add suggested edit text from linter in display issue text
The tool did not print suggested edits by the linter when displaying the issues. If there is suggested edits by the linter, it should be displayed along with the issue. Closes golangci#2134
1 parent 245257b commit 5b7cebe

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

pkg/golinters/goanalysis/runners.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,18 @@ func buildIssues(diags []Diagnostic, linterNameBuilder func(diag *Diagnostic) st
9090
diag := &diags[i]
9191
linterName := linterNameBuilder(diag)
9292

93+
var fix string
94+
for _, suggestedFix := range diag.SuggestedFixes {
95+
for _, textEdit := range suggestedFix.TextEdits {
96+
fix += string(textEdit.NewText)
97+
}
98+
}
99+
93100
var text string
94101
if diag.Analyzer.Name == linterName {
95102
text = diag.Message
96103
} else {
97-
text = fmt.Sprintf("%s: %s", diag.Analyzer.Name, diag.Message)
104+
text = fmt.Sprintf("%s: %s\n%s", diag.Analyzer.Name, diag.Message, formatSuggestFix(fix))
98105
}
99106

100107
issues = append(issues, result.Issue{
@@ -118,6 +125,13 @@ func buildIssues(diags []Diagnostic, linterNameBuilder func(diag *Diagnostic) st
118125
return issues
119126
}
120127

128+
func formatSuggestFix(fix string) string {
129+
if len(fix) == 0 {
130+
return ""
131+
}
132+
return fmt.Sprintf("\n```\n%s\n```\n", fix)
133+
}
134+
121135
func getIssuesCacheKey(analyzers []*analysis.Analyzer) string {
122136
return "lint/result:" + analyzersHashID(analyzers)
123137
}

0 commit comments

Comments
 (0)