[tools] go/analysis/passes/modernize: fix duplicate //go:fix inline directive

1 view
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
12:49 AM (13 hours ago) 12:49 AM
to goph...@pubsubhelper.golang.org, Kally Dev, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

go/analysis/passes/modernize: fix duplicate //go:fix inline directive

The newexpr analyzer used strings.Contains to check whether a //go:fix inline directive already exists. This could miss cases where the directive was present but formatted differently, and could also match partial strings.

Use astutil.Directives to structurally check for the directive, preventing duplicate additions when go fix is run multiple times.

Fixes golang/go#78196
Change-Id: I7a5d32deb57fe7a90f472d6e49cc5f485a9b13f4
GitHub-Last-Rev: e5ba0820c3eb4800e4e92c6e339d852aa26785ba
GitHub-Pull-Request: golang/tools#622

Change diff

diff --git a/go/analysis/passes/modernize/newexpr.go b/go/analysis/passes/modernize/newexpr.go
index cd924ec..c99d9c2 100644
--- a/go/analysis/passes/modernize/newexpr.go
+++ b/go/analysis/passes/modernize/newexpr.go
@@ -6,12 +6,11 @@

import (
_ "embed"
+ "fmt"
"go/ast"
"go/token"
"go/types"
- "strings"
-
- "fmt"
+ "slices"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
@@ -94,7 +93,9 @@
// 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") {
+ if !slices.ContainsFunc(astutil.Directives(decl.Doc), func(d *astutil.Directive) bool {
+ return d.Tool == "go" && d.Name == "fix" && d.Args == "inline"
+ }) {
edits = append(edits, analysis.TextEdit{
Pos: decl.Pos(),
End: decl.Pos(),
diff --git a/go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go b/go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go
index e36898a..df78445 100644
--- a/go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go
+++ b/go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go
@@ -11,6 +11,9 @@

func varOf[T any](x T) *T { return &x } // want `varOf can be an inlinable wrapper around new\(expr\)` varOf:"newlike"

+//go:fix inline
+func alreadyAnnotated[T any](x T) *T { return &x } // want `alreadyAnnotated can be an inlinable wrapper around new\(expr\)` alreadyAnnotated:"newlike"
+
var (
s struct {
int
@@ -24,6 +27,7 @@
_ = varOf(int64(123)) // want `call of varOf\(x\) can be simplified to new\(x\)`
_ = varOf[int](123) // want `call of varOf\(x\) can be simplified to new\(x\)`
_ = varOf[int64](123) // nope: implicit conversion from untyped int to int64
- _ = varOf( // want `call of varOf\(x\) can be simplified to new\(x\)`
+ _ = varOf( // want `call of varOf\(x\) can be simplified to new\(x\)`
varOf(123)) // want `call of varOf\(x\) can be simplified to new\(x\)`
+ _ = alreadyAnnotated[int](123) // want `call of alreadyAnnotated\(x\) can be simplified to new\(x\)`
)
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 8f3fe76..52060c4 100644
--- a/go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go.golden
+++ b/go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go.golden
@@ -16,6 +16,9 @@
//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"

+//go:fix inline
+func alreadyAnnotated[T any](x T) *T { return new(x) } // want `alreadyAnnotated can be an inlinable wrapper around new\(expr\)` alreadyAnnotated:"newlike"
+
var (
s struct {
int
@@ -31,4 +34,5 @@
_ = varOf[int64](123) // nope: implicit conversion from untyped int to int64
_ = new( // want `call of varOf\(x\) can be simplified to new\(x\)`
new(123)) // want `call of varOf\(x\) can be simplified to new\(x\)`
+ _ = new(123) // want `call of alreadyAnnotated\(x\) can be simplified to new\(x\)`
)
\ No newline at end of file

Change information

Files:
  • M go/analysis/passes/modernize/newexpr.go
  • M go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go
  • M go/analysis/passes/modernize/testdata/src/newexpr/newexpr.go.golden
Change size: S
Delta: 3 files changed, 14 insertions(+), 5 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: I7a5d32deb57fe7a90f472d6e49cc5f485a9b13f4
Gerrit-Change-Number: 756300
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Kally Dev <kall...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
12:49 AM (13 hours ago) 12:49 AM
to Kally Dev, Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gopher Robot added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Gopher Robot . unresolved

I spotted some possible problems with your PR:

  1. You have a long 222 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)

Please address any problems by updating the GitHub PR.

When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

For more details, see:

(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not 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: I7a5d32deb57fe7a90f472d6e49cc5f485a9b13f4
    Gerrit-Change-Number: 756300
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Kally Dev <kall...@gmail.com>
    Gerrit-Comment-Date: Wed, 18 Mar 2026 04:49:05 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gopher Robot (Gerrit)

    unread,
    12:53 AM (13 hours ago) 12:53 AM
    to Kally Dev, Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Message from Gopher Robot

    Congratulations on opening your first change. Thank you for your contribution!

    Next steps:
    A maintainer will review your change and provide feedback. See
    https://go.dev/doc/contribute#review for more info and tips to get your
    patch through code review.

    Most changes in the Go project go through a few rounds of revision. This can be
    surprising to people new to the project. The careful, iterative review process
    is our way of helping mentor contributors and ensuring that their contributions
    have a lasting impact.

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not 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: I7a5d32deb57fe7a90f472d6e49cc5f485a9b13f4
    Gerrit-Change-Number: 756300
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Kally Dev <kall...@gmail.com>
    Gerrit-Comment-Date: Wed, 18 Mar 2026 04:53:36 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gerrit Bot (Gerrit)

    unread,
    1:11 AM (13 hours ago) 1:11 AM
    to Kally Dev, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Gerrit Bot uploaded new patchset

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

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not 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: I7a5d32deb57fe7a90f472d6e49cc5f485a9b13f4
    Gerrit-Change-Number: 756300
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages