diff --git a/go/analysis/passes/modernize/doc.go b/go/analysis/passes/modernize/doc.go
index f7c03ff..a059608 100644
--- a/go/analysis/passes/modernize/doc.go
+++ b/go/analysis/passes/modernize/doc.go
@@ -185,11 +185,15 @@
is replaced by:
+ //go:fix inline
use(new(123))
-(Wrapper functions such as varOf are common when working with Go
+The directive comment causes the 'inline' analyzer to suggest
+that calls to such functions are inlined.
+
+Wrapper functions such as varOf are common when working with Go
serialization packages such as for JSON or protobuf, where pointers
-are often used to express optionality.)
+are often used to express optionality.
# Analyzer omitzero
diff --git a/go/analysis/passes/modernize/newexpr.go b/go/analysis/passes/modernize/newexpr.go
index b889324..2d82e6f 100644
--- a/go/analysis/passes/modernize/newexpr.go
+++ b/go/analysis/passes/modernize/newexpr.go
@@ -87,25 +87,18 @@
}
}
- // Disabled until we resolve https://go.dev/issue/75726
- // (Go version skew between caller and callee in inliner.)
- // TODO(adonovan): fix and reenable.
+ // Add a //go:fix inline annotation, if not already present.
//
- // Also, restore these lines to our section of doc.go:
- // //go:fix inline
- // ...
- // (The directive comment causes the inline analyzer to suggest
- // that calls to such functions are inlined.)
- if false {
- // Add a //go:fix inline annotation, if not already present.
- // TODO(adonovan): use ast.ParseDirective when go1.26 is assured.
- if !strings.Contains(decl.Doc.Text(), "go:fix inline") {
- edits = append(edits, analysis.TextEdit{
- Pos: decl.Pos(),
- End: decl.Pos(),
- NewText: []byte("//go:fix inline\n"),
- })
- }
+ // The inliner will not inline a newer callee body into an
+ // older Go file; see https://go.dev/issue/75726.
+ //
+ // TODO(adonovan): use ast.ParseDirective when go1.26 is assured.
+ if !strings.Contains(decl.Doc.Text(), "go:fix inline") {
+ edits = append(edits, analysis.TextEdit{
+ Pos: decl.Pos(),
+ End: decl.Pos(),
+ NewText: []byte("//go:fix inline\n"),
+ })
}
if len(edits) > 0 {
diff --git a/go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go.golden b/go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go.golden
index 648618e..8f3fe76 100644
--- a/go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go.golden
+++ b/go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go.golden
@@ -3,12 +3,17 @@
package newexpr
// intVar returns a new var whose value is i.
+//
+//go:fix inline
func intVar(i int) *int { return new(i) } // want `intVar can be an inlinable wrapper around new\(expr\)` intVar:"newlike"
+//go:fix inline
func int64Var(i int64) *int64 { return new(i) } // want `int64Var can be an inlinable wrapper around new\(expr\)` int64Var:"newlike"
+//go:fix inline
func stringVar(s string) *string { return new(s) } // want `stringVar can be an inlinable wrapper around new\(expr\)` stringVar:"newlike"
+//go:fix inline
func varOf[T any](x T) *T { return new(x) } // want `varOf can be an inlinable wrapper around new\(expr\)` varOf:"newlike"
var (
diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md
index 629bb65..2237f1d 100644
--- a/gopls/doc/analyzers.md
+++ b/gopls/doc/analyzers.md
@@ -3477,9 +3477,12 @@
is replaced by:
+ //go:fix inline
use(new(123))
-(Wrapper functions such as varOf are common when working with Go serialization packages such as for JSON or protobuf, where pointers are often used to express optionality.)
+The directive comment causes the 'inline' analyzer to suggest that calls to such functions are inlined.
+
+Wrapper functions such as varOf are common when working with Go serialization packages such as for JSON or protobuf, where pointers are often used to express optionality.
Default: on.
diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json
index bcefa16..920e4b6 100644
--- a/gopls/internal/doc/api.json
+++ b/gopls/internal/doc/api.json
@@ -1546,7 +1546,7 @@
},
{
"Name": "\"newexpr\"",
- "Doc": "simplify code by using go1.26's new(expr)\n\nThis analyzer finds declarations of functions of this form:\n\n\tfunc varOf(x int) *int { return \u0026x }\n\nand suggests a fix to turn them into inlinable wrappers around\ngo1.26's built-in new(expr) function:\n\n\tfunc varOf(x int) *int { return new(x) }\n\nIn addition, this analyzer suggests a fix for each call\nto one of the functions before it is transformed, so that\n\n\tuse(varOf(123))\n\nis replaced by:\n\n\tuse(new(123))\n\n(Wrapper functions such as varOf are common when working with Go\nserialization packages such as for JSON or protobuf, where pointers\nare often used to express optionality.)",
+ "Doc": "simplify code by using go1.26's new(expr)\n\nThis analyzer finds declarations of functions of this form:\n\n\tfunc varOf(x int) *int { return \u0026x }\n\nand suggests a fix to turn them into inlinable wrappers around\ngo1.26's built-in new(expr) function:\n\n\tfunc varOf(x int) *int { return new(x) }\n\nIn addition, this analyzer suggests a fix for each call\nto one of the functions before it is transformed, so that\n\n\tuse(varOf(123))\n\nis replaced by:\n\n\t//go:fix inline\n\tuse(new(123))\n\nThe directive comment causes the 'inline' analyzer to suggest\nthat calls to such functions are inlined.\n\nWrapper functions such as varOf are common when working with Go\nserialization packages such as for JSON or protobuf, where pointers\nare often used to express optionality.",
"Default": "true",
"Status": ""
},
@@ -3449,7 +3449,7 @@
},
{
"Name": "newexpr",
- "Doc": "simplify code by using go1.26's new(expr)\n\nThis analyzer finds declarations of functions of this form:\n\n\tfunc varOf(x int) *int { return \u0026x }\n\nand suggests a fix to turn them into inlinable wrappers around\ngo1.26's built-in new(expr) function:\n\n\tfunc varOf(x int) *int { return new(x) }\n\nIn addition, this analyzer suggests a fix for each call\nto one of the functions before it is transformed, so that\n\n\tuse(varOf(123))\n\nis replaced by:\n\n\tuse(new(123))\n\n(Wrapper functions such as varOf are common when working with Go\nserialization packages such as for JSON or protobuf, where pointers\nare often used to express optionality.)",
+ "Doc": "simplify code by using go1.26's new(expr)\n\nThis analyzer finds declarations of functions of this form:\n\n\tfunc varOf(x int) *int { return \u0026x }\n\nand suggests a fix to turn them into inlinable wrappers around\ngo1.26's built-in new(expr) function:\n\n\tfunc varOf(x int) *int { return new(x) }\n\nIn addition, this analyzer suggests a fix for each call\nto one of the functions before it is transformed, so that\n\n\tuse(varOf(123))\n\nis replaced by:\n\n\t//go:fix inline\n\tuse(new(123))\n\nThe directive comment causes the 'inline' analyzer to suggest\nthat calls to such functions are inlined.\n\nWrapper functions such as varOf are common when working with Go\nserialization packages such as for JSON or protobuf, where pointers\nare often used to express optionality.",
"URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize#newexpr",
"Default": true
},