|
| 1 | +// / ctx: https://ctx.ist |
| 2 | +// ,'`./ do you remember? |
| 3 | +// `.,'\ |
| 4 | +// \ Copyright 2026-present Context contributors. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +package audit |
| 8 | + |
| 9 | +import ( |
| 10 | + "go/ast" |
| 11 | + "go/token" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +// TestNoVariableShadowing detects two forms of variable |
| 16 | +// shadowing in non-test Go files: |
| 17 | +// |
| 18 | +// (a) Bare "err" reuse: multiple := assignments to |
| 19 | +// the unadorned name "err" in the same function body. |
| 20 | +// The convention requires descriptive names (readErr, |
| 21 | +// writeErr, parseErr) so each error site is |
| 22 | +// independently identifiable. |
| 23 | +// |
| 24 | +// (b) General inner-scope shadowing (any variable): |
| 25 | +// already caught by golangci-lint's shadow checker, |
| 26 | +// which is enabled in .golangci.yml. |
| 27 | +// |
| 28 | +// Test files are exempt. |
| 29 | +func TestNoVariableShadowing(t *testing.T) { |
| 30 | + pkgs := loadPackages(t) |
| 31 | + var violations []string |
| 32 | + |
| 33 | + for _, pkg := range pkgs { |
| 34 | + for _, file := range pkg.Syntax { |
| 35 | + fpath := pkg.Fset.Position(file.Pos()).Filename |
| 36 | + if isTestFile(fpath) { |
| 37 | + continue |
| 38 | + } |
| 39 | + |
| 40 | + for _, decl := range file.Decls { |
| 41 | + fn, ok := decl.(*ast.FuncDecl) |
| 42 | + if !ok || fn.Body == nil { |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + sites := collectBareErrDefines( |
| 47 | + pkg.Fset, fn.Body, |
| 48 | + ) |
| 49 | + |
| 50 | + if len(sites) > 1 { |
| 51 | + for _, pos := range sites { |
| 52 | + violations = append( |
| 53 | + violations, |
| 54 | + pos+ |
| 55 | + ": bare err := in "+ |
| 56 | + fn.Name.Name+ |
| 57 | + " (use descriptive "+ |
| 58 | + "name)", |
| 59 | + ) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if len(violations) > 0 { |
| 67 | + t.Errorf( |
| 68 | + "%d bare err := reuses found:", |
| 69 | + len(violations), |
| 70 | + ) |
| 71 | + } |
| 72 | + limit := 30 |
| 73 | + if len(violations) < limit { |
| 74 | + limit = len(violations) |
| 75 | + } |
| 76 | + for _, v := range violations[:limit] { |
| 77 | + t.Error(v) |
| 78 | + } |
| 79 | + if len(violations) > 30 { |
| 80 | + t.Errorf( |
| 81 | + "... and %d more", |
| 82 | + len(violations)-30, |
| 83 | + ) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// collectBareErrDefines walks a function body and |
| 88 | +// returns positions of := assignments where "err" is |
| 89 | +// on the LHS. |
| 90 | +func collectBareErrDefines( |
| 91 | + fset *token.FileSet, body *ast.BlockStmt, |
| 92 | +) []string { |
| 93 | + var sites []string |
| 94 | + |
| 95 | + ast.Inspect(body, func(n ast.Node) bool { |
| 96 | + assign, ok := n.(*ast.AssignStmt) |
| 97 | + if !ok { |
| 98 | + return true |
| 99 | + } |
| 100 | + |
| 101 | + if assign.Tok != token.DEFINE { |
| 102 | + return true |
| 103 | + } |
| 104 | + |
| 105 | + for _, lhs := range assign.Lhs { |
| 106 | + ident, ok := lhs.(*ast.Ident) |
| 107 | + if !ok { |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + if ident.Name == "err" { |
| 112 | + sites = append(sites, |
| 113 | + posString(fset, assign.Pos()), |
| 114 | + ) |
| 115 | + break |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return true |
| 120 | + }) |
| 121 | + |
| 122 | + return sites |
| 123 | +} |
0 commit comments