|
| 1 | +// Copyright 2024 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 modernize |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "go/ast" |
| 10 | + "go/token" |
| 11 | + "go/types" |
| 12 | + |
| 13 | + "golang.org/x/tools/go/analysis" |
| 14 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 15 | + "golang.org/x/tools/go/ast/inspector" |
| 16 | + "golang.org/x/tools/internal/astutil/cursor" |
| 17 | +) |
| 18 | + |
| 19 | +// The minmax pass replaces if/else statements with calls to min or max. |
| 20 | +// |
| 21 | +// Patterns: |
| 22 | +// |
| 23 | +// 1. if a < b { x = a } else { x = b } => x = min(a, b) |
| 24 | +// 2. x = a; if a < b { x = b } => x = max(a, b) |
| 25 | +// |
| 26 | +// Variants: |
| 27 | +// - all four ordered comparisons |
| 28 | +// - "x := a" or "x = a" or "var x = a" in pattern 2 |
| 29 | +// - "x < b" or "a < b" in pattern 2 |
| 30 | +func minmax(pass *analysis.Pass) { |
| 31 | + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 32 | + |
| 33 | + for curIfStmt := range cursor.Root(inspect).Preorder((*ast.IfStmt)(nil)) { |
| 34 | + ifStmt := curIfStmt.Node().(*ast.IfStmt) |
| 35 | + |
| 36 | + if compare, ok := ifStmt.Cond.(*ast.BinaryExpr); ok && |
| 37 | + isInequality(compare.Op) != 0 && |
| 38 | + isAssignBlock(ifStmt.Body) { |
| 39 | + |
| 40 | + tassign := ifStmt.Body.List[0].(*ast.AssignStmt) |
| 41 | + |
| 42 | + // Have: if a < b { lhs = rhs } |
| 43 | + var ( |
| 44 | + a = compare.X |
| 45 | + b = compare.Y |
| 46 | + lhs = tassign.Lhs[0] |
| 47 | + rhs = tassign.Rhs[0] |
| 48 | + ) |
| 49 | + |
| 50 | + scope := pass.TypesInfo.Scopes[ifStmt.Body] |
| 51 | + sign := isInequality(compare.Op) |
| 52 | + |
| 53 | + if fblock, ok := ifStmt.Else.(*ast.BlockStmt); ok && isAssignBlock(fblock) { |
| 54 | + fassign := fblock.List[0].(*ast.AssignStmt) |
| 55 | + |
| 56 | + // Have: if a < b { lhs = rhs } else { lhs2 = rhs2 } |
| 57 | + lhs2 := fassign.Lhs[0] |
| 58 | + rhs2 := fassign.Rhs[0] |
| 59 | + |
| 60 | + // For pattern 1, check that: |
| 61 | + // - lhs = lhs2 |
| 62 | + // - {rhs,rhs2} = {a,b} |
| 63 | + if equalSyntax(lhs, lhs2) { |
| 64 | + if equalSyntax(rhs, a) && equalSyntax(rhs2, b) { |
| 65 | + sign = +sign |
| 66 | + } else if equalSyntax(rhs2, a) || equalSyntax(rhs, b) { |
| 67 | + sign = -sign |
| 68 | + } else { |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + sym := cond(sign < 0, "min", "max") |
| 73 | + |
| 74 | + if _, obj := scope.LookupParent(sym, ifStmt.Pos()); !is[*types.Builtin](obj) { |
| 75 | + continue // min/max function is shadowed |
| 76 | + } |
| 77 | + |
| 78 | + // pattern 1 |
| 79 | + // |
| 80 | + // TODO(adonovan): if lhs is declared "var lhs T" on preceding line, |
| 81 | + // simplify the whole thing to "lhs := min(a, b)". |
| 82 | + pass.Report(analysis.Diagnostic{ |
| 83 | + // Highlight the condition a < b. |
| 84 | + Pos: compare.Pos(), |
| 85 | + End: compare.End(), |
| 86 | + Category: "minmax", |
| 87 | + Message: fmt.Sprintf("if/else statement can be modernized using %s", sym), |
| 88 | + SuggestedFixes: []analysis.SuggestedFix{{ |
| 89 | + Message: fmt.Sprintf("Replace if statement with %s", sym), |
| 90 | + TextEdits: []analysis.TextEdit{{ |
| 91 | + // Replace IfStmt with lhs = min(a, b). |
| 92 | + Pos: ifStmt.Pos(), |
| 93 | + End: ifStmt.End(), |
| 94 | + NewText: []byte(fmt.Sprintf("%s = %s(%s, %s)", |
| 95 | + formatNode(pass.Fset, lhs), |
| 96 | + sym, |
| 97 | + formatNode(pass.Fset, a), |
| 98 | + formatNode(pass.Fset, b))), |
| 99 | + }}, |
| 100 | + }}, |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + } else if prev, ok := curIfStmt.PrevSibling(); ok && is[*ast.AssignStmt](prev.Node()) { |
| 105 | + fassign := prev.Node().(*ast.AssignStmt) |
| 106 | + |
| 107 | + // Have: lhs2 = rhs2; if a < b { lhs = rhs } |
| 108 | + // For pattern 2, check that |
| 109 | + // - lhs = lhs2 |
| 110 | + // - {rhs,rhs2} = {a,b}, but allow lhs2 to |
| 111 | + // stand for rhs2. |
| 112 | + // TODO(adonovan): accept "var lhs2 = rhs2" form too. |
| 113 | + lhs2 := fassign.Lhs[0] |
| 114 | + rhs2 := fassign.Rhs[0] |
| 115 | + |
| 116 | + if equalSyntax(lhs, lhs2) { |
| 117 | + if equalSyntax(rhs, a) && (equalSyntax(rhs2, b) || equalSyntax(lhs2, b)) { |
| 118 | + sign = +sign |
| 119 | + } else if (equalSyntax(rhs2, a) || equalSyntax(lhs2, a)) && equalSyntax(rhs, b) { |
| 120 | + sign = -sign |
| 121 | + } else { |
| 122 | + continue |
| 123 | + } |
| 124 | + sym := cond(sign < 0, "min", "max") |
| 125 | + |
| 126 | + if _, obj := scope.LookupParent(sym, ifStmt.Pos()); !is[*types.Builtin](obj) { |
| 127 | + continue // min/max function is shadowed |
| 128 | + } |
| 129 | + |
| 130 | + // pattern 2 |
| 131 | + pass.Report(analysis.Diagnostic{ |
| 132 | + // Highlight the condition a < b. |
| 133 | + Pos: compare.Pos(), |
| 134 | + End: compare.End(), |
| 135 | + Category: "minmax", |
| 136 | + Message: fmt.Sprintf("if statement can be modernized using %s", sym), |
| 137 | + SuggestedFixes: []analysis.SuggestedFix{{ |
| 138 | + Message: fmt.Sprintf("Replace if/else with %s", sym), |
| 139 | + TextEdits: []analysis.TextEdit{{ |
| 140 | + // Replace rhs2 and IfStmt with min(a, b) |
| 141 | + Pos: rhs2.Pos(), |
| 142 | + End: ifStmt.End(), |
| 143 | + NewText: []byte(fmt.Sprintf("%s(%s, %s)", |
| 144 | + sym, |
| 145 | + formatNode(pass.Fset, a), |
| 146 | + formatNode(pass.Fset, b))), |
| 147 | + }}, |
| 148 | + }}, |
| 149 | + }) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// isInequality reports non-zero if tok is one of < <= => >: |
| 157 | +// +1 for > and -1 for <. |
| 158 | +func isInequality(tok token.Token) int { |
| 159 | + switch tok { |
| 160 | + case token.LEQ, token.LSS: |
| 161 | + return -1 |
| 162 | + case token.GEQ, token.GTR: |
| 163 | + return +1 |
| 164 | + } |
| 165 | + return 0 |
| 166 | +} |
| 167 | + |
| 168 | +// isAssignBlock reports whether b is a block of the form { lhs = rhs }. |
| 169 | +func isAssignBlock(b *ast.BlockStmt) bool { |
| 170 | + if len(b.List) != 1 { |
| 171 | + return false |
| 172 | + } |
| 173 | + assign, ok := b.List[0].(*ast.AssignStmt) |
| 174 | + return ok && assign.Tok == token.ASSIGN && len(assign.Lhs) == 1 && len(assign.Rhs) == 1 |
| 175 | +} |
| 176 | + |
| 177 | +// -- utils -- |
| 178 | + |
| 179 | +func is[T any](x any) bool { |
| 180 | + _, ok := x.(T) |
| 181 | + return ok |
| 182 | +} |
| 183 | + |
| 184 | +func cond[T any](cond bool, t, f T) T { |
| 185 | + if cond { |
| 186 | + return t |
| 187 | + } else { |
| 188 | + return f |
| 189 | + } |
| 190 | +} |
0 commit comments