From 9aa5199c318d4a35497b8baf57e3b03d0cdea4bf Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Thu, 2 Mar 2023 03:09:05 +0100 Subject: [PATCH] lintcmd: export fields necessary for gob encoding, again Updates: gh-1370 Closes: gh-1372 (cherry picked from commit cc140e9b3719aadd3b628b7a7b00523681c8b34d) --- lintcmd/cmd.go | 18 +++++++++--------- lintcmd/directives.go | 2 +- lintcmd/format.go | 2 +- lintcmd/lint.go | 30 ++++++++++++++++-------------- lintcmd/sarif.go | 2 +- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/lintcmd/cmd.go b/lintcmd/cmd.go index de5ca5339..d2b3d4cb0 100644 --- a/lintcmd/cmd.go +++ b/lintcmd/cmd.go @@ -565,7 +565,7 @@ func mergeRuns(runs []run) []diagnostic { var relevantDiagnostics []diagnostic for _, r := range runs { for _, diag := range r.diagnostics { - switch diag.mergeIf { + switch diag.MergeIf { case lint.MergeIfAny: relevantDiagnostics = append(relevantDiagnostics, diag) case lint.MergeIfAll: @@ -607,8 +607,8 @@ func (cmd *Command) printDiagnostics(cs []*lint.Analyzer, diagnostics []diagnost if di.Message != dj.Message { return di.Message < dj.Message } - if di.buildName != dj.buildName { - return di.buildName < dj.buildName + if di.BuildName != dj.BuildName { + return di.BuildName < dj.BuildName } return di.Category < dj.Category }) @@ -617,7 +617,7 @@ func (cmd *Command) printDiagnostics(cs []*lint.Analyzer, diagnostics []diagnost diagnostics[0], } builds := []map[string]struct{}{ - {diagnostics[0].buildName: {}}, + {diagnostics[0].BuildName: {}}, } for _, diag := range diagnostics[1:] { // We may encounter duplicate diagnostics because one file @@ -626,11 +626,11 @@ func (cmd *Command) printDiagnostics(cs []*lint.Analyzer, diagnostics []diagnost if !filtered[len(filtered)-1].equal(diag) { if filtered[len(filtered)-1].descriptor() == diag.descriptor() { // Diagnostics only differ in build name, track new name - builds[len(filtered)-1][diag.buildName] = struct{}{} + builds[len(filtered)-1][diag.BuildName] = struct{}{} } else { filtered = append(filtered, diag) builds = append(builds, map[string]struct{}{}) - builds[len(filtered)-1][diag.buildName] = struct{}{} + builds[len(filtered)-1][diag.BuildName] = struct{}{} } } } @@ -642,7 +642,7 @@ func (cmd *Command) printDiagnostics(cs []*lint.Analyzer, diagnostics []diagnost names = append(names, k) } sort.Strings(names) - filtered[i].buildName = strings.Join(names, ",") + filtered[i].BuildName = strings.Join(names, ",") } diagnostics = filtered } @@ -693,14 +693,14 @@ func (cmd *Command) printDiagnostics(cs []*lint.Analyzer, diagnostics []diagnost if diag.Category == "compile" && cmd.flags.debugNoCompileErrors { continue } - if diag.severity == severityIgnored && !cmd.flags.showIgnored { + if diag.Severity == severityIgnored && !cmd.flags.showIgnored { numIgnored++ continue } if shouldExit[diag.Category] { numErrors++ } else { - diag.severity = severityWarning + diag.Severity = severityWarning numWarnings++ } notIgnored = append(notIgnored, diag) diff --git a/lintcmd/directives.go b/lintcmd/directives.go index bbee508ff..3076be543 100644 --- a/lintcmd/directives.go +++ b/lintcmd/directives.go @@ -22,7 +22,7 @@ func parseDirectives(dirs []runner.SerializedDirective) ([]ignore, []diagnostic) Message: "malformed linter directive; missing the required reason field?", Category: "compile", }, - severity: severityError, + Severity: severityError, } diagnostics = append(diagnostics, p) continue diff --git a/lintcmd/format.go b/lintcmd/format.go index c1a092a61..8a23c3c9f 100644 --- a/lintcmd/format.go +++ b/lintcmd/format.go @@ -89,7 +89,7 @@ func (o jsonFormatter) Format(_ []*lint.Analyzer, ps []diagnostic) { Related []related `json:"related,omitempty"` }{ Code: p.Category, - Severity: p.severity.String(), + Severity: p.Severity.String(), Location: location{ File: p.Position.Filename, Line: p.Position.Line, diff --git a/lintcmd/lint.go b/lintcmd/lint.go index 437102627..50a2fd16a 100644 --- a/lintcmd/lint.go +++ b/lintcmd/lint.go @@ -151,7 +151,7 @@ func (l *linter) run(bconf buildConfig) (lintResult, error) { } res, err := l.lint(r, cfg, l.opts.patterns) for i := range res.Diagnostics { - res.Diagnostics[i].buildName = bconf.Name + res.Diagnostics[i].BuildName = bconf.Name } return res, err } @@ -214,7 +214,7 @@ func (l *linter) lint(r *runner.Runner, cfg *packages.Config, patterns []string) a := l.analyzers[diag.Category] // Some diag.Category don't map to analyzers, such as "staticcheck" if a != nil { - filtered[i].mergeIf = a.Doc.MergeIf + filtered[i].MergeIf = a.Doc.MergeIf } } out.Diagnostics = append(out.Diagnostics, filtered...) @@ -262,7 +262,7 @@ func (l *linter) lint(r *runner.Runner, cfg *packages.Config, patterns []string) Message: fmt.Sprintf("%s %s is unused", uo.obj.Kind, uo.obj.Name), Category: "U1000", }, - mergeIf: lint.MergeIfAll, + MergeIf: lint.MergeIfAll, }) } @@ -302,7 +302,7 @@ func filterIgnored(diagnostics []diagnostic, res runner.ResultData, allowedAnaly for i := range diagnostics { diag := &diagnostics[i] if ig.match(*diag) { - diag.severity = severityIgnored + diag.Severity = severityIgnored } } @@ -396,9 +396,11 @@ func (s severity) String() string { // diagnostic represents a diagnostic in some source code. type diagnostic struct { runner.Diagnostic - severity severity - mergeIf lint.MergeStrategy - buildName string + + // These fields are exported so that we can gob encode them. + Severity severity + MergeIf lint.MergeStrategy + BuildName string } func (p diagnostic) equal(o diagnostic) bool { @@ -406,14 +408,14 @@ func (p diagnostic) equal(o diagnostic) bool { p.End == o.End && p.Message == o.Message && p.Category == o.Category && - p.severity == o.severity && - p.mergeIf == o.mergeIf && - p.buildName == o.buildName + p.Severity == o.Severity && + p.MergeIf == o.MergeIf && + p.BuildName == o.BuildName } func (p *diagnostic) String() string { - if p.buildName != "" { - return fmt.Sprintf("%s [%s] (%s)", p.Message, p.buildName, p.Category) + if p.BuildName != "" { + return fmt.Sprintf("%s [%s] (%s)", p.Message, p.BuildName, p.Category) } else { return fmt.Sprintf("%s (%s)", p.Message, p.Category) } @@ -460,7 +462,7 @@ func failed(res runner.Result) []diagnostic { Message: msg, Category: "compile", }, - severity: severityError, + Severity: severityError, } diagnostics = append(diagnostics, diag) case error: @@ -470,7 +472,7 @@ func failed(res runner.Result) []diagnostic { Message: e.Error(), Category: "compile", }, - severity: severityError, + Severity: severityError, } diagnostics = append(diagnostics, diag) } diff --git a/lintcmd/sarif.go b/lintcmd/sarif.go index eac00e8fc..5231d3ffb 100644 --- a/lintcmd/sarif.go +++ b/lintcmd/sarif.go @@ -345,7 +345,7 @@ func (o *sarifFormatter) Format(checks []*lint.Analyzer, diagnostics []diagnosti }) } - if p.severity == severityIgnored { + if p.Severity == severityIgnored { // Note that GitHub does not support suppressions, which is why Staticcheck still requires the -show-ignored flag to be set for us to emit ignored diagnostics. r.Suppressions = []sarif.Suppression{{