| 1 | // Copyright 2025 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 scannererr defines an analyzer for uses of bufio.Scanner |
| 6 | // in which the user has forgotten to check Scanner.Err. |
| 7 | package scannererrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/scannererr`/ |
| 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 | 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`/" |
| 19 | "golang.org/x/tools/internal/moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/" |
| 20 | "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/" |
| 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/scannererr`/doc. = `scannererr: report failure to check bufio.Scanner.Err |
| 25 | |
| 26 | This analyzer reports uses of bufio.Scanner in which the result of |
| 27 | NewScanner is assigned to a local variable that is then used in a loop |
| 28 | that calls Scanner.Scan, but lacks a final check of Scanner.Err, |
| 29 | which is how I/O errors are reported. |
| 30 | |
| 31 | For example: |
| 32 | |
| 33 | sc := bufio.NewScanner(os.Stdin) // error: "bufio.Scanner sc is used in Scan loop without final check of sc.Err()" |
| 34 | for sc.Scan() { |
| 35 | line := sc.Text() |
| 36 | use(line) |
| 37 | } |
| 38 | /* ...no use of sc.Err()... */ |
| 39 | |
| 40 | To avoid false positives, the analyzer is silent if the scanner is |
| 41 | passed into or out of the function or assigned somewhere other than a |
| 42 | local variable. |
| 43 | |
| 44 | It is not this analyzer's goal to ensure proper handling of errors in |
| 45 | all cases, but merely the simple mistakes where the user may have been |
| 46 | oblivious to the existence of the Scanner.Err method. |
| 47 | |
| 48 | The analyzer ignores calls to bufio.NewScanner whose argument is an |
| 49 | infallible memory-backed io.Reader such as strings.Reader or bytes.Buffer. |
| 50 | (In such cases, Scan may yet fail if a token or line is too long for the |
| 51 | scanner's internal buffer, but this is rare.) |
| 52 | |
| 53 | If you know that errors are impossible for a given scanner, you can |
| 54 | suppress the diagnostic thus: |
| 55 | |
| 56 | _ = sc.Err() // ignore error; neither reading nor scanning can fail |
| 57 | |
| 58 | ` |
| 59 | |
| 60 | var Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/scannererr`/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#{ |
| 61 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "scannererr", |
| 62 | 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/scannererr`/doc., |
| 63 | 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/scannererr", |
| 64 | 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.}, |
| 65 | 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/scannererr`/run()., |
| 66 | } |
| 67 | |
| 68 | func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/scannererr`/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) { |
| 69 | var ( |
| 70 | 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#) |
| 71 | info = pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo. |
| 72 | ) |
| 73 | |
| 74 | callLoop: |
| 75 | for curCall := range index.Callsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Calls().(index.Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Object().("bufio", "NewScanner")) { |
| 76 | // If NewScanner's reader operand is infallible (such |
| 77 | // as strings.Reader, bytes.Reader, or bytes.Buffer), |
| 78 | // disregard it. (Scanning can still fail if a token |
| 79 | // or line is longer than the internal buffer, but |
| 80 | // that's much less common than an I/O error). |
| 81 | tArg := info.TypeOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#TypeOf().(curCall.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`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#).Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[0]) |
| 82 | if typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsPointerToNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsPointerToNamed().(tArg, "bytes", "Buffer", "Reader") || |
| 83 | typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsPointerToNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsPointerToNamed().(tArg, "strings", "Reader") { |
| 84 | continue |
| 85 | } |
| 86 | |
| 87 | // TODO(adonovan): factor this common pattern. |
| 88 | 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# |
| 89 | switch ek, idx := curCall.ParentEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdge().(); ek { |
| 90 | 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.: |
| 91 | // var sc = bufio.NewScanner(...) |
| 92 | 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., idx) |
| 93 | 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#) |
| 94 | 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.: |
| 95 | // sc := bufio.NewScanner(...) (or '=') |
| 96 | 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., idx) |
| 97 | 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#) |
| 98 | } |
| 99 | 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#) |
| 100 | if !ok { |
| 101 | continue |
| 102 | } |
| 103 | sc, 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#) |
| 104 | if !ok { |
| 105 | continue |
| 106 | } |
| 107 | // Have: sc := bufio.NewScanner(...) |
| 108 | |
| 109 | // Check all uses of the var sc. |
| 110 | scanLoop := 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 sc.Scan() call within a loop |
| 111 | for curUse := range index.Usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Uses().(sc) { |
| 112 | // If the var sc is used in a context other than sc.Method(...), |
| 113 | // assume conservatively that it may escape, and reject this candidate. |
| 114 | switch curUse.ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().() { |
| 115 | case 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., 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.: |
| 116 | // ok |
| 117 | default: |
| 118 | continue callLoop |
| 119 | } |
| 120 | |
| 121 | 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. { |
| 122 | case "Err": |
| 123 | // If the sc.Err method is called anywhere, reject this candidate. |
| 124 | continue callLoop |
| 125 | case "Scan": |
| 126 | // The Scan call must be in a loop that intervenes the declaration of sc. |
| 127 | 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 { |
| 128 | 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().() > sc.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Pos().() { |
| 129 | scanLoop = 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().() |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | if !scanLoop.IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 135 | continue |
| 136 | } |
| 137 | |
| 138 | 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#{ |
| 139 | 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().(), |
| 140 | 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().(), |
| 141 | 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().("bufio.Scanner %q is used in Scan loop at line %d without final check of %s.Err()", |
| 142 | sc.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().(scanLoop).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line., sc.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().()), |
| 143 | }) |
| 144 | } |
| 145 | |
| 146 | return nil, nil |
| 147 | } |