Skip to content

Commit

Permalink
lintcmd: export fields necessary for gob encoding, again
Browse files Browse the repository at this point in the history
Updates: gh-1370
Closes: gh-1372
(cherry picked from commit cc140e9)
  • Loading branch information
dominikh committed Mar 15, 2023
1 parent 3cd466f commit 9aa5199
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
18 changes: 9 additions & 9 deletions lintcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
})
Expand All @@ -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
Expand All @@ -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{}{}
}
}
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lintcmd/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lintcmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 16 additions & 14 deletions lintcmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -396,24 +396,26 @@ 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 {
return p.Position == o.Position &&
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)
}
Expand Down Expand Up @@ -460,7 +462,7 @@ func failed(res runner.Result) []diagnostic {
Message: msg,
Category: "compile",
},
severity: severityError,
Severity: severityError,
}
diagnostics = append(diagnostics, diag)
case error:
Expand All @@ -470,7 +472,7 @@ func failed(res runner.Result) []diagnostic {
Message: e.Error(),
Category: "compile",
},
severity: severityError,
Severity: severityError,
}
diagnostics = append(diagnostics, diag)
}
Expand Down
2 changes: 1 addition & 1 deletion lintcmd/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{{
Expand Down

0 comments on commit 9aa5199

Please sign in to comment.