[tools] go/analysis/passes/modernize: newexpr: add //go:fix inline directives

8 views
Skip to first unread message

Alan Donovan (Gerrit)

unread,
Oct 30, 2025, 1:16:27 PM (7 days ago) Oct 30
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Alan Donovan has uploaded the change for review

Commit message

go/analysis/passes/modernize: newexpr: add //go:fix inline directives

This CL causes the newexpr suggested fix to add "inline" directives
on the function equivalent to new(expr) so that calls to it are
eventually melted away. This is safe because the inliner now checks
that the caller's Go version is at least as new as the callee's.

Updates golang/go#45624
Updates golang/go#75726
Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce

Change diff

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
},

Change information

Files:
  • M go/analysis/passes/modernize/doc.go
  • M go/analysis/passes/modernize/newexpr.go
  • M go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go.golden
  • M gopls/doc/analyzers.md
  • M gopls/internal/doc/api.json
Change size: M
Delta: 5 files changed, 28 insertions(+), 23 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce
Gerrit-Change-Number: 716561
Gerrit-PatchSet: 1
Gerrit-Owner: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Alan Donovan (Gerrit)

unread,
Oct 30, 2025, 1:16:43 PM (7 days ago) Oct 30
to goph...@pubsubhelper.golang.org, Robert Findley, Go LUCI, golang-co...@googlegroups.com
Attention needed from Robert Findley

Alan Donovan voted Auto-Submit+1

Auto-Submit+1
Open in Gerrit

Related details

Attention is currently required from:
  • Robert Findley
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce
Gerrit-Change-Number: 716561
Gerrit-PatchSet: 1
Gerrit-Owner: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-Attention: Robert Findley <rfin...@google.com>
Gerrit-Comment-Date: Thu, 30 Oct 2025 17:16:39 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Alan Donovan (Gerrit)

unread,
Oct 30, 2025, 1:17:44 PM (7 days ago) Oct 30
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Alan Donovan and Robert Findley

Alan Donovan uploaded new patchset

Alan Donovan uploaded patch set #2 to this change.
Open in Gerrit

Related details

Attention is currently required from:
  • Alan Donovan
  • Robert Findley
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce
Gerrit-Change-Number: 716561
Gerrit-PatchSet: 2
Gerrit-Owner: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-Attention: Alan Donovan <adon...@google.com>
Gerrit-Attention: Robert Findley <rfin...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Robert Findley (Gerrit)

unread,
Oct 30, 2025, 2:22:03 PM (7 days ago) Oct 30
to Alan Donovan, goph...@pubsubhelper.golang.org, Go LUCI, golang-co...@googlegroups.com
Attention needed from Alan Donovan

Robert Findley voted and added 1 comment

Votes added by Robert Findley

Code-Review+2

1 comment

Patchset-level comments
File-level comment, Patchset 2 (Latest):
Robert Findley . resolved

LGTM, modulo previous discussion.

Open in Gerrit

Related details

Attention is currently required from:
  • Alan Donovan
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce
Gerrit-Change-Number: 716561
Gerrit-PatchSet: 2
Gerrit-Owner: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-Attention: Alan Donovan <adon...@google.com>
Gerrit-Comment-Date: Thu, 30 Oct 2025 18:21:58 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
open
diffy

Alan Donovan (Gerrit)

unread,
Oct 30, 2025, 2:33:19 PM (7 days ago) Oct 30
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Alan Donovan

Alan Donovan uploaded new patchset

Alan Donovan uploaded patch set #3 to this change.
Following approvals got outdated and were removed:
  • TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
Open in Gerrit

Related details

Attention is currently required from:
  • Alan Donovan
Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce
    Gerrit-Change-Number: 716561
    Gerrit-PatchSet: 3
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Alan Donovan (Gerrit)

    unread,
    Oct 30, 2025, 4:07:18 PM (7 days ago) Oct 30
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Alan Donovan

    Alan Donovan uploaded new patchset

    Alan Donovan uploaded patch set #4 to this change.
    Following approvals got outdated and were removed:
    • TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Alan Donovan
    Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce
    Gerrit-Change-Number: 716561
    Gerrit-PatchSet: 4
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Alan Donovan (Gerrit)

    unread,
    Nov 3, 2025, 5:20:57 PM (3 days ago) Nov 3
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Alan Donovan

    Alan Donovan uploaded new patchset

    Alan Donovan uploaded patch set #5 to this change.
    Following approvals got outdated and were removed:
    • TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Alan Donovan
    Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce
    Gerrit-Change-Number: 716561
    Gerrit-PatchSet: 5
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Alan Donovan (Gerrit)

    unread,
    Nov 3, 2025, 5:22:47 PM (3 days ago) Nov 3
    to goph...@pubsubhelper.golang.org, Go LUCI, Robert Findley, golang-co...@googlegroups.com

    Alan Donovan voted Auto-Submit+1

    Auto-Submit+1
    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce
    Gerrit-Change-Number: 716561
    Gerrit-PatchSet: 5
    Gerrit-Owner: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Robert Findley <rfin...@google.com>
    Gerrit-Comment-Date: Mon, 03 Nov 2025 22:22:43 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Alan Donovan (Gerrit)

    unread,
    Nov 3, 2025, 7:47:01 PM (2 days ago) Nov 3
    to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Go LUCI, Robert Findley, golang-co...@googlegroups.com

    Alan Donovan submitted the change

    Unreviewed changes

    2 is the latest approved patch-set.
    No files were changed between the latest approved patch-set and the submitted one.

    Change information

    Commit message:
    go/analysis/passes/modernize: newexpr: add //go:fix inline directives

    This CL causes the newexpr suggested fix to add "inline" directives
    on the function equivalent to new(expr) so that calls to it are
    eventually melted away. This is safe because the inliner now checks
    that the caller's Go version is at least as new as the callee's.

    Updates golang/go#45624
    Updates golang/go#75726
    Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce
    Auto-Submit: Alan Donovan <adon...@google.com>
    Reviewed-by: Robert Findley <rfin...@google.com>
    Files:
    • M go/analysis/passes/modernize/doc.go
    • M go/analysis/passes/modernize/newexpr.go
    • M go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go.golden
    • M gopls/doc/analyzers.md
    • M gopls/internal/doc/api.json
    Change size: M
    Delta: 5 files changed, 28 insertions(+), 23 deletions(-)
    Branch: refs/heads/master
    Submit Requirements:
    • requirement satisfiedCode-Review: +2 by Robert Findley
    • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
    Open in Gerrit
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: merged
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I36d27c7fa92965340da739ab71ed0cddff0d03ce
    Gerrit-Change-Number: 716561
    Gerrit-PatchSet: 6
    open
    diffy
    satisfied_requirement
    Reply all
    Reply to author
    Forward
    0 new messages