Debian Codegraph @

go/analysis/passes/inline/doc.go

raw symbols SCIP
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 /*
6 Package inline defines an analyzer that inlines calls to functions
7 and uses of constants marked with a "//go:fix inline" directive.
8
9 # Analyzer inline
10
11 inline: apply fixes based on 'go:fix inline' comment directives
12
13 The inline analyzer inlines functions, constants, and type aliases
14 that are marked for inlining.
15
16 Use this command to apply (just) inline fixes en masse:
17
18 $ go fix -inline ./...
19
20 ## Functions
21
22 Given a function that is marked for inlining, like this one:
23
24 //go:fix inline
25 func Square(x int) int { return Pow(x, 2) }
26
27 this analyzer will recommend that calls to the function elsewhere, in the same
28 or other packages, should be inlined.
29
30 Inlining can be used to move off of a deprecated function:
31
32 // Deprecated: prefer Pow(x, 2).
33 //go:fix inline
34 func Square(x int) int { return Pow(x, 2) }
35
36 It can also be used to move off of an obsolete package,
37 as when the import path has changed or a higher major version is available:
38
39 package pkg
40
41 import pkg2 "pkg/v2"
42
43 //go:fix inline
44 func F() { pkg2.F(nil) }
45
46 Replacing a call pkg.F() by pkg2.F(nil) can have no effect on the program,
47 so this mechanism provides a low-risk way to update large numbers of calls.
48 We recommend, where possible, expressing the old API in terms of the new one
49 to enable automatic migration.
50
51 The inliner takes care to avoid behavior changes, even subtle ones,
52 such as changes to the order in which argument expressions are
53 evaluated. When it cannot safely eliminate all parameter variables,
54 it may introduce a "binding declaration" of the form
55
56 var params = args
57
58 to evaluate argument expressions in the correct order and bind them to
59 parameter variables. Since the resulting code transformation may be
60 stylistically suboptimal, such inlinings may be disabled by specifying
61 the -inline.allow_binding_decl=false flag to the analyzer driver.
62
63 (In cases where it is not safe to "reduce" a call—that is, to replace
64 a call f(x) by the body of function f, suitably substituted—the
65 inliner machinery is capable of replacing f by a function literal,
66 func(){...}(). However, the inline analyzer discards all such
67 "literalizations" unconditionally, again on grounds of style.)
68
69 ## Constants
70
71 Given a constant that is marked for inlining, like this one:
72
73 //go:fix inline
74 const Ptr = Pointer
75
76 this analyzer will recommend that uses of Ptr should be replaced with Pointer.
77
78 As with functions, inlining can be used to replace deprecated constants and
79 constants in obsolete packages.
80
81 A constant definition can be marked for inlining only if it refers to another
82 named constant.
83
84 The "//go:fix inline" comment must appear before a single const declaration on its own,
85 as above; before a const declaration that is part of a group, as in this case:
86
87 const (
88 C = 1
89 //go:fix inline
90 Ptr = Pointer
91 )
92
93 or before a group, applying to every constant in the group:
94
95 //go:fix inline
96 const (
97 Ptr = Pointer
98 Val = Value
99 )
100
101 ## Type aliases
102
103 Similar to named constants, a type alias can also be marked for inlining:
104
105 //go:fix inline
106 type A = newpkg.A
107
108 The analyzer will replace all references to the annotated type
109 (A) by the type on the right-hand side of the declaration (newpkg.A).
110
111 ## Tests
112
113 A use of a function, named constant, or type alias X from its
114 dedicated test (TestX), is not inlined, since the purpose of the test
115 is to exercise X itself, even if it is deprecated and other uses of it
116 should be inlined.
117 This applies to benchmarks and examples too, and follows the usual
118 conventions of test function naming.
119
120 Similarly, if the symbol X is declared in a file named foo.go, any use
121 of it within a file named foo_test.go will also not be inlined.
122
123 # Analyzer gofixdirective
124
125 gofixdirective: validate uses of //go:fix comment directives
126
127 The gofixdirective analyzer checks "//go:fix inline" directives for correctness.
128 See the documentation for the gofix analyzer for more about "/go:fix inline".
129 */
130 package inline