| 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package unusedresult defines an analyzer that checks for unused |
| 6 | // results of calls to certain functions. |
| 7 | package unusedresultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/ |
| 8 | |
| 9 | // It is tempting to make this analysis inductive: for each function |
| 10 | // that tail-calls one of the functions that we check, check those |
| 11 | // functions too. However, just because you must use the result of |
| 12 | // fmt.Sprintf doesn't mean you need to use the result of every |
| 13 | // function that returns a formatted string: it may have other results |
| 14 | // and effects. |
| 15 | |
| 16 | import ( |
| 17 | _ "embedscip-go gomod github.com/golang/go/src go1.25.0 embed/" |
| 18 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 19 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 20 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 21 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 22 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 23 | |
| 24 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 25 | "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/" |
| 26 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 27 | "golang.org/x/tools/go/types/typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/" |
| 28 | "golang.org/x/tools/internal/analysis/analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/" |
| 29 | "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/" |
| 30 | ) |
| 31 | |
| 32 | //go:embed doc.go |
| 33 | var docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/doc. string |
| 34 | |
| 35 | var Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/Analyzer. = &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 36 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "unusedresult", |
| 37 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/.MustExtractDocscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/MustExtractDoc().(docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/doc., "unusedresult"), |
| 38 | URLscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#URL.: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/unusedresult", |
| 39 | Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires.: []*analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer.}, |
| 40 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/run()., |
| 41 | } |
| 42 | |
| 43 | // flags |
| 44 | var funcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/funcs., stringMethodsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringMethods. stringSetFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringSetFlag# |
| 45 | |
| 46 | func initscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/init().() { |
| 47 | // TODO(adonovan): provide a comment or declaration syntax to |
| 48 | // allow users to add their functions to this set using facts. |
| 49 | // For example: |
| 50 | // |
| 51 | // func ignoringTheErrorWouldBeVeryBad() error { |
| 52 | // type mustUseResult struct{} // enables vet unusedresult check |
| 53 | // ... |
| 54 | // } |
| 55 | // |
| 56 | // ignoringTheErrorWouldBeVeryBad() // oops |
| 57 | // |
| 58 | |
| 59 | // List standard library functions here. |
| 60 | // The context.With{Cancel,Deadline,Timeout} entries are |
| 61 | // effectively redundant wrt the lostcancel analyzer. |
| 62 | funcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/funcs. = stringSetFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringSetFlag#{ |
| 63 | "context.WithCancel": true, |
| 64 | "context.WithDeadline": true, |
| 65 | "context.WithTimeout": true, |
| 66 | "context.WithValue": true, |
| 67 | "errors.New": true, |
| 68 | "fmt.Append": true, |
| 69 | "fmt.Appendf": true, |
| 70 | "fmt.Appendln": true, |
| 71 | "fmt.Errorf": true, |
| 72 | "fmt.Sprint": true, |
| 73 | "fmt.Sprintf": true, |
| 74 | "fmt.Sprintln": true, |
| 75 | "maps.All": true, |
| 76 | "maps.Clone": true, |
| 77 | "maps.Collect": true, |
| 78 | "maps.Equal": true, |
| 79 | "maps.EqualFunc": true, |
| 80 | "maps.Keys": true, |
| 81 | "maps.Values": true, |
| 82 | "slices.All": true, |
| 83 | "slices.AppendSeq": true, |
| 84 | "slices.Backward": true, |
| 85 | "slices.BinarySearch": true, |
| 86 | "slices.BinarySearchFunc": true, |
| 87 | "slices.Chunk": true, |
| 88 | "slices.Clip": true, |
| 89 | "slices.Clone": true, |
| 90 | "slices.Collect": true, |
| 91 | "slices.Compact": true, |
| 92 | "slices.CompactFunc": true, |
| 93 | "slices.Compare": true, |
| 94 | "slices.CompareFunc": true, |
| 95 | "slices.Concat": true, |
| 96 | "slices.Contains": true, |
| 97 | "slices.ContainsFunc": true, |
| 98 | "slices.Delete": true, |
| 99 | "slices.DeleteFunc": true, |
| 100 | "slices.Equal": true, |
| 101 | "slices.EqualFunc": true, |
| 102 | "slices.Grow": true, |
| 103 | "slices.Index": true, |
| 104 | "slices.IndexFunc": true, |
| 105 | "slices.Insert": true, |
| 106 | "slices.IsSorted": true, |
| 107 | "slices.IsSortedFunc": true, |
| 108 | "slices.Max": true, |
| 109 | "slices.MaxFunc": true, |
| 110 | "slices.Min": true, |
| 111 | "slices.MinFunc": true, |
| 112 | "slices.Repeat": true, |
| 113 | "slices.Replace": true, |
| 114 | "slices.Sorted": true, |
| 115 | "slices.SortedFunc": true, |
| 116 | "slices.SortedStableFunc": true, |
| 117 | "slices.Values": true, |
| 118 | "sort.Reverse": true, |
| 119 | } |
| 120 | Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/Analyzer..Flagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Flags..Varscip-go gomod github.com/golang/go/src go1.25.0 flag/FlagSet#Var().(&funcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/funcs., "funcs", |
| 121 | "comma-separated list of functions whose results must be used") |
| 122 | |
| 123 | stringMethodsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringMethods..Setscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringSetFlag#Set().("Error,String") |
| 124 | Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/Analyzer..Flagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Flags..Varscip-go gomod github.com/golang/go/src go1.25.0 flag/FlagSet#Var().(&stringMethodsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringMethods., "stringmethods", |
| 125 | "comma-separated list of names of methods of type func() string whose results must be used") |
| 126 | } |
| 127 | |
| 128 | func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/run().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 129 | inspect := pass.ResultOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ResultOf.[inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer.].(*inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#) |
| 130 | |
| 131 | // Split functions into (pkg, name) pairs to save allocation later. |
| 132 | pkgFuncs := make(map[[2]string]bool, len(funcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/funcs.)) |
| 133 | for s := range funcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/funcs. { |
| 134 | if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexBytescip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndexByte().(s, '.'); i > 0 { |
| 135 | pkgFuncs[[2]string{s[:i], s[i+1:]}] = true |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | nodeFilter := []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#{ |
| 140 | (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ExprStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#)(nil), |
| 141 | } |
| 142 | inspect.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Preorder().(nodeFilter, func(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) { |
| 143 | call, ok := astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Unparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Unparen().(n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ExprStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#).Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#X.).(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) |
| 144 | if !ok { |
| 145 | return // not a call statement |
| 146 | } |
| 147 | |
| 148 | // Call to function or method? |
| 149 | fn, ok := typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Callee().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo., call).(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#) |
| 150 | if !ok { |
| 151 | return // e.g. var or builtin |
| 152 | } |
| 153 | if sig := fn.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#Signature().(); sig.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Recv().() != nil { |
| 154 | // method (e.g. foo.String()) |
| 155 | if typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Identicalscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Identical().(sig, sigNoArgsStringResultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/sigNoArgsStringResult.) { |
| 156 | if stringMethodsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringMethods.[fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().()] { |
| 157 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.RangeOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/RangeOf().(call.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Pos().(), call.Lparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Lparen.), |
| 158 | "result of (%s).%s call not used", |
| 159 | sig.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Recv().().Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().(), fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().()) |
| 160 | } |
| 161 | } |
| 162 | } else { |
| 163 | // package-level function (e.g. fmt.Errorf) |
| 164 | if pkgFuncs[[2]string{fn.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#Pkg().().Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().(), fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().()}] { |
| 165 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.RangeOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/RangeOf().(call.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Pos().(), call.Lparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Lparen.), |
| 166 | "result of %s.%s call not used", |
| 167 | fn.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#Pkg().().Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().(), fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().()) |
| 168 | } |
| 169 | } |
| 170 | }) |
| 171 | return nil, nil |
| 172 | } |
| 173 | |
| 174 | // func() string |
| 175 | var sigNoArgsStringResultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/sigNoArgsStringResult. = typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewSignatureTypescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewSignatureType().(nil, nil, nil, nil, typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewTuplescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewTuple().(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewParamscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewParam().(tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NoPosscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NoPos., nil, "", typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/String.])), false) |
| 176 | |
| 177 | type stringSetFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringSetFlag# map[string]bool |
| 178 | |
| 179 | func (ss *stringSetFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringSetFlag#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringSetFlag#String().() string { |
| 180 | var items []string |
| 181 | for item := range *ss { |
| 182 | items = append(items, item) |
| 183 | } |
| 184 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(items) |
| 185 | return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(items, ",") |
| 186 | } |
| 187 | |
| 188 | func (ss *stringSetFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringSetFlag#) Setscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/unusedresult`/stringSetFlag#Set().(s string) error { |
| 189 | m := make(map[string]bool) // clobber previous value |
| 190 | if s != "" { |
| 191 | for name := range stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.SplitSeqscip-go gomod github.com/golang/go/src go1.25.0 strings/SplitSeq().(s, ",") { |
| 192 | if name == "" { |
| 193 | continue // TODO: report error? proceed? |
| 194 | } |
| 195 | m[name] = true |
| 196 | } |
| 197 | } |
| 198 | *ss = m |
| 199 | return nil |
| 200 | } |