| 1 | // Copyright 2026 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 sqlrowserr defines an analyzer for uses of sql.Rows |
| 6 | // in which the user has forgotten to check Rows.Err. |
| 7 | package sqlrowserrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/sqlrowserr`/ |
| 8 | |
| 9 | import ( |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 11 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 12 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 13 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 14 | |
| 15 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 16 | "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/" |
| 17 | "golang.org/x/tools/go/ast/edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/" |
| 18 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 19 | typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/ "golang.org/x/tools/internal/analysis/typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/" |
| 20 | "golang.org/x/tools/internal/moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/" |
| 21 | "golang.org/x/tools/internal/typesinternal/typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/" |
| 22 | ) |
| 23 | |
| 24 | const docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/sqlrowserr`/doc. = `sqlrowserr: report failure to check sql.Rows.Err |
| 25 | |
| 26 | This analyzer reports uses of sql.Rows in which the result of a query |
| 27 | such as db.Query() is assigned to a local variable that is then used |
| 28 | in a loop that calls Rows.Next, but lacks a final check of Rows.Err. |
| 29 | This causes row iteration errors to be discarded. |
| 30 | |
| 31 | For example: |
| 32 | |
| 33 | rows, err := db.Query("select ...") // error: "sql.Rows rows is used in Next loop without final check of rows.Err()" |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | defer rows.Close() // ignore error |
| 38 | for rows.Next() { |
| 39 | var x int |
| 40 | if err := rows.Scan(&x); err != nil { |
| 41 | return err |
| 42 | } |
| 43 | use(x) |
| 44 | } |
| 45 | /* ...no use of rows.Err()... */ |
| 46 | |
| 47 | Correct usage of sql.Rows demands both a call to Rows.Close to release |
| 48 | resources and a call to Rows.Err to report iteration errors. It is |
| 49 | not critical to report resource cleanup errors, but it is crucial to |
| 50 | report iteration errors as they would otherwise be indistinguishable |
| 51 | from a smaller result. |
| 52 | |
| 53 | To avoid false positives, the analyzer is silent if the Rows is passed |
| 54 | into or out of the function or assigned somewhere other than a local |
| 55 | variable. |
| 56 | |
| 57 | It is not this analyzer's goal to ensure proper handling of errors in |
| 58 | all cases, but merely the simple mistakes where the user may have been |
| 59 | oblivious to the existence of the Rows.Err method. |
| 60 | ` |
| 61 | |
| 62 | var Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/sqlrowserr`/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#{ |
| 63 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "sqlrowserr", |
| 64 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/sqlrowserr`/doc., |
| 65 | 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/sqlrowserr", |
| 66 | 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., typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/Analyzer.}, |
| 67 | 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/sqlrowserr`/run()., |
| 68 | } |
| 69 | |
| 70 | // TODO(adonovan): factor common structures with the scannererr analyzer. |
| 71 | |
| 72 | func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/sqlrowserr`/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) { |
| 73 | var ( |
| 74 | index = pass.ResultOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ResultOf.[typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/Analyzer.].(*typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#) |
| 75 | info = pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo. |
| 76 | ) |
| 77 | |
| 78 | checkCall := func(curCall inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) { |
| 79 | var lhs astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr# |
| 80 | switch curCall.ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().() { |
| 81 | case edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.ValueSpec_Valuesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/ValueSpec_Values.: |
| 82 | // var sc, err = db.Query(...) |
| 83 | curName := curCall.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().ChildAtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ChildAt().(edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.ValueSpec_Namesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/ValueSpec_Names., 0) |
| 84 | lhs = curName.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 85 | case edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.AssignStmt_Rhsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/AssignStmt_Rhs.: |
| 86 | // sc, err := db.Query(...) (or '=') |
| 87 | curLhs := curCall.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().ChildAtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ChildAt().(edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.AssignStmt_Lhsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/AssignStmt_Lhs., 0) |
| 88 | lhs = curLhs.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) |
| 89 | } |
| 90 | id, ok := lhs.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 91 | if !ok { |
| 92 | return |
| 93 | } |
| 94 | rows, ok := info.ObjectOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#ObjectOf().(id).(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#) |
| 95 | if !ok { |
| 96 | return |
| 97 | } |
| 98 | // Have: rows, err := db.Query(...) |
| 99 | |
| 100 | // Check all uses of the var rows. |
| 101 | nextLoop := 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. // position of rows.Next() call within a loop |
| 102 | for curUse := range index.Usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Uses().(rows) { |
| 103 | // If the var rows is used in a context other than rows.Method(...), |
| 104 | // assume conservatively that it may escape, and reject this candidate. |
| 105 | if curUse.ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().() != edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.SelectorExpr_Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/SelectorExpr_X. || |
| 106 | curUse.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().() != edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.CallExpr_Funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/CallExpr_Fun. { |
| 107 | return |
| 108 | } |
| 109 | |
| 110 | switch curUse.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#).Selscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#Sel..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. { |
| 111 | case "Err": |
| 112 | // If the rows.Err method is called anywhere, reject this candidate. |
| 113 | return |
| 114 | case "Next": |
| 115 | // The Next call must be in a loop that intervenes the declaration of rows. |
| 116 | if curLoop, ok := moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/.Firstscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/First().(curUse.Enclosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Enclosing().((*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.RangeStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#)(nil), (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ForStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#)(nil))); ok { |
| 117 | if curLoop.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() > rows.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Pos().() { |
| 118 | nextLoop = curUse.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | if !nextLoop.IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 124 | return |
| 125 | } |
| 126 | pass.Reportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Report.(analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Diagnosticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#{ |
| 127 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.: curCall.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 128 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#End.: curCall.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 129 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("sql.Rows %q is used in Next loop at line %d without final check of %s.Err()", |
| 130 | rows.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().(), pass.Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Fset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(nextLoop).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line., rows.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().()), |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | // Check each query method in the sql package that returns (*Rows, error). |
| 135 | // (This could be generalized for arbitrary such functions...) |
| 136 | for _, m := range [...][2]string{ |
| 137 | {"Conn", "QueryContext"}, |
| 138 | {"DB", "QueryContext"}, |
| 139 | {"DB", "Query"}, |
| 140 | {"Stmt", "QueryContext"}, |
| 141 | {"Stmt", "Query"}, |
| 142 | {"Tx", "QueryContext"}, |
| 143 | {"Tx", "Query"}, |
| 144 | } { |
| 145 | for cur := range index.Callsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Calls().(index.Selectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Selection().("database/sql", m[0], m[1])) { |
| 146 | checkCall(cur) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return nil, nil |
| 151 | } |