Skip to content

Commit 35f9d8e

Browse files
committed
Fix unparam crash on nil constant
1 parent facc3a0 commit 35f9d8e

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

Gopkg.lock

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

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
[[constraint]]
2828
name = "mvdan.cc/unparam"
29-
branch = "master"
29+
branch = "fix-crash-on-nil-constant"
3030
source = "github.com/golangci/unparam"
3131

3232
[[constraint]]

pkg/golinters/unparam.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func (lint Unparam) Run(ctx context.Context, lintCtx *linter.Context) ([]result.
2222
us := &lintCtx.Settings().Unparam
2323

2424
c := &check.Checker{}
25-
c.Algo(us.Algo)
26-
c.Exported(us.CheckExported)
25+
c.CallgraphAlgorithm(us.Algo)
26+
c.CheckExportedFuncs(us.CheckExported)
2727
c.Program(lintCtx.Program)
2828
c.ProgramSSA(lintCtx.SSAProgram)
2929

vendor/mvdan.cc/unparam/check/check.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ func (c *Checker) ProgramSSA(prog *ssa.Program) {
133133
c.prog = prog
134134
}
135135

136-
// Algo supplies Checker with the call graph construction algorithm.
137-
func (c *Checker) Algo(algo string) {
136+
// CallgraphAlgorithm supplies Checker with the call graph construction algorithm.
137+
func (c *Checker) CallgraphAlgorithm(algo string) {
138138
c.algo = algo
139139
}
140140

141-
// Algo supplies Checker with the exported setting.
142-
func (c *Checker) Exported(exported bool) {
141+
// CheckExportedFuncs sets whether to inspect exported functions
142+
func (c *Checker) CheckExportedFuncs(exported bool) {
143143
c.exported = exported
144144
}
145145

@@ -255,6 +255,16 @@ func (c *Checker) addIssue(fn *ssa.Function, pos token.Pos, format string, args
255255
})
256256
}
257257

258+
// constantValueToString returns string representation for constant value
259+
func constantValueToString(val constant.Value) string {
260+
valStr := "nil" // an untyped nil is a nil constant.Value
261+
if val != nil {
262+
valStr = val.String()
263+
}
264+
265+
return valStr
266+
}
267+
258268
// checkFunc checks a single function for unused parameters.
259269
func (c *Checker) checkFunc(fn *ssa.Function, pkgInfo *loader.PackageInfo) {
260270
c.debug("func %s\n", fn.RelString(fn.Package().Pkg))
@@ -310,10 +320,7 @@ func (c *Checker) checkFunc(fn *ssa.Function, pkgInfo *loader.PackageInfo) {
310320
// just one non-nil return (too many false positives)
311321
continue
312322
}
313-
valStr := "nil" // an untyped nil is a nil constant.Value
314-
if val != nil {
315-
valStr = val.String()
316-
}
323+
valStr := constantValueToString(val)
317324
if calledInReturn(inboundCalls) {
318325
continue
319326
}
@@ -497,10 +504,11 @@ func (c *Checker) alwaysReceivedConst(in []*callgraph.Edge, par *ssa.Parameter,
497504
seenOrig = ""
498505
}
499506
}
500-
if seenOrig != "" && seenOrig != seen.String() {
507+
seenStr := constantValueToString(seen)
508+
if seenOrig != "" && seenOrig != seenStr {
501509
return fmt.Sprintf("%s (%v)", seenOrig, seen)
502510
}
503-
return seen.String()
511+
return seenStr
504512
}
505513

506514
// anyRealUse reports whether a parameter has any relevant use within its

0 commit comments

Comments
 (0)