all: simplify code by using modern Go constructs

Generated using modernize by running:

    go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

Change-Id: Ifc7d61cf6735cc53f2bdf890a338961f55075af5
Reviewed-on: https://go-review.googlesource.com/c/sys/+/661975
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Tobias Klauser
2025-04-03 13:04:48 -07:00
committed by Gopher Robot
parent 1b2bd6bb49
commit 01aaa8342f
8 changed files with 33 additions and 54 deletions
+2 -2
View File
@@ -13,7 +13,7 @@ import "strconv"
// https://golang.org/cl/209597. // https://golang.org/cl/209597.
func parseRelease(rel string) (major, minor, patch int, ok bool) { func parseRelease(rel string) (major, minor, patch int, ok bool) {
// Strip anything after a dash or plus. // Strip anything after a dash or plus.
for i := 0; i < len(rel); i++ { for i := range len(rel) {
if rel[i] == '-' || rel[i] == '+' { if rel[i] == '-' || rel[i] == '+' {
rel = rel[:i] rel = rel[:i]
break break
@@ -21,7 +21,7 @@ func parseRelease(rel string) (major, minor, patch int, ok bool) {
} }
next := func() (int, bool) { next := func() (int, bool) {
for i := 0; i < len(rel); i++ { for i := range len(rel) {
if rel[i] == '.' { if rel[i] == '.' {
ver, err := strconv.Atoi(rel[:i]) ver, err := strconv.Atoi(rel[:i])
rel = rel[i+1:] rel = rel[i+1:]
+1 -1
View File
@@ -107,7 +107,7 @@ func TestDirentRepeat(t *testing.T) {
d := t.TempDir() d := t.TempDir()
var files []string var files []string
for i := 0; i < N; i++ { for i := range N {
files = append(files, fmt.Sprintf("file%d", i)) files = append(files, fmt.Sprintf("file%d", i))
} }
for _, file := range files { for _, file := range files {
+5 -5
View File
@@ -15,21 +15,21 @@ import (
func TestFdSet(t *testing.T) { func TestFdSet(t *testing.T) {
var fdSet unix.FdSet var fdSet unix.FdSet
fdSet.Zero() fdSet.Zero()
for fd := 0; fd < unix.FD_SETSIZE; fd++ { for fd := range unix.FD_SETSIZE {
if fdSet.IsSet(fd) { if fdSet.IsSet(fd) {
t.Fatalf("Zero did not clear fd %d", fd) t.Fatalf("Zero did not clear fd %d", fd)
} }
fdSet.Set(fd) fdSet.Set(fd)
} }
for fd := 0; fd < unix.FD_SETSIZE; fd++ { for fd := range unix.FD_SETSIZE {
if !fdSet.IsSet(fd) { if !fdSet.IsSet(fd) {
t.Fatalf("IsSet(%d): expected true, got false", fd) t.Fatalf("IsSet(%d): expected true, got false", fd)
} }
} }
fdSet.Zero() fdSet.Zero()
for fd := 0; fd < unix.FD_SETSIZE; fd++ { for fd := range unix.FD_SETSIZE {
if fdSet.IsSet(fd) { if fdSet.IsSet(fd) {
t.Fatalf("Zero did not clear fd %d", fd) t.Fatalf("Zero did not clear fd %d", fd)
} }
@@ -39,7 +39,7 @@ func TestFdSet(t *testing.T) {
fdSet.Set(fd) fdSet.Set(fd)
} }
for fd := 0; fd < unix.FD_SETSIZE; fd++ { for fd := range unix.FD_SETSIZE {
if fd&0x1 == 0x1 { if fd&0x1 == 0x1 {
if !fdSet.IsSet(fd) { if !fdSet.IsSet(fd) {
t.Fatalf("IsSet(%d): expected true, got false", fd) t.Fatalf("IsSet(%d): expected true, got false", fd)
@@ -55,7 +55,7 @@ func TestFdSet(t *testing.T) {
fdSet.Clear(fd) fdSet.Clear(fd)
} }
for fd := 0; fd < unix.FD_SETSIZE; fd++ { for fd := range unix.FD_SETSIZE {
if fdSet.IsSet(fd) { if fdSet.IsSet(fd) {
t.Fatalf("Clear(%d) did not clear fd", fd) t.Fatalf("Clear(%d) did not clear fd", fd)
} }
+2 -2
View File
@@ -137,7 +137,7 @@ type filterFn func(codeElem) bool
// filter parses and filters Go source code from src, removing top // filter parses and filters Go source code from src, removing top
// level declarations using keep as predicate. // level declarations using keep as predicate.
// For src parameter, please see docs for parser.ParseFile. // For src parameter, please see docs for parser.ParseFile.
func filter(src interface{}, keep filterFn) ([]byte, error) { func filter(src any, keep filterFn) ([]byte, error) {
// Parse the src into an ast // Parse the src into an ast
fset := token.NewFileSet() fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", src, parser.ParseComments) f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
@@ -243,7 +243,7 @@ func getCommonSet(files []srcFile) (*codeSet, error) {
// getCodeSet returns the set of all top-level consts, types, and funcs from src. // getCodeSet returns the set of all top-level consts, types, and funcs from src.
// src must be string, []byte, or io.Reader (see go/parser.ParseFile docs) // src must be string, []byte, or io.Reader (see go/parser.ParseFile docs)
func getCodeSet(src interface{}) (*codeSet, error) { func getCodeSet(src any) (*codeSet, error) {
set := newCodeSet() set := newCodeSet()
fset := token.NewFileSet() fset := token.NewFileSet()
+1 -1
View File
@@ -498,7 +498,7 @@ func diffLines(t *testing.T, got, expected []byte) {
func addLineNr(src []byte) []byte { func addLineNr(src []byte) []byte {
lines := bytes.Split(src, []byte("\n")) lines := bytes.Split(src, []byte("\n"))
for i, line := range lines { for i, line := range lines {
lines[i] = []byte(fmt.Sprintf("%d: %s", i+1, line)) lines[i] = fmt.Appendf(nil, "%d: %s", i+1, line)
} }
return bytes.Join(lines, []byte("\n")) return bytes.Join(lines, []byte("\n"))
} }
+16 -26
View File
@@ -13,6 +13,7 @@ package unix
import ( import (
"encoding/binary" "encoding/binary"
"slices"
"strconv" "strconv"
"syscall" "syscall"
"time" "time"
@@ -417,7 +418,7 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
return nil, 0, EINVAL return nil, 0, EINVAL
} }
sa.raw.Family = AF_UNIX sa.raw.Family = AF_UNIX
for i := 0; i < n; i++ { for i := range n {
sa.raw.Path[i] = int8(name[i]) sa.raw.Path[i] = int8(name[i])
} }
// length is family (uint16), name, NUL. // length is family (uint16), name, NUL.
@@ -507,7 +508,7 @@ func (sa *SockaddrL2) sockaddr() (unsafe.Pointer, _Socklen, error) {
psm := (*[2]byte)(unsafe.Pointer(&sa.raw.Psm)) psm := (*[2]byte)(unsafe.Pointer(&sa.raw.Psm))
psm[0] = byte(sa.PSM) psm[0] = byte(sa.PSM)
psm[1] = byte(sa.PSM >> 8) psm[1] = byte(sa.PSM >> 8)
for i := 0; i < len(sa.Addr); i++ { for i := range len(sa.Addr) {
sa.raw.Bdaddr[i] = sa.Addr[len(sa.Addr)-1-i] sa.raw.Bdaddr[i] = sa.Addr[len(sa.Addr)-1-i]
} }
cid := (*[2]byte)(unsafe.Pointer(&sa.raw.Cid)) cid := (*[2]byte)(unsafe.Pointer(&sa.raw.Cid))
@@ -589,11 +590,11 @@ func (sa *SockaddrCAN) sockaddr() (unsafe.Pointer, _Socklen, error) {
sa.raw.Family = AF_CAN sa.raw.Family = AF_CAN
sa.raw.Ifindex = int32(sa.Ifindex) sa.raw.Ifindex = int32(sa.Ifindex)
rx := (*[4]byte)(unsafe.Pointer(&sa.RxID)) rx := (*[4]byte)(unsafe.Pointer(&sa.RxID))
for i := 0; i < 4; i++ { for i := range 4 {
sa.raw.Addr[i] = rx[i] sa.raw.Addr[i] = rx[i]
} }
tx := (*[4]byte)(unsafe.Pointer(&sa.TxID)) tx := (*[4]byte)(unsafe.Pointer(&sa.TxID))
for i := 0; i < 4; i++ { for i := range 4 {
sa.raw.Addr[i+4] = tx[i] sa.raw.Addr[i+4] = tx[i]
} }
return unsafe.Pointer(&sa.raw), SizeofSockaddrCAN, nil return unsafe.Pointer(&sa.raw), SizeofSockaddrCAN, nil
@@ -618,11 +619,11 @@ func (sa *SockaddrCANJ1939) sockaddr() (unsafe.Pointer, _Socklen, error) {
sa.raw.Family = AF_CAN sa.raw.Family = AF_CAN
sa.raw.Ifindex = int32(sa.Ifindex) sa.raw.Ifindex = int32(sa.Ifindex)
n := (*[8]byte)(unsafe.Pointer(&sa.Name)) n := (*[8]byte)(unsafe.Pointer(&sa.Name))
for i := 0; i < 8; i++ { for i := range 8 {
sa.raw.Addr[i] = n[i] sa.raw.Addr[i] = n[i]
} }
p := (*[4]byte)(unsafe.Pointer(&sa.PGN)) p := (*[4]byte)(unsafe.Pointer(&sa.PGN))
for i := 0; i < 4; i++ { for i := range 4 {
sa.raw.Addr[i+8] = p[i] sa.raw.Addr[i+8] = p[i]
} }
sa.raw.Addr[12] = sa.Addr sa.raw.Addr[12] = sa.Addr
@@ -911,7 +912,7 @@ func (sa *SockaddrIUCV) sockaddr() (unsafe.Pointer, _Socklen, error) {
// These are EBCDIC encoded by the kernel, but we still need to pad them // These are EBCDIC encoded by the kernel, but we still need to pad them
// with blanks. Initializing with blanks allows the caller to feed in either // with blanks. Initializing with blanks allows the caller to feed in either
// a padded or an unpadded string. // a padded or an unpadded string.
for i := 0; i < 8; i++ { for i := range 8 {
sa.raw.Nodeid[i] = ' ' sa.raw.Nodeid[i] = ' '
sa.raw.User_id[i] = ' ' sa.raw.User_id[i] = ' '
sa.raw.Name[i] = ' ' sa.raw.Name[i] = ' '
@@ -1148,7 +1149,7 @@ func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
var user [8]byte var user [8]byte
var name [8]byte var name [8]byte
for i := 0; i < 8; i++ { for i := range 8 {
user[i] = byte(pp.User_id[i]) user[i] = byte(pp.User_id[i])
name[i] = byte(pp.Name[i]) name[i] = byte(pp.Name[i])
} }
@@ -1173,11 +1174,11 @@ func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
Ifindex: int(pp.Ifindex), Ifindex: int(pp.Ifindex),
} }
name := (*[8]byte)(unsafe.Pointer(&sa.Name)) name := (*[8]byte)(unsafe.Pointer(&sa.Name))
for i := 0; i < 8; i++ { for i := range 8 {
name[i] = pp.Addr[i] name[i] = pp.Addr[i]
} }
pgn := (*[4]byte)(unsafe.Pointer(&sa.PGN)) pgn := (*[4]byte)(unsafe.Pointer(&sa.PGN))
for i := 0; i < 4; i++ { for i := range 4 {
pgn[i] = pp.Addr[i+8] pgn[i] = pp.Addr[i+8]
} }
addr := (*[1]byte)(unsafe.Pointer(&sa.Addr)) addr := (*[1]byte)(unsafe.Pointer(&sa.Addr))
@@ -1188,11 +1189,11 @@ func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
Ifindex: int(pp.Ifindex), Ifindex: int(pp.Ifindex),
} }
rx := (*[4]byte)(unsafe.Pointer(&sa.RxID)) rx := (*[4]byte)(unsafe.Pointer(&sa.RxID))
for i := 0; i < 4; i++ { for i := range 4 {
rx[i] = pp.Addr[i] rx[i] = pp.Addr[i]
} }
tx := (*[4]byte)(unsafe.Pointer(&sa.TxID)) tx := (*[4]byte)(unsafe.Pointer(&sa.TxID))
for i := 0; i < 4; i++ { for i := range 4 {
tx[i] = pp.Addr[i+4] tx[i] = pp.Addr[i+4]
} }
return sa, nil return sa, nil
@@ -2216,10 +2217,7 @@ func readvRacedetect(iovecs []Iovec, n int, err error) {
return return
} }
for i := 0; n > 0 && i < len(iovecs); i++ { for i := 0; n > 0 && i < len(iovecs); i++ {
m := int(iovecs[i].Len) m := min(int(iovecs[i].Len), n)
if m > n {
m = n
}
n -= m n -= m
if m > 0 { if m > 0 {
raceWriteRange(unsafe.Pointer(iovecs[i].Base), m) raceWriteRange(unsafe.Pointer(iovecs[i].Base), m)
@@ -2270,10 +2268,7 @@ func writevRacedetect(iovecs []Iovec, n int) {
return return
} }
for i := 0; n > 0 && i < len(iovecs); i++ { for i := 0; n > 0 && i < len(iovecs); i++ {
m := int(iovecs[i].Len) m := min(int(iovecs[i].Len), n)
if m > n {
m = n
}
n -= m n -= m
if m > 0 { if m > 0 {
raceReadRange(unsafe.Pointer(iovecs[i].Base), m) raceReadRange(unsafe.Pointer(iovecs[i].Base), m)
@@ -2320,12 +2315,7 @@ func isGroupMember(gid int) bool {
return false return false
} }
for _, g := range groups { return slices.Contains(groups, gid)
if g == gid {
return true
}
}
return false
} }
func isCapDacOverrideSet() bool { func isCapDacOverrideSet() bool {
+3 -3
View File
@@ -367,7 +367,7 @@ func TestTime(t *testing.T) {
var now time.Time var now time.Time
for i := 0; i < 10; i++ { for range 10 {
ut, err = unix.Time(nil) ut, err = unix.Time(nil)
if err != nil { if err != nil {
t.Fatalf("Time: %v", err) t.Fatalf("Time: %v", err)
@@ -555,7 +555,7 @@ func TestSchedSetaffinity(t *testing.T) {
cpu = 1 cpu = 1
if !oldMask.IsSet(cpu) { if !oldMask.IsSet(cpu) {
newMask.Zero() newMask.Zero()
for i := 0; i < len(oldMask); i++ { for i := range len(oldMask) {
if oldMask.IsSet(i) { if oldMask.IsSet(i) {
newMask.Set(i) newMask.Set(i)
break break
@@ -878,7 +878,7 @@ func openMountByID(mountID int) (f *os.File, err error) {
} }
defer mi.Close() defer mi.Close()
bs := bufio.NewScanner(mi) bs := bufio.NewScanner(mi)
wantPrefix := []byte(fmt.Sprintf("%v ", mountID)) wantPrefix := fmt.Appendf(nil, "%v ", mountID)
for bs.Scan() { for bs.Scan() {
if !bytes.HasPrefix(bs.Bytes(), wantPrefix) { if !bytes.HasPrefix(bs.Bytes(), wantPrefix) {
continue continue
+3 -14
View File
@@ -10,6 +10,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"slices"
"strings" "strings"
"testing" "testing"
@@ -62,13 +63,7 @@ func TestXattr(t *testing.T) {
// name and Listxattr doesn't return the namespace prefix. // name and Listxattr doesn't return the namespace prefix.
xattrWant = strings.TrimPrefix(xattrWant, "user.") xattrWant = strings.TrimPrefix(xattrWant, "user.")
} }
found := false found := slices.Contains(xattrs, xattrWant)
for _, name := range xattrs {
if name == xattrWant {
found = true
break
}
}
if !found { if !found {
t.Errorf("Listxattr did not return previously set attribute %q in attributes %v", xattrName, xattrs) t.Errorf("Listxattr did not return previously set attribute %q in attributes %v", xattrName, xattrs)
@@ -170,13 +165,7 @@ func TestFdXattr(t *testing.T) {
// name and Listxattr doesn't return the namespace prefix. // name and Listxattr doesn't return the namespace prefix.
xattrWant = strings.TrimPrefix(xattrWant, "user.") xattrWant = strings.TrimPrefix(xattrWant, "user.")
} }
found := false found := slices.Contains(xattrs, xattrWant)
for _, name := range xattrs {
if name == xattrWant {
found = true
break
}
}
if !found { if !found {
t.Errorf("Flistxattr did not return previously set attribute %q in attributes %v", xattrName, xattrs) t.Errorf("Flistxattr did not return previously set attribute %q in attributes %v", xattrName, xattrs)