| 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 modernizescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/ |
| 6 | |
| 7 | import ( |
| 8 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 9 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 10 | |
| 11 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 12 | "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/" |
| 13 | "golang.org/x/tools/internal/analysis/analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/" |
| 14 | "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/" |
| 15 | "golang.org/x/tools/internal/refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/" |
| 16 | "golang.org/x/tools/internal/versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/" |
| 17 | ) |
| 18 | |
| 19 | var ForVarAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/ForVarAnalyzer. = &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#{ |
| 20 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "forvar", |
| 21 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/.MustExtractDocscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/MustExtractDoc().(docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/doc., "forvar"), |
| 22 | 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.}, |
| 23 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: forvarscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/forvar()., |
| 24 | 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/modernize#forvar", |
| 25 | } |
| 26 | |
| 27 | // forvar offers to fix unnecessary copying of a for variable |
| 28 | // |
| 29 | // for _, x := range foo { |
| 30 | // x := x // offer to remove this superfluous assignment |
| 31 | // } |
| 32 | // |
| 33 | // Prerequisites: |
| 34 | // First statement in a range loop has to be <ident> := <ident> |
| 35 | // where the two idents are the same, |
| 36 | // and the ident is defined (:=) as a variable in the for statement. |
| 37 | // (Note that this 'fix' does not work for three clause loops |
| 38 | // because the Go spec says "The variable used by each subsequent iteration |
| 39 | // is declared implicitly before executing the post statement and initialized to the |
| 40 | // value of the previous iteration's variable at that moment.") |
| 41 | // |
| 42 | // Variant: same thing in an IfStmt.Init, when the IfStmt is the sole |
| 43 | // loop body statement: |
| 44 | // |
| 45 | // for _, x := range foo { |
| 46 | // if x := x; cond { ... } |
| 47 | // } |
| 48 | // |
| 49 | // (The restriction is necessary to avoid potential problems arising |
| 50 | // from merging two distinct variables.) |
| 51 | // |
| 52 | // This analyzer is synergistic with stditerators, |
| 53 | // which may create redundant "x := x" statements. |
| 54 | func forvarscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/forvar().(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) { |
| 55 | for curFile := range filesUsingGoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/filesUsingGoVersion().(pass, versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/.Go1_22scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/Go1_22.) { |
| 56 | for curLoop := range curFile.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().((*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)) { |
| 57 | loop := curLoop.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`/.RangeStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#) |
| 58 | if loop.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Tok. != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.DEFINEscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/DEFINE. { |
| 59 | continue |
| 60 | } |
| 61 | isLoopVarRedecl := func(stmt astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt#) bool { |
| 62 | if assign, ok := stmt.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.AssignStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#); ok && |
| 63 | assign.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Tok. == tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.DEFINEscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/DEFINE. && |
| 64 | len(assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.) == len(assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.) { |
| 65 | |
| 66 | for i, lhs := range assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs. { |
| 67 | if !(astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EqualSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EqualSyntax().(lhs, assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.[i]) && |
| 68 | (astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EqualSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EqualSyntax().(lhs, loop.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Key.) || |
| 69 | astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EqualSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EqualSyntax().(lhs, loop.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Value.))) { |
| 70 | return false |
| 71 | } |
| 72 | } |
| 73 | return true |
| 74 | } |
| 75 | return false |
| 76 | } |
| 77 | // Have: for k, v := range x { stmts } |
| 78 | // |
| 79 | // Delete the prefix of stmts that are |
| 80 | // of the form k := k; v := v; k, v := k, v; v, k := v, k. |
| 81 | for _, stmt := range loop.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List. { |
| 82 | if isLoopVarRedecl(stmt) { |
| 83 | // { x := x; ... } |
| 84 | // ------ |
| 85 | } else if ifstmt, ok := stmt.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IfStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#); ok && |
| 86 | ifstmt.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Init. != nil && |
| 87 | len(loop.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List.) == 1 && // must be sole statement in loop body |
| 88 | isLoopVarRedecl(ifstmt.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Init.) { |
| 89 | // if x := x; cond { |
| 90 | // ------ |
| 91 | stmt = ifstmt.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Init. |
| 92 | } else { |
| 93 | break // stop at first other statement |
| 94 | } |
| 95 | |
| 96 | curStmt, _ := curLoop.FindNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindNode().(stmt) |
| 97 | edits := refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/.DeleteStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteStmt().(pass.Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Fset..Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(stmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()), curStmt) |
| 98 | if len(edits) > 0 { |
| 99 | 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#{ |
| 100 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.: stmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 101 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#End.: stmt.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 102 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.: "copying variable is unneeded", |
| 103 | SuggestedFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#SuggestedFixes.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.SuggestedFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#{{ |
| 104 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.: "Remove unneeded redeclaration", |
| 105 | TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits.: edits, |
| 106 | }}, |
| 107 | }) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return nil, nil |
| 113 | } |