@@ -777,36 +777,28 @@ func (c *Checker) alwaysReceivedConst(callSites []ssa.CallInstruction, par *ssa.
777777 // times, hence a potential false positive.
778778 return ""
779779 }
780- if ast .IsExported (par .Parent ().Name ()) {
780+ fn := par .Parent ()
781+ if ast .IsExported (fn .Name ()) {
781782 // we might not have all call sites for an exported func
782783 return ""
783784 }
784- var seen * ssa.Const
785- origPos := pos
786- if par .Parent ().Signature .Recv () != nil {
787- // go/ast's CallExpr.Args does not include the receiver, but
788- // go/ssa's equivalent does.
789- origPos --
785+ if fn .Signature .Recv () != nil {
786+ pos -- // paramArgs and go/ast's CallExpr.Args exclude the receiver
790787 }
788+ var seen * ssa.Const
791789 seenOrig := ""
792790 for _ , site := range callSites {
793791 call := site .Common ()
794- if pos >= len (call .Args ) {
795- // TODO: investigate? Weird crash in
796- // internal/x/net/http2/hpack/hpack_test.go, where we
797- // roughly do: "at := d.mustAt; at(3)".
798- return ""
799- }
800- cnst := constValue (call .Args [pos ])
792+ cnst := constValue (paramArgs (call , fn )[pos ])
801793 if cnst == nil {
802794 return "" // not a constant
803795 }
804796 origArg := ""
805797 origCall := c .callByPos [call .Pos ()]
806- if origPos >= len (origCall .Args ) {
798+ if pos >= len (origCall .Args ) {
807799 // variadic parameter that wasn't given
808800 } else {
809- origArg = nodeStr (origCall .Args [origPos ])
801+ origArg = nodeStr (origCall .Args [pos ])
810802 }
811803 if seen == nil {
812804 seen = cnst // first constant
@@ -1057,6 +1049,15 @@ func (c *Checker) multipleImpls(pkg *packages.Package, fn *ssa.Function) bool {
10571049 return count [name ] > 1
10581050}
10591051
1052+ // paramArgs returns the arguments of a call to fn, excluding any receiver,
1053+ // which a call via a bound method value like "f := x.Method" does not have.
1054+ func paramArgs (call * ssa.CallCommon , fn * ssa.Function ) []ssa.Value {
1055+ if fn .Signature .Recv () != nil && len (call .Args ) > fn .Signature .Params ().Len () {
1056+ return call .Args [1 :]
1057+ }
1058+ return call .Args
1059+ }
1060+
10601061// receivesExtractedArgs returns the statically called function, if its multiple
10611062// arguments were all received via another function call. That is, if a call to
10621063// function "foo" was of the form "foo(bar())". This often means that the
@@ -1071,12 +1072,7 @@ func receivesExtractedArgs(freeVars map[ssa.Value]*ssa.Function, call ssa.CallIn
10711072 // there aren't multiple parameters
10721073 return nil
10731074 }
1074- args := comm .Args
1075- if callee .Signature .Recv () != nil {
1076- // skip the receiver argument
1077- args = args [1 :]
1078- }
1079- if c := callExtract (call , args ); c != nil {
1075+ if c := callExtract (call , paramArgs (comm , callee )); c != nil {
10801076 return callee
10811077 }
10821078 return nil
0 commit comments