| 1 | // Copyright 2026 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 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 11 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 12 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 13 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 14 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 15 | |
| 16 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 17 | "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/" |
| 18 | "golang.org/x/tools/go/ast/edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/" |
| 19 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 20 | "golang.org/x/tools/internal/analysis/analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/" |
| 21 | typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/ "golang.org/x/tools/internal/analysis/typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/" |
| 22 | "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/" |
| 23 | "golang.org/x/tools/internal/moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/" |
| 24 | "golang.org/x/tools/internal/typesinternal/typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/" |
| 25 | "golang.org/x/tools/internal/versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/" |
| 26 | ) |
| 27 | |
| 28 | var EmbedLitAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/EmbedLitAnalyzer. = &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#{ |
| 29 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "embedlit", |
| 30 | 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., "embedlit"), |
| 31 | 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#{ |
| 32 | 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., |
| 33 | typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/Analyzer., |
| 34 | }, |
| 35 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: runEmbedLitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/runEmbedLit()., |
| 36 | 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#embedlit", |
| 37 | } |
| 38 | |
| 39 | // Go1.27 introduced the ability to directly access embedded struct fields. |
| 40 | // The embedlit modernizer suggests two types of fixes that use this feature: |
| 41 | // 1. Removing redundant field type specifiers in embedded struct fields. |
| 42 | // 2. Moving embedded struct field assignments inside of the struct literal |
| 43 | // initialization. |
| 44 | func runEmbedLitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/runEmbedLit().(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) { |
| 45 | var ( |
| 46 | inspect = pass.ResultOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ResultOf.[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.].(*inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#) |
| 47 | index = pass.ResultOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ResultOf.[typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/Analyzer.].(*typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#) |
| 48 | info = pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo. |
| 49 | ) |
| 50 | for curLit := range inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().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`/.CompositeLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#)(nil)) { |
| 51 | if curLit.ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().() != edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.KeyValueExpr_Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/KeyValueExpr_Value. { // non-nested comp lit |
| 52 | // TODO(mkalil): Figure out how to handle addition/removal of commas in |
| 53 | // the comp lit when we observe code where both patterns apply. (This will |
| 54 | // likely require a significant amount of work). For now, only apply edits |
| 55 | // from one pattern at a time. |
| 56 | if !embedlitUnnestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/embedlitUnnest().(pass, info, curLit) { |
| 57 | err := embedlitCombinescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/embedlitCombine().(pass, index, info, curLit) // calls pass.ReadFile |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | return nil, nil |
| 65 | } |
| 66 | |
| 67 | // Pattern A: removing unneeded embedded field type specifier from the struct |
| 68 | // literal. |
| 69 | // T{U: U{f: v, ...}} => T{f: v, ...} |
| 70 | // It returns true if it reported a diagnostic with edits. |
| 71 | func embedlitUnnestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/embedlitUnnest().(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#, info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, curLit inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) bool { |
| 72 | var ( |
| 73 | edits []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit# |
| 74 | names []string // names of the embedded field types that can be removed |
| 75 | lit = curLit.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`/.CompositeLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#) |
| 76 | compLitType = info.TypeOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#TypeOf().(lit) |
| 77 | ) |
| 78 | |
| 79 | // checkLit determines whether any of the fields in the given struct literal can |
| 80 | // be promoted, and calculates the corresponding edits. |
| 81 | var checkLit func(lit *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CompositeLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#) |
| 82 | checkLit = func(lit *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CompositeLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#) { |
| 83 | for i, elt := range lit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts. { |
| 84 | // Can't promote an unkeyed field; would result in a syntax error. |
| 85 | if kv, ok := elt.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.KeyValueExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#); ok { |
| 86 | if innerLit := isEmbeddedFieldLitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/isEmbeddedFieldLit().(info, compLitType, kv); innerLit != nil { |
| 87 | // Emit edits to delete the unnecessary embedded field type specifier |
| 88 | // and its closing brace. |
| 89 | closingPos := innerLit.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Rbrace. |
| 90 | if len(innerLit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts.) > 0 { |
| 91 | // Delete any inner trailing commas or white space. Extra trailing commas |
| 92 | // would result in invalid code. |
| 93 | closingPos = innerLit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts.[len(innerLit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts.)-1].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() |
| 94 | } |
| 95 | file := astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EnclosingFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EnclosingFile().(curLit) |
| 96 | // Enable modernizer only for Go1.27. |
| 97 | if !analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/.FileUsesGoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/FileUsesGoVersion().(pass, file, versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/.Go1_27scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/Go1_27.) { |
| 98 | return |
| 99 | } |
| 100 | // If any comments overlap with the range to delete, don't suggest a fix. |
| 101 | if !moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/.Emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/Empty().(astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.Commentsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Comments().(file, kv.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Pos().(), innerLit.Lbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Lbrace.+1)) || |
| 102 | !moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/.Emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/Empty().(astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.Commentsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Comments().(file, closingPos, innerLit.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Rbrace.+1)) { |
| 103 | continue |
| 104 | } |
| 105 | // Delete starting from the key to the character right after the |
| 106 | // opening brace of the inner literal: |
| 107 | // T{U: U{f: v, ...}} |
| 108 | // ----- |
| 109 | startPos := kv.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Pos().() |
| 110 | endPos := innerLit.Lbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Lbrace. + 1 |
| 111 | |
| 112 | // Delete the entire line if the key and its opening brace are |
| 113 | // together on their own line. This prevents leaving behind unneeded |
| 114 | // blank lines inside struct literals that `gofmt` will not remove. |
| 115 | // T{ |
| 116 | // U: U{ <- delete entire line |
| 117 | // f: v, |
| 118 | // } |
| 119 | // } |
| 120 | // |
| 121 | tokFile := 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().(kv.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Pos().()) |
| 122 | lineOf := func(pos tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#) int { |
| 123 | return tokFile.PositionForscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#PositionFor().(pos, false).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. |
| 124 | } |
| 125 | curLine := lineOf(kv.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Pos().()) |
| 126 | var prevLine int |
| 127 | if i == 0 { |
| 128 | // First element, so the previous line is the parent lbrace. |
| 129 | prevLine = lineOf(lit.Lbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Lbrace.) |
| 130 | } else { |
| 131 | prevLine = lineOf(lit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts.[i-1].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) |
| 132 | } |
| 133 | |
| 134 | // We can safely delete the entire line if the key value expression is |
| 135 | // on a different line than the previous element, and the closing |
| 136 | // brace of the inner literal is on a different line than its opening |
| 137 | // brace. |
| 138 | if prevLine < curLine && curLine < tokFile.LineCountscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#LineCount().() && // (1-based) |
| 139 | lineOf(innerLit.Lbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Lbrace.) < lineOf(innerLit.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Rbrace.) { |
| 140 | lineStart := tokFile.LineStartscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#LineStart().(curLine) |
| 141 | nextLineStart := tokFile.LineStartscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#LineStart().(curLine + 1) |
| 142 | // Check that there are no comments on the line we are going to delete. |
| 143 | if moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/.Emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/Empty().(astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.Commentsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Comments().(file, lineStart, nextLineStart)) { |
| 144 | startPos = tokFile.LineStartscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#LineStart().(curLine) |
| 145 | endPos = nextLineStart |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | edits = append(edits, []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{ |
| 150 | // T{U: U{f: v, ...}} |
| 151 | // ----- - |
| 152 | { |
| 153 | // Delete the key and the opening brace of the inner struct literal. |
| 154 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: startPos, |
| 155 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: endPos, |
| 156 | }, |
| 157 | { |
| 158 | // Delete the corresponding closing brace, including preceding |
| 159 | // white space or commas. Failing to delete trailing commas may |
| 160 | // result in invalid code. |
| 161 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: closingPos, |
| 162 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: innerLit.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Rbrace. + 1, |
| 163 | }, |
| 164 | }...) |
| 165 | names = append(names, kv.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Key..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#).Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.) |
| 166 | checkLit(innerLit) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | checkLit(lit) |
| 172 | if len(edits) > 0 { |
| 173 | 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#{ |
| 174 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.: curLit.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 175 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#End.: curLit.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 176 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.: "embedded field type can be removed from struct literal", |
| 177 | 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#{ |
| 178 | { |
| 179 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("Remove embedded field type%s %s", condscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/cond().(len(names) == 1, "", "s"), stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(names, ", ")), |
| 180 | TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits.: edits, |
| 181 | }, |
| 182 | }, |
| 183 | }) |
| 184 | return true |
| 185 | } |
| 186 | return false |
| 187 | } |
| 188 | |
| 189 | // Pattern B: moving embedded field assignments inside the struct literal |
| 190 | // initialization. |
| 191 | // t := T{...}; t.x = x => t := T{..., x: x} |
| 192 | // (or var t = ...) |
| 193 | func embedlitCombinescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/embedlitCombine().(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#, index *typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#, info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, curLit inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) error { |
| 194 | compLit := curLit.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`/.CompositeLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#) |
| 195 | if !moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/.Everyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/Every().(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Valuesscip-go gomod github.com/golang/go/src go1.25.0 slices/Values().(compLit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts.), func(e astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) bool { |
| 196 | return isscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/is().[*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.KeyValueExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#](e) |
| 197 | }) { |
| 198 | // Promoting additional embedded fields would result in mixing keyed and |
| 199 | // unkeyed fields, which isn't allowed. |
| 200 | return nil |
| 201 | } |
| 202 | var ( |
| 203 | // Ident for "t" in the assignment. |
| 204 | lhs *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident# |
| 205 | // The cursor representing the statement that initializes the comp lit "t". |
| 206 | // We use its siblings to search for field assignments and verify that there |
| 207 | // are no intervening statements, in case those statements observe "t". |
| 208 | curStmt inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor# |
| 209 | ) |
| 210 | switch curLit.ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().() { |
| 211 | case edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.AssignStmt_Rhsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/AssignStmt_Rhs.: |
| 212 | assign := curLit.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().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`/.AssignStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#) |
| 213 | // TODO(mkalil): Handle lhs forms that aren't idents, i.e. x.y[i] = T{...}. |
| 214 | if id, ok := assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[curLit.ParentEdgeIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeIndex().()].(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#); ok { |
| 215 | lhs = id |
| 216 | curStmt = curLit.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().() |
| 217 | } |
| 218 | case edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.ValueSpec_Valuesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/ValueSpec_Values.: |
| 219 | spec := curLit.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().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`/.ValueSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#) |
| 220 | lhs = spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.[curLit.ParentEdgeIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeIndex().()] |
| 221 | if decl, ok := moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/.Firstscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/First().(curLit.Enclosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Enclosing().((*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.DeclStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/DeclStmt#)(nil))); ok { |
| 222 | curStmt = decl |
| 223 | } |
| 224 | default: |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | if lhs == nil || !curStmt.Validscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Valid().() { |
| 229 | return nil |
| 230 | } |
| 231 | |
| 232 | var ( |
| 233 | tObj = info.ObjectOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#ObjectOf().(lhs) |
| 234 | // Marks the contiguous block of embedded field assign statements that will |
| 235 | // be moved into the struct initialization. |
| 236 | firstStmt, lastStmt inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor# |
| 237 | ) |
| 238 | stmtloop: |
| 239 | for { |
| 240 | var ok bool |
| 241 | curStmt, ok = curStmt.NextSiblingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#NextSibling().() |
| 242 | if !ok { |
| 243 | break // end of (e.g.) block |
| 244 | } |
| 245 | // All embedded field value assignments must immediately follow the struct |
| 246 | // initialization. |
| 247 | assign, ok := curStmt.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`/.AssignStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#) |
| 248 | if !ok || len(assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.) != 1 || !(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`/.ASSIGNscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/ASSIGN. || 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.) { |
| 249 | // TODO(mkalil): handle multi-assignments like t.x, t.y = 1, 2 |
| 250 | break |
| 251 | } |
| 252 | expr := assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[0] |
| 253 | sel, ok := expr.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#) |
| 254 | if !ok { |
| 255 | break |
| 256 | } |
| 257 | // Verify that sel.X refers to the same object as "t" |
| 258 | selXId, ok := sel.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 259 | if !ok { |
| 260 | // TODO(mkalil): handle deeply nested expressions like t.B.x |
| 261 | break |
| 262 | } |
| 263 | obj := info.ObjectOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#ObjectOf().(selXId) |
| 264 | if obj != tObj { |
| 265 | break |
| 266 | } |
| 267 | rhsCur := curStmt.ChildAtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ChildAt().(edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.AssignStmt_Rhsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/AssignStmt_Rhs., 0) |
| 268 | if usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/uses().(index, rhsCur, tObj) { |
| 269 | break |
| 270 | } |
| 271 | for c := range rhsCur.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`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#)(nil)) { |
| 272 | id := c.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`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 273 | // If the rhs uses a value of t (e.g. t.x = t.y), don't suggest a fix because |
| 274 | // we can't evaluate t.y when constructing the new literal. |
| 275 | if info.ObjectOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#ObjectOf().(id) == tObj { |
| 276 | break stmtloop |
| 277 | } |
| 278 | // Note: we don't need to worry about expressions with side effects |
| 279 | // changing the behavior when moved inside the comp lit. The order of |
| 280 | // effects will be preserved because we preserve the order of the key |
| 281 | // value pairs inside the comp lit. |
| 282 | } |
| 283 | if !firstStmt.Validscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Valid().() { |
| 284 | firstStmt = curStmt |
| 285 | } |
| 286 | lastStmt = curStmt |
| 287 | } |
| 288 | |
| 289 | if !firstStmt.Validscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Valid().() { |
| 290 | return nil |
| 291 | } |
| 292 | |
| 293 | file := astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EnclosingFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EnclosingFile().(curLit) |
| 294 | // Enable modernizer only for Go1.27. |
| 295 | if !analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/.FileUsesGoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/FileUsesGoVersion().(pass, file, versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/.Go1_27scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/Go1_27.) { |
| 296 | return nil |
| 297 | } |
| 298 | |
| 299 | // Read file content to determine if the struct lit has a trailing comma |
| 300 | // after its last element. |
| 301 | tokFile := 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().(compLit.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Rbrace.) |
| 302 | filename := tokFile.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Name().() |
| 303 | src, err := pass.ReadFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReadFile.(filename) |
| 304 | if err != nil { |
| 305 | return err |
| 306 | } |
| 307 | |
| 308 | hasTrailingComma := false |
| 309 | if len(compLit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts.) > 0 { |
| 310 | lastElt := compLit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts.[len(compLit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts.)-1] |
| 311 | lastEltOffset := tokFile.Offsetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Offset().(lastElt.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) |
| 312 | rbraceOffset := tokFile.Offsetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Offset().(compLit.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Rbrace.) |
| 313 | hasTrailingComma = bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Containsscip-go gomod github.com/golang/go/src go1.25.0 bytes/Contains().(src[lastEltOffset:rbraceOffset], []byte(",")) |
| 314 | } |
| 315 | var edits []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit# |
| 316 | // Emit edits to move the field assignment into the struct lit while |
| 317 | // removing it from its current place. |
| 318 | // t := T{...}; t.x = v |
| 319 | // ----- --- - |
| 320 | // t := T{..., x: v} |
| 321 | |
| 322 | // Add a trailing comma before the closing brace of compLit if one doesn't |
| 323 | // exist, and delete the closing brace itself. |
| 324 | // t := T{...}; t.x = v |
| 325 | // - |
| 326 | // t := T{..., t.x = v |
| 327 | if len(compLit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts.) > 0 && !hasTrailingComma { |
| 328 | edits = append(edits, analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{ |
| 329 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: compLit.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Rbrace., |
| 330 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: compLit.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Rbrace. + 1, |
| 331 | NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte(","), |
| 332 | }) |
| 333 | } else { |
| 334 | edits = append(edits, analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{ |
| 335 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: compLit.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Rbrace., |
| 336 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: compLit.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Rbrace. + 1, |
| 337 | }) |
| 338 | } |
| 339 | |
| 340 | // For each assignment: |
| 341 | // t.x = v |
| 342 | // -- --- |
| 343 | // x : v |
| 344 | curStmt = firstStmt |
| 345 | var prevStmt inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor# |
| 346 | for { |
| 347 | assign := curStmt.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`/.AssignStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#) |
| 348 | expr := assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[0] |
| 349 | sel := expr.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#) |
| 350 | // Delete "t." |
| 351 | edits = append(edits, analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{ |
| 352 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: assign.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Pos().(), |
| 353 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: sel.Selscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#Sel..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().(), |
| 354 | }) |
| 355 | // Replace "=" with ":" |
| 356 | edits = append(edits, analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{ |
| 357 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: expr.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 358 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: assign.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#TokPos. + 1, |
| 359 | NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte(":"), |
| 360 | }) |
| 361 | |
| 362 | // Add a comma after the previous assignment if this is not the first one. |
| 363 | if prevStmt.Validscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Valid().() { |
| 364 | edits = append(edits, analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{ |
| 365 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: prevStmt.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 366 | NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte(","), |
| 367 | }) |
| 368 | } |
| 369 | |
| 370 | // For the last assignment, add the closing brace of the struct lit. |
| 371 | if curStmt == lastStmt { |
| 372 | edits = append(edits, analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{ |
| 373 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: assign.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#End().(), |
| 374 | NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte("}"), |
| 375 | }) |
| 376 | break |
| 377 | } |
| 378 | prevStmt = curStmt |
| 379 | curStmt, _ = curStmt.NextSiblingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#NextSibling().() // can't fail because we break out of the loop when we hit lastStmt |
| 380 | } |
| 381 | |
| 382 | 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#{ |
| 383 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.: curLit.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 384 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#End.: curLit.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 385 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.: "embedded field assignment can be moved to struct literal", |
| 386 | 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#{ |
| 387 | { |
| 388 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.: "Move embedded field assignment to struct literal", |
| 389 | TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits.: edits, |
| 390 | }, |
| 391 | }, |
| 392 | }) |
| 393 | return nil |
| 394 | } |
| 395 | |
| 396 | // isEmbeddedFieldLit determines whether elt is a KeyValueExpr "T: T{...}" for |
| 397 | // an embedded field for which we can safely remove its type. |
| 398 | // If so, it returns the corresponding CompositeLit. |
| 399 | // If elt contains an unkeyed field or ambiguous type, it returns nil. |
| 400 | func isEmbeddedFieldLitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/isEmbeddedFieldLit().(info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, topLevelType typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#, kv *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.KeyValueExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#) *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CompositeLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit# { |
| 401 | obj := keyedFieldscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/keyedField().(info, kv) |
| 402 | if obj == nil || !obj.Embeddedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#Embedded().() { |
| 403 | return nil |
| 404 | } |
| 405 | lit, ok := kv.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Value..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CompositeLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#) |
| 406 | if !ok || len(lit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts.) == 0 { |
| 407 | // Skip if the struct literal is empty. |
| 408 | return nil |
| 409 | } |
| 410 | // We cannot remove this type if any of its nested composite elements have |
| 411 | // unkeyed fields or are ambiguous, so we check for those conditions before |
| 412 | // returning. |
| 413 | for _, elt := range lit.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts. { |
| 414 | kv, ok := elt.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.KeyValueExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#) |
| 415 | if !ok { |
| 416 | return nil |
| 417 | } |
| 418 | obj := keyedFieldscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/keyedField().(info, kv) |
| 419 | if obj == nil { |
| 420 | return nil |
| 421 | } |
| 422 | k := kv.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Key..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) // can't fail |
| 423 | // Cannot promote an ambiguous type, for example: |
| 424 | // type T struct { A; B } |
| 425 | // type A struct { x int } |
| 426 | // type B struct { x int } |
| 427 | // _ = T{A: A{x: 1}} |
| 428 | // cannot be simplified to T{x: 1} because T has two different embedded fields called "x". |
| 429 | // We also reject composite literals with slice elements, as parentObj will be nil. |
| 430 | parentObj, _, _ := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.LookupFieldOrMethodscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/LookupFieldOrMethod().(topLevelType, true, obj.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Pkg().(), k.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.) |
| 431 | if parentObj != obj { |
| 432 | return nil |
| 433 | } |
| 434 | } |
| 435 | return lit |
| 436 | } |
| 437 | |
| 438 | // keyedField reports whether the key of kv is an embedded field type. If so, it |
| 439 | // returns the type of the embedded field, otherwise it returns nil. |
| 440 | func keyedFieldscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/keyedField().(info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, kv *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.KeyValueExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#) *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var# { |
| 441 | k, ok := kv.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Key..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 442 | if !ok { |
| 443 | return nil |
| 444 | } |
| 445 | obj, ok := info.ObjectOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#ObjectOf().(k).(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#) |
| 446 | if !ok || !obj.IsFieldscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#IsField().() { |
| 447 | return nil |
| 448 | } |
| 449 | return obj |
| 450 | } |