|
| 1 | +package rule |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "go/ast" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/mgechev/revive/lint" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + ruleUTAMessagePanic = "type assertion will panic if not matched" |
| 13 | + ruleUTAMessageIgnored = "type assertion result ignored" |
| 14 | +) |
| 15 | + |
| 16 | +// UncheckedTypeAssertionRule lints missing or ignored `ok`-value in danymic type casts. |
| 17 | +type UncheckedTypeAssertionRule struct { |
| 18 | + sync.Mutex |
| 19 | + acceptIgnoredAssertionResult bool |
| 20 | +} |
| 21 | + |
| 22 | +func (u *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) { |
| 23 | + u.Lock() |
| 24 | + defer u.Unlock() |
| 25 | + |
| 26 | + if len(arguments) == 0 { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + args, ok := arguments[0].(map[string]any) |
| 31 | + if !ok { |
| 32 | + panic("Unable to get arguments. Expected object of key-value-pairs.") |
| 33 | + } |
| 34 | + |
| 35 | + for k, v := range args { |
| 36 | + switch k { |
| 37 | + case "acceptIgnoredAssertionResult": |
| 38 | + u.acceptIgnoredAssertionResult, ok = v.(bool) |
| 39 | + if !ok { |
| 40 | + panic(fmt.Sprintf("Unable to parse argument '%s'. Expected boolean.", k)) |
| 41 | + } |
| 42 | + default: |
| 43 | + panic(fmt.Sprintf("Unknown argument: %s", k)) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Apply applies the rule to given file. |
| 49 | +func (u *UncheckedTypeAssertionRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { |
| 50 | + u.configure(args) |
| 51 | + |
| 52 | + var failures []lint.Failure |
| 53 | + |
| 54 | + walker := &lintUnchekedTypeAssertion{ |
| 55 | + pkg: file.Pkg, |
| 56 | + onFailure: func(failure lint.Failure) { |
| 57 | + failures = append(failures, failure) |
| 58 | + }, |
| 59 | + acceptIgnoredTypeAssertionResult: u.acceptIgnoredAssertionResult, |
| 60 | + } |
| 61 | + |
| 62 | + file.Pkg.TypeCheck() |
| 63 | + ast.Walk(walker, file.AST) |
| 64 | + |
| 65 | + return failures |
| 66 | +} |
| 67 | + |
| 68 | +// Name returns the rule name. |
| 69 | +func (*UncheckedTypeAssertionRule) Name() string { |
| 70 | + return "unchecked-type-assertion" |
| 71 | +} |
| 72 | + |
| 73 | +type lintUnchekedTypeAssertion struct { |
| 74 | + pkg *lint.Package |
| 75 | + onFailure func(lint.Failure) |
| 76 | + acceptIgnoredTypeAssertionResult bool |
| 77 | +} |
| 78 | + |
| 79 | +func isIgnored(e ast.Expr) bool { |
| 80 | + ident, ok := e.(*ast.Ident) |
| 81 | + if !ok { |
| 82 | + return false |
| 83 | + } |
| 84 | + |
| 85 | + return ident.Name == "_" |
| 86 | +} |
| 87 | + |
| 88 | +func isTypeSwitch(e *ast.TypeAssertExpr) bool { |
| 89 | + return e.Type == nil |
| 90 | +} |
| 91 | + |
| 92 | +func (w *lintUnchekedTypeAssertion) requireNoTypeAssert(expr ast.Expr) { |
| 93 | + e, ok := expr.(*ast.TypeAssertExpr) |
| 94 | + if ok && !isTypeSwitch(e) { |
| 95 | + w.addFailure(e, ruleUTAMessagePanic) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func (w *lintUnchekedTypeAssertion) handleIfStmt(n *ast.IfStmt) { |
| 100 | + ifCondition, ok := n.Cond.(*ast.BinaryExpr) |
| 101 | + if !ok { |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + w.requireNoTypeAssert(ifCondition.X) |
| 106 | + w.requireNoTypeAssert(ifCondition.Y) |
| 107 | +} |
| 108 | + |
| 109 | +func (w *lintUnchekedTypeAssertion) requireBinaryExpressionWithoutTypeAssertion(expr ast.Expr) { |
| 110 | + binaryExpr, ok := expr.(*ast.BinaryExpr) |
| 111 | + if ok { |
| 112 | + w.requireNoTypeAssert(binaryExpr.X) |
| 113 | + w.requireNoTypeAssert(binaryExpr.Y) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func (w *lintUnchekedTypeAssertion) handleCaseClause(n *ast.CaseClause) { |
| 118 | + for _, expr := range n.List { |
| 119 | + w.requireNoTypeAssert(expr) |
| 120 | + w.requireBinaryExpressionWithoutTypeAssertion(expr) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func (w *lintUnchekedTypeAssertion) handleSwitch(n *ast.SwitchStmt) { |
| 125 | + w.requireNoTypeAssert(n.Tag) |
| 126 | + w.requireBinaryExpressionWithoutTypeAssertion(n.Tag) |
| 127 | +} |
| 128 | + |
| 129 | +func (w *lintUnchekedTypeAssertion) handleAssignment(n *ast.AssignStmt) { |
| 130 | + if len(n.Rhs) == 0 { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + e, ok := n.Rhs[0].(*ast.TypeAssertExpr) |
| 135 | + if !ok || e == nil { |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + if isTypeSwitch(e) { |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + if len(n.Lhs) == 1 { |
| 144 | + w.addFailure(e, ruleUTAMessagePanic) |
| 145 | + } |
| 146 | + |
| 147 | + if !w.acceptIgnoredTypeAssertionResult && len(n.Lhs) == 2 && isIgnored(n.Lhs[1]) { |
| 148 | + w.addFailure(e, ruleUTAMessageIgnored) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// handles "return foo(.*bar)" - one of them is enough to fail as golang does not forward the type cast tuples in return statements |
| 153 | +func (w *lintUnchekedTypeAssertion) handleReturn(n *ast.ReturnStmt) { |
| 154 | + for _, r := range n.Results { |
| 155 | + w.requireNoTypeAssert(r) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func (w *lintUnchekedTypeAssertion) handleRange(n *ast.RangeStmt) { |
| 160 | + w.requireNoTypeAssert(n.X) |
| 161 | +} |
| 162 | + |
| 163 | +func (w *lintUnchekedTypeAssertion) handleChannelSend(n *ast.SendStmt) { |
| 164 | + w.requireNoTypeAssert(n.Value) |
| 165 | +} |
| 166 | + |
| 167 | +func (w *lintUnchekedTypeAssertion) Visit(node ast.Node) ast.Visitor { |
| 168 | + switch n := node.(type) { |
| 169 | + case *ast.RangeStmt: |
| 170 | + w.handleRange(n) |
| 171 | + case *ast.SwitchStmt: |
| 172 | + w.handleSwitch(n) |
| 173 | + case *ast.ReturnStmt: |
| 174 | + w.handleReturn(n) |
| 175 | + case *ast.AssignStmt: |
| 176 | + w.handleAssignment(n) |
| 177 | + case *ast.IfStmt: |
| 178 | + w.handleIfStmt(n) |
| 179 | + case *ast.CaseClause: |
| 180 | + w.handleCaseClause(n) |
| 181 | + case *ast.SendStmt: |
| 182 | + w.handleChannelSend(n) |
| 183 | + } |
| 184 | + |
| 185 | + return w |
| 186 | +} |
| 187 | + |
| 188 | +func (w *lintUnchekedTypeAssertion) addFailure(n *ast.TypeAssertExpr, why string) { |
| 189 | + s := fmt.Sprintf("type cast result is unchecked in %v - %s", gofmt(n), why) |
| 190 | + w.onFailure(lint.Failure{ |
| 191 | + Category: "bad practice", |
| 192 | + Confidence: 1, |
| 193 | + Node: n, |
| 194 | + Failure: s, |
| 195 | + }) |
| 196 | +} |
0 commit comments