|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/ast" |
| 5 | + "go/constant" |
| 6 | + "go/token" |
| 7 | + "go/types" |
| 8 | + |
| 9 | + "github.com/dgageot/rubocop-go/cop" |
| 10 | + "github.com/dgageot/rubocop-go/prog" |
| 11 | +) |
| 12 | + |
| 13 | +// CutSuffix catches adjacent suffix checks and removals without moving effects. |
| 14 | +var CutSuffix = prog.FromFile(cutSuffixFile) |
| 15 | + |
| 16 | +var cutSuffixFile = &cop.Func{ |
| 17 | + Meta: cop.Meta{ |
| 18 | + Name: "Lint/CutSuffix", |
| 19 | + Description: "use strings.CutSuffix instead of checking and then removing a suffix", |
| 20 | + Severity: cop.Warning, |
| 21 | + }, |
| 22 | + Types: true, |
| 23 | + Scope: outsideFrozenConfig, |
| 24 | + Run: func(p *cop.Pass) { |
| 25 | + if p.Info == nil || ast.IsGenerated(p.File) { |
| 26 | + return |
| 27 | + } |
| 28 | + ast.Inspect(p.File, func(n ast.Node) bool { |
| 29 | + switch node := n.(type) { |
| 30 | + case *ast.IfStmt: |
| 31 | + if node.Init == nil && len(node.Body.List) > 0 { |
| 32 | + checkCutSuffix(p, node.Cond, node.Body.List[0]) |
| 33 | + } |
| 34 | + case *ast.BlockStmt: |
| 35 | + checkCutSuffixGuards(p, node.List) |
| 36 | + case *ast.CaseClause: |
| 37 | + checkCutSuffixGuards(p, node.Body) |
| 38 | + case *ast.CommClause: |
| 39 | + checkCutSuffixGuards(p, node.Body) |
| 40 | + } |
| 41 | + return true |
| 42 | + }) |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +func checkCutSuffixGuards(p *cop.Pass, stmts []ast.Stmt) { |
| 47 | + for i, stmt := range stmts { |
| 48 | + guard, ok := stmt.(*ast.IfStmt) |
| 49 | + if !ok || guard.Init != nil || guard.Else != nil || len(guard.Body.List) != 1 || i+1 == len(stmts) { |
| 50 | + continue |
| 51 | + } |
| 52 | + negated, ok := ast.Unparen(guard.Cond).(*ast.UnaryExpr) |
| 53 | + if !ok || negated.Op != token.NOT { |
| 54 | + continue |
| 55 | + } |
| 56 | + switch exit := guard.Body.List[0].(type) { |
| 57 | + case *ast.ReturnStmt: |
| 58 | + case *ast.BranchStmt: |
| 59 | + if exit.Tok != token.CONTINUE || exit.Label != nil { |
| 60 | + continue |
| 61 | + } |
| 62 | + default: |
| 63 | + continue |
| 64 | + } |
| 65 | + checkCutSuffix(p, negated.X, stmts[i+1]) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func checkCutSuffix(p *cop.Pass, cond ast.Expr, stmt ast.Stmt) { |
| 70 | + call := splitTrimStringsCall(p.Info, cond, "HasSuffix") |
| 71 | + if call == nil || !cutSuffixStable(p.Info, call.Args[0]) || !cutSuffixStable(p.Info, call.Args[1]) { |
| 72 | + return |
| 73 | + } |
| 74 | + pattern := cutSuffixPattern{info: p.Info, source: call.Args[0], suffix: call.Args[1]} |
| 75 | + if pattern.removal(cutSuffixValue(p.Info, stmt)) { |
| 76 | + p.Report(call, "use strings.CutSuffix instead of HasSuffix followed by suffix removal; preserve original values, assignment scope, and evaluation order") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// splitTrimStringsCall matches a two-argument strings.<name> call, such as |
| 81 | +// HasSuffix, TrimSuffix, or Join, with no variadic expansion. |
| 82 | +func splitTrimStringsCall(info *types.Info, expr ast.Expr, name string) *ast.CallExpr { |
| 83 | + call, ok := ast.Unparen(expr).(*ast.CallExpr) |
| 84 | + if !ok || len(call.Args) != 2 || call.Ellipsis.IsValid() { |
| 85 | + return nil |
| 86 | + } |
| 87 | + fn, ok := calleeObject(info, call).(*types.Func) |
| 88 | + if !ok || fn.Pkg() == nil || fn.Pkg().Path() != "strings" || fn.Name() != name { |
| 89 | + return nil |
| 90 | + } |
| 91 | + return call |
| 92 | +} |
| 93 | + |
| 94 | +// Restrict consumption to the first evaluation, with no effectful assignment target. |
| 95 | +func cutSuffixValue(info *types.Info, stmt ast.Stmt) ast.Expr { |
| 96 | + switch stmt := stmt.(type) { |
| 97 | + case *ast.AssignStmt: |
| 98 | + if (stmt.Tok == token.DEFINE || stmt.Tok == token.ASSIGN) && len(stmt.Lhs) == 1 && len(stmt.Rhs) == 1 { |
| 99 | + if _, ok := stmt.Lhs[0].(*ast.Ident); ok { |
| 100 | + return stmt.Rhs[0] |
| 101 | + } |
| 102 | + } |
| 103 | + case *ast.DeclStmt: |
| 104 | + decl, ok := stmt.Decl.(*ast.GenDecl) |
| 105 | + if ok && decl.Tok == token.VAR && len(decl.Specs) == 1 { |
| 106 | + value, ok := decl.Specs[0].(*ast.ValueSpec) |
| 107 | + if ok && len(value.Names) == 1 && len(value.Values) == 1 { |
| 108 | + return value.Values[0] |
| 109 | + } |
| 110 | + } |
| 111 | + case *ast.ReturnStmt: |
| 112 | + if len(stmt.Results) == 0 { |
| 113 | + return nil |
| 114 | + } |
| 115 | + for _, result := range stmt.Results[1:] { |
| 116 | + if value := info.Types[result]; value.Value == nil && !value.IsNil() { |
| 117 | + return nil |
| 118 | + } |
| 119 | + } |
| 120 | + return stmt.Results[0] |
| 121 | + } |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func cutSuffixStable(info *types.Info, expr ast.Expr) bool { |
| 126 | + if info.Types[expr].Value != nil { |
| 127 | + return true |
| 128 | + } |
| 129 | + id, ok := ast.Unparen(expr).(*ast.Ident) |
| 130 | + if !ok { |
| 131 | + return false |
| 132 | + } |
| 133 | + variable, ok := info.Uses[id].(*types.Var) |
| 134 | + return ok && variable.Parent() != nil && variable.Pkg() != nil && variable.Parent() != variable.Pkg().Scope() |
| 135 | +} |
| 136 | + |
| 137 | +type cutSuffixPattern struct { |
| 138 | + info *types.Info |
| 139 | + source, suffix ast.Expr |
| 140 | +} |
| 141 | + |
| 142 | +func (p cutSuffixPattern) same(a, b ast.Expr) bool { |
| 143 | + if a == nil || b == nil { |
| 144 | + return false |
| 145 | + } |
| 146 | + av, bv := p.info.Types[a].Value, p.info.Types[b].Value |
| 147 | + if av != nil && bv != nil && av.Kind() == bv.Kind() { |
| 148 | + return constant.Compare(av, token.EQL, bv) |
| 149 | + } |
| 150 | + aid, aok := ast.Unparen(a).(*ast.Ident) |
| 151 | + bid, bok := ast.Unparen(b).(*ast.Ident) |
| 152 | + return aok && bok && p.info.Uses[aid] != nil && p.info.Uses[aid] == p.info.Uses[bid] |
| 153 | +} |
| 154 | + |
| 155 | +func (p cutSuffixPattern) length(expr, value ast.Expr) bool { |
| 156 | + call, ok := ast.Unparen(expr).(*ast.CallExpr) |
| 157 | + return ok && len(call.Args) == 1 && calleeObject(p.info, call) == types.Universe.Lookup("len") && p.same(call.Args[0], value) |
| 158 | +} |
| 159 | + |
| 160 | +func (p cutSuffixPattern) integer(expr ast.Expr, want int64) bool { |
| 161 | + value := p.info.Types[expr].Value |
| 162 | + if value == nil || value.Kind() != constant.Int { |
| 163 | + return false |
| 164 | + } |
| 165 | + got, ok := constant.Int64Val(value) |
| 166 | + return ok && got == want |
| 167 | +} |
| 168 | + |
| 169 | +func (p cutSuffixPattern) removal(expr ast.Expr) bool { |
| 170 | + if trim := splitTrimStringsCall(p.info, expr, "TrimSuffix"); trim != nil { |
| 171 | + return p.same(trim.Args[0], p.source) && p.same(trim.Args[1], p.suffix) |
| 172 | + } |
| 173 | + if slice, ok := ast.Unparen(expr).(*ast.SliceExpr); ok { |
| 174 | + if slice.Slice3 || !types.Identical(p.info.TypeOf(slice), types.Typ[types.String]) || !p.same(slice.X, p.source) || slice.Low != nil && !p.integer(slice.Low, 0) { |
| 175 | + return false |
| 176 | + } |
| 177 | + high, ok := ast.Unparen(slice.High).(*ast.BinaryExpr) |
| 178 | + if !ok || high.Op != token.SUB || !p.length(high.X, p.source) { |
| 179 | + return false |
| 180 | + } |
| 181 | + if p.length(high.Y, p.suffix) { |
| 182 | + return true |
| 183 | + } |
| 184 | + suffix := p.info.Types[p.suffix].Value |
| 185 | + return suffix != nil && suffix.Kind() == constant.String && p.integer(high.Y, int64(len(constant.StringVal(suffix)))) |
| 186 | + } |
| 187 | + // Keep wrappers (e.g. TrimRight) in place; only their first argument is cut. |
| 188 | + call, ok := ast.Unparen(expr).(*ast.CallExpr) |
| 189 | + if !ok || len(call.Args) == 0 || call.Ellipsis.IsValid() { |
| 190 | + return false |
| 191 | + } |
| 192 | + fn, ok := calleeObject(p.info, call).(*types.Func) |
| 193 | + if !ok || fn.Pkg() == nil || fn.Pkg().Path() != "strings" || fn.Type().(*types.Signature).Recv() != nil { |
| 194 | + return false |
| 195 | + } |
| 196 | + for _, arg := range call.Args[1:] { |
| 197 | + if p.info.Types[arg].Value == nil { |
| 198 | + return false |
| 199 | + } |
| 200 | + } |
| 201 | + return p.removal(call.Args[0]) |
| 202 | +} |
0 commit comments