Skip to content

Commit 284447f

Browse files
committed
prettify issue texts
1 parent e58c27e commit 284447f

File tree

19 files changed

+84
-29
lines changed

19 files changed

+84
-29
lines changed

Gopkg.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/gas.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (lint Gas) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issu
3939

4040
res := make([]result.Issue, 0, len(issues))
4141
for _, i := range issues {
42-
text := fmt.Sprintf("%s: %s", i.RuleID, i.What) // TODO: use severity and confidence
42+
text := fmt.Sprintf("%s: %s", i.RuleID, markIdentifiers(i.What)) // TODO: use severity and confidence
4343
var r *result.Range
4444
line, err := strconv.Atoi(i.Line)
4545
if err != nil {

pkg/golinters/gofmt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ func (g Gofmt) extractIssuesFromPatch(patch string, log logutils.Log) ([]result.
8383
}
8484
}
8585

86-
text := "File is not gofmt-ed with -s"
86+
text := "File is not `gofmt`-ed with `-s`"
8787
if g.UseGoimports {
88-
text = "File is not goimports-ed"
88+
text = "File is not `goimports`-ed"
8989
}
9090
i := result.Issue{
9191
FromLinter: g.Name(),

pkg/golinters/golint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (g Golint) lintPkg(minConfidence float64, files []*ast.File, fset *token.Fi
6060
if p.Confidence >= minConfidence {
6161
issues = append(issues, result.Issue{
6262
Pos: p.Position,
63-
Text: p.Text,
63+
Text: markIdentifiers(p.Text),
6464
FromLinter: g.Name(),
6565
})
6666
// TODO: use p.Link and p.Category

pkg/golinters/govet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (g Govet) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue
5353
for _, i := range govetIssues {
5454
res = append(res, result.Issue{
5555
Pos: i.Pos,
56-
Text: i.Message,
56+
Text: markIdentifiers(i.Message),
5757
FromLinter: g.Name(),
5858
})
5959
}

pkg/golinters/interfacer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (lint Interfacer) Run(ctx context.Context, lintCtx *linter.Context) ([]resu
3737
pos := lintCtx.SSAProgram.Fset.Position(i.Pos())
3838
res = append(res, result.Issue{
3939
Pos: pos,
40-
Text: i.Message(),
40+
Text: markIdentifiers(i.Message()),
4141
FromLinter: lint.Name(),
4242
})
4343
}

pkg/golinters/lll.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func (lint Lll) getIssuesForFile(filename string, maxLineLen int, tabSpaces stri
4343
Pos: token.Position{
4444
Filename: filename,
4545
Line: lineNumber,
46-
Column: 1,
4746
},
4847
Text: fmt.Sprintf("line is %d characters", lineLen),
4948
FromLinter: lint.Name(),

pkg/golinters/megacheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (m Megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.I
112112
for _, i := range issues {
113113
res = append(res, result.Issue{
114114
Pos: i.Position,
115-
Text: i.Text,
115+
Text: markIdentifiers(i.Text),
116116
FromLinter: m.Name(),
117117
})
118118
}

pkg/golinters/typecheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (lint TypeCheck) parseError(srcErr error) (*result.Issue, error) {
6161
Line: line,
6262
Column: column,
6363
},
64-
Text: message,
64+
Text: markIdentifiers(message),
6565
FromLinter: lint.Name(),
6666
}, nil
6767
}

pkg/golinters/unparam.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (lint Unparam) Run(ctx context.Context, lintCtx *linter.Context) ([]result.
3636
for _, i := range unparamIssues {
3737
res = append(res, result.Issue{
3838
Pos: lintCtx.Program.Fset.Position(i.Pos()),
39-
Text: i.Message(),
39+
Text: markIdentifiers(i.Message()),
4040
FromLinter: lint.Name(),
4141
})
4242
}

pkg/golinters/utils.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"fmt"
55
"go/ast"
66
"go/token"
7+
"regexp"
78
"strings"
9+
"sync"
810

911
"github.com/golangci/golangci-lint/pkg/lint/linter"
1012
"github.com/golangci/golangci-lint/pkg/packages"
@@ -28,6 +30,65 @@ func formatCodeBlock(code string, _ *config.Config) string {
2830
return fmt.Sprintf("```\n%s\n```", code)
2931
}
3032

33+
type replacePattern struct {
34+
re string
35+
repl string
36+
}
37+
38+
type replaceRegexp struct {
39+
re *regexp.Regexp
40+
repl string
41+
}
42+
43+
var replaceRegexps []replaceRegexp
44+
var replaceRegexpsOnce sync.Once
45+
46+
var replacePatterns = []replacePattern{
47+
// unparam
48+
{`^(\S+) - (\S+) is unused$`, "`${1}` - `${2}` is unused"},
49+
{`^(\S+) - (\S+) always receives (\S+) \((.*)\)$`, "`${1}` - `${2}` always receives `${3}` (`${4}`)"},
50+
{`^(\S+) - (\S+) always receives (.*)$`, "`${1}` - `${2}` always receives `${3}`"},
51+
52+
// interfacer
53+
{`^(\S+) can be (\S+)$`, "`${1}` can be `${2}`"},
54+
55+
// govet
56+
{`^(\S+) arg list ends with redundant newline$`, "`${1}` arg list ends with redundant newline"},
57+
{`^(\S+) composite literal uses unkeyed fields$`, "`${1}` composite literal uses unkeyed fields"},
58+
59+
// gas
60+
{`^Blacklisted import (\S+): weak cryptographic primitive$`,
61+
"Blacklisted import `${1}`: weak cryptographic primitive"},
62+
{`^TLS InsecureSkipVerify set true.$`, "TLS `InsecureSkipVerify` set true."},
63+
64+
// megacheck
65+
{`^this value of (\S+) is never used$`, "this value of `${1}` is never used"},
66+
{`^should use time.Since instead of time.Now().Sub$`,
67+
"should use `time.Since` instead of `time.Now().Sub`"},
68+
{`^(func|const|field|type) (\S+) is unused$`, "${1} `${2}` is unused"},
69+
}
70+
71+
func markIdentifiers(s string) string {
72+
replaceRegexpsOnce.Do(func() {
73+
for _, p := range replacePatterns {
74+
r := replaceRegexp{
75+
re: regexp.MustCompile(p.re),
76+
repl: p.repl,
77+
}
78+
replaceRegexps = append(replaceRegexps, r)
79+
}
80+
})
81+
82+
for _, rr := range replaceRegexps {
83+
rs := rr.re.ReplaceAllString(s, rr.repl)
84+
if rs != s {
85+
return rs
86+
}
87+
}
88+
89+
return s
90+
}
91+
3192
func getASTFilesForPkg(ctx *linter.Context, pkg *packages.Package) ([]*ast.File, *token.FileSet, error) {
3293
filenames := pkg.Files(ctx.Cfg.Run.AnalyzeTests)
3394
files := make([]*ast.File, 0, len(filenames))

pkg/printers/text.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func (p Text) printSourceCode(i *result.Issue) {
6969
}
7070

7171
func (p Text) printUnderLinePointer(i *result.Issue) {
72+
// if column == 0 it means column is unknown (e.g. for gas)
7273
if len(i.SourceLines) != 1 || i.Pos.Column == 0 {
7374
return
7475
}

test/testdata/gas.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package testdata
33

44
import (
5-
"crypto/md5" // ERROR "G501: Blacklisted import crypto/md5: weak cryptographic primitive"
5+
"crypto/md5" // ERROR "G501: Blacklisted import `crypto/md5`: weak cryptographic primitive"
66
"log"
77
)
88

test/testdata/gofmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import "fmt"
55

66
func GofmtNotSimplified() {
77
var x []string
8-
fmt.Print(x[1:len(x)]) // ERROR "File is not gofmt-ed with -s"
8+
fmt.Print(x[1:len(x)]) // ERROR "File is not `gofmt`-ed with `-s`"
99
}

test/testdata/goimports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package testdata
33

44
import (
5-
"fmt" // ERROR "File is not goimports-ed"
5+
"fmt" // ERROR "File is not `goimports`-ed"
66
"github.com/golangci/golangci-lint/pkg/config"
77
)
88

test/testdata/govet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func Govet() error {
10-
return &os.PathError{"first", "path", os.ErrNotExist} // ERROR "os.PathError composite literal uses unkeyed fields"
10+
return &os.PathError{"first", "path", os.ErrNotExist} // ERROR "`os.PathError` composite literal uses unkeyed fields"
1111
}
1212

1313
func GovetShadow(f io.Reader, buf []byte) (err error) {

test/testdata/interfacer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package testdata
33

44
import "io"
55

6-
func InterfacerCheck(f io.ReadCloser) { // ERROR "f can be io.Closer"
6+
func InterfacerCheck(f io.ReadCloser) { // ERROR "`f` can be `io.Closer`"
77
f.Close()
88
}

test/testdata/unparam.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// args: -Eunparam
22
package testdata
33

4-
func unparamUnused(a, b uint) uint { // ERROR "unparamUnused - b is unused"
4+
func unparamUnused(a, b uint) uint { // ERROR "`unparamUnused` - `b` is unused"
55
a++
66
return a
77
}

vendor/github.com/golangci/govet/composite.go

Lines changed: 5 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)