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
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
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
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.)
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
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.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |