@@ -33,7 +33,7 @@ var Analyzer = &analysis.Analyzer{
33
33
Doc : analysisinternal .MustExtractDoc (doc , "gofix" ),
34
34
URL : "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix" ,
35
35
Run : run ,
36
- FactTypes : []analysis.Fact {new (goFixInlineFuncFact ), new (goFixForwardConstFact )},
36
+ FactTypes : []analysis.Fact {new (goFixInlineFuncFact ), new (goFixInlineConstFact )},
37
37
Requires : []* analysis.Analyzer {inspect .Analyzer },
38
38
}
39
39
@@ -64,19 +64,14 @@ func run(pass *analysis.Pass) (any, error) {
64
64
// comment (the syntax proposed by #32816),
65
65
// and export a fact for each one.
66
66
inlinableFuncs := make (map [* types.Func ]* inline.Callee ) // memoization of fact import (nil => no fact)
67
- forwardableConsts := make (map [* types.Const ]* goFixForwardConstFact )
67
+ inlinableConsts := make (map [* types.Const ]* goFixInlineConstFact )
68
68
69
69
inspect := pass .ResultOf [inspect .Analyzer ].(* inspector.Inspector )
70
70
nodeFilter := []ast.Node {(* ast .FuncDecl )(nil ), (* ast .GenDecl )(nil )}
71
71
inspect .Preorder (nodeFilter , func (n ast.Node ) {
72
72
switch decl := n .(type ) {
73
73
case * ast.FuncDecl :
74
- hasInline , hasForward := fixDirectives (decl .Doc )
75
- if hasForward {
76
- pass .Reportf (decl .Doc .Pos (), "use //go:fix inline for functions" )
77
- return
78
- }
79
- if ! hasInline {
74
+ if ! hasFixInline (decl .Doc ) {
80
75
return
81
76
}
82
77
content , err := readFile (decl )
@@ -97,20 +92,12 @@ func run(pass *analysis.Pass) (any, error) {
97
92
if decl .Tok != token .CONST {
98
93
return
99
94
}
100
- declInline , declForward := fixDirectives (decl .Doc )
101
- if declInline {
102
- pass .Reportf (decl .Doc .Pos (), "use //go:fix forward for constants" )
103
- return
104
- }
105
- // Accept forward directives on the entire decl as well as individual specs.
95
+ declInline := hasFixInline (decl .Doc )
96
+ // Accept inline directives on the entire decl as well as individual specs.
106
97
for _ , spec := range decl .Specs {
107
98
spec := spec .(* ast.ValueSpec ) // guaranteed by Tok == CONST
108
- specInline , specForward := fixDirectives (spec .Doc )
109
- if specInline {
110
- pass .Reportf (spec .Doc .Pos (), "use //go:fix forward for constants" )
111
- return
112
- }
113
- if declForward || specForward {
99
+ specInline := hasFixInline (spec .Doc )
100
+ if declInline || specInline {
114
101
for i , name := range spec .Names {
115
102
if i >= len (spec .Values ) {
116
103
// Possible following an iota.
@@ -120,29 +107,29 @@ func run(pass *analysis.Pass) (any, error) {
120
107
var rhsID * ast.Ident
121
108
switch e := val .(type ) {
122
109
case * ast.Ident :
123
- // Constants defined with the predeclared iota cannot be forwarded .
110
+ // Constants defined with the predeclared iota cannot be inlined .
124
111
if pass .TypesInfo .Uses [e ] == builtinIota {
125
- pass .Reportf (val .Pos (), "invalid //go:fix forward directive: const value is iota" )
112
+ pass .Reportf (val .Pos (), "invalid //go:fix inline directive: const value is iota" )
126
113
continue
127
114
}
128
115
rhsID = e
129
116
case * ast.SelectorExpr :
130
117
rhsID = e .Sel
131
118
default :
132
- pass .Reportf (val .Pos (), "invalid //go:fix forward directive: const value is not the name of another constant" )
119
+ pass .Reportf (val .Pos (), "invalid //go:fix inline directive: const value is not the name of another constant" )
133
120
continue
134
121
}
135
122
lhs := pass .TypesInfo .Defs [name ].(* types.Const )
136
123
rhs := pass .TypesInfo .Uses [rhsID ].(* types.Const ) // must be so in a well-typed program
137
- con := & goFixForwardConstFact {
124
+ con := & goFixInlineConstFact {
138
125
RHSName : rhs .Name (),
139
126
RHSPkgName : rhs .Pkg ().Name (),
140
127
RHSPkgPath : rhs .Pkg ().Path (),
141
128
}
142
129
if rhs .Pkg () == pass .Pkg {
143
130
con .rhsObj = rhs
144
131
}
145
- forwardableConsts [lhs ] = con
132
+ inlinableConsts [lhs ] = con
146
133
// Create a fact only if the LHS is exported and defined at top level.
147
134
// We create a fact even if the RHS is non-exported,
148
135
// so we can warn uses in other packages.
@@ -155,8 +142,8 @@ func run(pass *analysis.Pass) (any, error) {
155
142
}
156
143
})
157
144
158
- // Pass 2. Inline each static call to an inlinable function,
159
- // and forward each reference to a forwardable constant.
145
+ // Pass 2. Inline each static call to an inlinable function
146
+ // and each reference to an inlinable constant.
160
147
//
161
148
// TODO(adonovan): handle multiple diffs that each add the same import.
162
149
for cur := range cursor .Root (inspect ).Preorder ((* ast .CallExpr )(nil ), (* ast .Ident )(nil )) {
@@ -231,14 +218,14 @@ func run(pass *analysis.Pass) (any, error) {
231
218
}
232
219
233
220
case * ast.Ident :
234
- // If the identifier is a use of a forwardable constant, suggest forwarding it.
221
+ // If the identifier is a use of an inlinable constant, suggest inlining it.
235
222
if con , ok := pass .TypesInfo .Uses [n ].(* types.Const ); ok {
236
- fcon , ok := forwardableConsts [con ]
223
+ fcon , ok := inlinableConsts [con ]
237
224
if ! ok {
238
- var fact goFixForwardConstFact
225
+ var fact goFixInlineConstFact
239
226
if pass .ImportObjectFact (con , & fact ) {
240
227
fcon = & fact
241
- forwardableConsts [con ] = fcon
228
+ inlinableConsts [con ] = fcon
242
229
}
243
230
}
244
231
if fcon == nil {
@@ -253,7 +240,7 @@ func run(pass *analysis.Pass) (any, error) {
253
240
curFile := currentFile (cur )
254
241
255
242
// We have an identifier A here (n), possibly qualified by a package identifier (sel.X),
256
- // and a forwardable "const A = B" elsewhere (fcon).
243
+ // and an inlinable "const A = B" elsewhere (fcon).
257
244
// Consider replacing A with B.
258
245
259
246
// Check that the expression we are inlining (B) means the same thing
@@ -268,10 +255,10 @@ func run(pass *analysis.Pass) (any, error) {
268
255
if obj == nil {
269
256
// Should be impossible: if code at n can refer to the LHS,
270
257
// it can refer to the RHS.
271
- panic (fmt .Sprintf ("no object for forwardable const %s RHS %s" , n .Name , fcon .RHSName ))
258
+ panic (fmt .Sprintf ("no object for inlinable const %s RHS %s" , n .Name , fcon .RHSName ))
272
259
}
273
260
if obj != fcon .rhsObj {
274
- // "B" means something different here than at the forwardable const's scope.
261
+ // "B" means something different here than at the inlinable const's scope.
275
262
continue
276
263
}
277
264
}
@@ -304,9 +291,9 @@ func run(pass *analysis.Pass) (any, error) {
304
291
pass .Report (analysis.Diagnostic {
305
292
Pos : pos ,
306
293
End : end ,
307
- Message : fmt .Sprintf ("Constant %s should be forwarded " , name ),
294
+ Message : fmt .Sprintf ("Constant %s should be inlined " , name ),
308
295
SuggestedFixes : []analysis.SuggestedFix {{
309
- Message : fmt .Sprintf ("Forward constant %s" , name ),
296
+ Message : fmt .Sprintf ("Inline constant %s" , name ),
310
297
TextEdits : edits ,
311
298
}},
312
299
})
@@ -317,20 +304,15 @@ func run(pass *analysis.Pass) (any, error) {
317
304
return nil , nil
318
305
}
319
306
320
- // fixDirectives reports the presence of "//go:fix inline" and "//go:fix forward"
321
- // directives in the comments.
322
- func fixDirectives (cg * ast.CommentGroup ) ( inline , forward bool ) {
307
+ // hasFixInline reports the presence of a "//go:fix inline" directive
308
+ // in the comments.
309
+ func hasFixInline (cg * ast.CommentGroup ) bool {
323
310
for _ , d := range directives (cg ) {
324
- if d .Tool == "go" && d .Name == "fix" {
325
- switch d .Args {
326
- case "inline" :
327
- inline = true
328
- case "forward" :
329
- forward = true
330
- }
311
+ if d .Tool == "go" && d .Name == "fix" && d .Args == "inline" {
312
+ return true
331
313
}
332
314
}
333
- return
315
+ return false
334
316
}
335
317
336
318
// A goFixInlineFuncFact is exported for each function marked "//go:fix inline".
@@ -340,21 +322,21 @@ type goFixInlineFuncFact struct{ Callee *inline.Callee }
340
322
func (f * goFixInlineFuncFact ) String () string { return "goFixInline " + f .Callee .String () }
341
323
func (* goFixInlineFuncFact ) AFact () {}
342
324
343
- // A goFixForwardConstFact is exported for each constant marked "//go:fix forward ".
344
- // It holds information about a forwardable constant. Gob-serializable.
345
- type goFixForwardConstFact struct {
325
+ // A goFixInlineConstFact is exported for each constant marked "//go:fix inline ".
326
+ // It holds information about an inlinable constant. Gob-serializable.
327
+ type goFixInlineConstFact struct {
346
328
// Information about "const LHSName = RHSName".
347
329
RHSName string
348
330
RHSPkgPath string
349
331
RHSPkgName string
350
332
rhsObj types.Object // for current package
351
333
}
352
334
353
- func (c * goFixForwardConstFact ) String () string {
354
- return fmt .Sprintf ("goFixForward const %q.%s" , c .RHSPkgPath , c .RHSName )
335
+ func (c * goFixInlineConstFact ) String () string {
336
+ return fmt .Sprintf ("goFixInline const %q.%s" , c .RHSPkgPath , c .RHSName )
355
337
}
356
338
357
- func (* goFixForwardConstFact ) AFact () {}
339
+ func (* goFixInlineConstFact ) AFact () {}
358
340
359
341
func discard (string , ... any ) {}
360
342
0 commit comments