Skip to content

Commit 32e1e0a

Browse files
mvdanlu4p
authored andcommitted
take advantage of some APIs added in Go 1.24
Primarily new iterator APIs in the strings and go/types packages. While here, verify that the processImportCfg hack is stil needed as of go1.24.2.
1 parent 2bb1d49 commit 32e1e0a

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

main.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ func (tf *transformer) transformAsm(args []string) ([]string, error) {
695695

696696
// Preprocessor lines to include another file.
697697
// For example: #include "foo.h"
698-
if quoted := strings.TrimPrefix(line, "#include"); quoted != line {
698+
if quoted, ok := strings.CutPrefix(line, "#include"); ok {
699699
quoted = strings.TrimSpace(quoted)
700700
path, err := strconv.Unquote(quoted)
701701
if err != nil { // note that strconv.Unquote errors do not include the input string
@@ -1185,10 +1185,9 @@ func (tf *transformer) transformLinkname(localName, newName string) (string, str
11851185

11861186
var newForeignName string
11871187
if receiver, name, ok := strings.Cut(foreignName, "."); ok {
1188-
if strings.HasPrefix(receiver, "(*") {
1188+
if receiver, ok = strings.CutPrefix(receiver, "(*"); ok {
11891189
// pkg/path.(*Receiver).method
1190-
receiver = strings.TrimPrefix(receiver, "(*")
1191-
receiver = strings.TrimSuffix(receiver, ")")
1190+
receiver, _ = strings.CutSuffix(receiver, ")")
11921191
receiver = "(*" + hashWithPackage(lpkg, receiver) + ")"
11931192
} else {
11941193
// pkg/path.Receiver.method
@@ -1234,7 +1233,7 @@ func (tf *transformer) processImportCfg(flags []string, requiredPkgs []string) (
12341233
}
12351234
}
12361235

1237-
for _, line := range strings.Split(string(data), "\n") {
1236+
for line := range strings.SplitSeq(string(data), "\n") {
12381237
if line == "" || strings.HasPrefix(line, "#") {
12391238
continue
12401239
}
@@ -1332,7 +1331,7 @@ func (tf *transformer) processImportCfg(flags []string, requiredPkgs []string) (
13321331
// See exporttest/*.go in testdata/scripts/test.txt.
13331332
// For now, spot the pattern and avoid the unnecessary error;
13341333
// the dependency is unused, so the packagefile line is redundant.
1335-
// This still triggers as of go1.21.
1334+
// This still triggers as of go1.24.
13361335
if strings.HasSuffix(tf.curPkg.ImportPath, ".test]") && strings.HasPrefix(tf.curPkg.ImportPath, impPath) {
13371336
continue
13381337
}
@@ -2257,7 +2256,7 @@ func flagValue(flags []string, name string) string {
22572256
// those whose values compose a list.
22582257
func flagValueIter(flags []string, name string, fn func(string)) {
22592258
for i, arg := range flags {
2260-
if val := strings.TrimPrefix(arg, name+"="); val != arg {
2259+
if val, ok := strings.CutPrefix(arg, name+"="); ok {
22612260
// -name=value
22622261
fn(val)
22632262
}

reflect.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,14 @@ func (ri *reflectInspector) ignoreReflectedTypes(ssaPkg *ssa.Package) {
6868
// so some logic is required to find the methods a type has
6969

7070
method := func(mset *types.MethodSet) {
71-
for i := range mset.Len() {
72-
at := mset.At(i)
73-
71+
for at := range mset.Methods() {
7472
if m := ssaPkg.Prog.MethodValue(at); m != nil {
7573
ri.checkFunction(m)
7674
} else {
7775
m := at.Obj().(*types.Func)
7876
// handle interface declarations
7977
ri.checkInterfaceMethod(m)
8078
}
81-
8279
}
8380
}
8481

@@ -110,15 +107,14 @@ func (ri *reflectInspector) checkMethodSignature(reflectParams map[int]bool, sig
110107
return
111108
}
112109

113-
params := sig.Params()
114-
for i := range params.Len() {
110+
i := 0
111+
for param := range sig.Params().Variables() {
115112
if reflectParams[i] {
113+
i++
116114
continue
117115
}
118116

119117
ignore := false
120-
param := params.At(i)
121-
122118
switch x := param.Type().(type) {
123119
case *types.Struct:
124120
ignore = true
@@ -136,6 +132,7 @@ func (ri *reflectInspector) checkMethodSignature(reflectParams map[int]bool, sig
136132
reflectParams[i] = true
137133
ri.recursivelyRecordUsedForReflect(param.Type())
138134
}
135+
i++
139136
}
140137
}
141138

0 commit comments

Comments
 (0)