[tools] go/analysis/passes/modernize: unsafefuncs: ptr+int => unsafe.Add

0 views
Skip to first unread message

Alan Donovan (Gerrit)

unread,
Dec 2, 2025, 9:58:14 PM (2 days ago) Dec 2
to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Go LUCI, Madeline Kalil, Dominik Honnef, Daniel Morsing, Robert Findley, David Chase, Michael Matloob, Gopher Robot, golang-co...@googlegroups.com

Alan Donovan submitted the change with unreviewed changes

Unreviewed changes

3 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:

```
The name of the file: go/analysis/passes/modernize/unsafefuncs.go
Insertions: 4, Deletions: 0.

@@ -147,6 +147,10 @@

// Variant: sum.y operand was converted from another integer type.
// Discard the conversion, as Add is generic over integers.
+ //
+ // e.g. unsafe.Pointer(uintptr(ptr) + uintptr(len(s)))
+ // -------- -
+ // unsafe.Add ( ptr, len(s))
if t, _ := isConversion(curY); t != nil && isInteger(t) {
edits = append(edits, deleteConv(curY)...)
}
```

Change information

Commit message:
go/analysis/passes/modernize: unsafefuncs: ptr+int => unsafe.Add

This CL adds a new modernizer (only to gopls for now) that
replaces unsafe pointer arithmetic by calls to helper functions
such as go1.17's unsafe.Add.

We really need some kind of template matcher like the one
in staticcheck, ideally integrated with cursors.

+ tests, docs.

For golang/go#76648
Change-Id: Ia3e7d603efd8a01e723370b485ab83b6838522b8
Reviewed-by: Daniel Morsing <daniel....@gmail.com>
Reviewed-by: Madeline Kalil <mka...@google.com>
Files:
  • M go/analysis/passes/modernize/doc.go
  • M go/analysis/passes/modernize/modernize.go
  • M go/analysis/passes/modernize/modernize_test.go
  • A go/analysis/passes/modernize/testdata/src/unsafefuncs/unsafefuncs.go
  • A go/analysis/passes/modernize/testdata/src/unsafefuncs/unsafefuncs.go.golden
  • A go/analysis/passes/modernize/unsafefuncs.go
  • M go/types/typeutil/callee.go
  • M gopls/doc/analyzers.md
  • M gopls/internal/doc/api.json
  • M gopls/internal/settings/analysis.go
  • M internal/goplsexport/export.go
  • M internal/versions/features.go
Change size: L
Delta: 12 files changed, 319 insertions(+), 0 deletions(-)
Branch: refs/heads/master
Submit Requirements:
  • requirement satisfiedCode-Review: +1 by Daniel Morsing, +2 by Madeline Kalil
  • 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: Ia3e7d603efd8a01e723370b485ab83b6838522b8
Gerrit-Change-Number: 725680
Gerrit-PatchSet: 5
Gerrit-Owner: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Daniel Morsing <daniel....@gmail.com>
Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
Gerrit-CC: David Chase <drc...@google.com>
Gerrit-CC: Dominik Honnef <dom...@honnef.co>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-CC: Michael Matloob <mat...@golang.org>
Gerrit-CC: Robert Findley <rfin...@google.com>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages