mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-10 05:53:39 +00:00
all: modernize (#5498)
* modernize string cut usage * modernize string cut prefix usage * modernize slices helper usage * modernize min and max usage * modernize loop variable copies * modernize integer range loops * modernize map copy loops * modernize go types iterator usage * modernize empty interface usage * modernize atomic type usage * modernize string builders * modernize string split iteration * modernize remaining loop variable copies * modernize usb cdc min usage * modernize src integer range loops * modernize example empty interface usage * modernize src min and max usage * modernize src integer range loops * modernize src empty interface usage * modernize src atomic type usage * modernize reflect type lookups * modernize review nits
This commit is contained in:
@@ -25,7 +25,7 @@ func main() {
|
||||
func escapesToHeap() {
|
||||
n := rand.Intn(100)
|
||||
println("Doing ", n, " iterations")
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
s := make([]byte, i)
|
||||
_ = append(s, 42)
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ func main() {
|
||||
printItf(Number(3))
|
||||
s := Stringer(thing)
|
||||
println("Stringer.String():", s.String())
|
||||
var itf interface{} = s
|
||||
var itf any = s
|
||||
println("Stringer.(*Thing).String():", itf.(Stringer).String())
|
||||
|
||||
// unusual calls
|
||||
@@ -124,7 +124,7 @@ func strlen(s string) int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
func printItf(val interface{}) {
|
||||
func printItf(val any) {
|
||||
switch val := val.(type) {
|
||||
case Doubler:
|
||||
println("is Doubler:", val.Double())
|
||||
|
||||
@@ -45,11 +45,8 @@ func Compare(a, b []byte) int {
|
||||
// This function was copied from the Go 1.23 source tree (with runtime_cmpstring
|
||||
// manually inlined).
|
||||
func CompareString(a, b string) int {
|
||||
l := len(a)
|
||||
if len(b) < l {
|
||||
l = len(b)
|
||||
}
|
||||
for i := 0; i < l; i++ {
|
||||
l := min(len(b), len(a))
|
||||
for i := range l {
|
||||
c1, c2 := a[i], b[i]
|
||||
if c1 < c2 {
|
||||
return -1
|
||||
@@ -170,7 +167,7 @@ const PrimeRK = 16777619
|
||||
// This function was removed in Go 1.22.
|
||||
func HashStrBytes(sep []byte) (uint32, uint32) {
|
||||
hash := uint32(0)
|
||||
for i := 0; i < len(sep); i++ {
|
||||
for i := range sep {
|
||||
hash = hash*PrimeRK + uint32(sep[i])
|
||||
}
|
||||
var pow, sq uint32 = 1, PrimeRK
|
||||
@@ -249,7 +246,7 @@ func IndexRabinKarpBytes(s, sep []byte) int {
|
||||
hashsep, pow := HashStrBytes(sep)
|
||||
n := len(sep)
|
||||
var h uint32
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
h = h*PrimeRK + uint32(s[i])
|
||||
}
|
||||
if h == hashsep && Equal(s[:n], sep) {
|
||||
@@ -276,7 +273,7 @@ func IndexRabinKarp[T string | []byte](s, sep T) int {
|
||||
hashss, pow := HashStr(sep)
|
||||
n := len(sep)
|
||||
var h uint32
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
h = h*PrimeRK + uint32(s[i])
|
||||
}
|
||||
if h == hashss && string(s[:n]) == string(sep) {
|
||||
|
||||
@@ -12,8 +12,8 @@ func CaseUnmarshaler[T ~uint8 | ~uint16 | ~uint32](cases []string) func(v *T, te
|
||||
return &emptyTextError{}
|
||||
}
|
||||
s := string(text)
|
||||
for i := 0; i < len(cases); i++ {
|
||||
if cases[i] == s {
|
||||
for i, c := range cases {
|
||||
if c == s {
|
||||
*v = T(i)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -268,7 +268,7 @@ func TestRing512_PutOversize(t *testing.T) {
|
||||
|
||||
func TestRing512_MultiplePutPeekDiscard(t *testing.T) {
|
||||
var r ring512
|
||||
for i := 0; i < 2000; i++ {
|
||||
for i := range 2000 {
|
||||
msg := []byte(fmt.Sprintf("msg%04d", i))
|
||||
if !r.Put(msg) {
|
||||
t.Fatalf("Put failed at iteration %d, Free=%d, Used=%d", i, r.Free(), r.Used())
|
||||
@@ -290,7 +290,7 @@ func TestRing512_HeadTailOverflow(t *testing.T) {
|
||||
t.Fatalf("Used=%d Free=%d, want 0/512", r.Used(), r.Free())
|
||||
}
|
||||
|
||||
for i := 0; i < 300; i++ {
|
||||
for i := range 300 {
|
||||
data := []byte{byte(i), byte(i + 1), byte(i + 2)}
|
||||
if !r.Put(data) {
|
||||
t.Fatalf("Put failed at iter %d (head=%d tail=%d)", i, r.head.Load(), r.tail.Load())
|
||||
@@ -370,7 +370,7 @@ func TestRing512_PeekTotalEqualsUsed(t *testing.T) {
|
||||
// --- Concurrent SPSC Test ---
|
||||
|
||||
func TestRing512_SPSC(t *testing.T) {
|
||||
for trial := 0; trial < 20; trial++ {
|
||||
for trial := range 20 {
|
||||
var r ring512
|
||||
const totalBytes = 1 << 18
|
||||
produced := make([]byte, totalBytes)
|
||||
@@ -431,7 +431,7 @@ func TestRing512_SPSCSmallChunks(t *testing.T) {
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < totalBytes; i++ {
|
||||
for i := range totalBytes {
|
||||
for !r.Put([]byte{byte(i)}) {
|
||||
}
|
||||
}
|
||||
@@ -494,10 +494,7 @@ func FuzzRing512(f *testing.F) {
|
||||
|
||||
switch op {
|
||||
case 0: // Put
|
||||
size := int(arg)
|
||||
if size > 512 {
|
||||
size = 512
|
||||
}
|
||||
size := min(int(arg), 512)
|
||||
data := make([]byte, size)
|
||||
for j := range data {
|
||||
data[j] = byte(j)
|
||||
|
||||
@@ -15,21 +15,21 @@ func TestCondSignal(t *testing.T) {
|
||||
cond.L.Lock()
|
||||
|
||||
// Start a goroutine to signal us once we wait.
|
||||
var signaled uint32
|
||||
var signaled atomic.Uint32
|
||||
go func() {
|
||||
// Wait for the test goroutine to wait.
|
||||
cond.L.Lock()
|
||||
defer cond.L.Unlock()
|
||||
|
||||
// Send a signal to the test goroutine.
|
||||
atomic.StoreUint32(&signaled, 1)
|
||||
signaled.Store(1)
|
||||
cond.Signal()
|
||||
}()
|
||||
|
||||
// Wait for a signal.
|
||||
// This will unlock the mutex, and allow the spawned goroutine to run.
|
||||
cond.Wait()
|
||||
if atomic.LoadUint32(&signaled) == 0 {
|
||||
if signaled.Load() == 0 {
|
||||
t.Error("wait returned before a signal was sent")
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ func TestCondBroadcast(t *testing.T) {
|
||||
// Start goroutines to wait for the broadcast.
|
||||
var wg sync.WaitGroup
|
||||
const n = 5
|
||||
for i := 0; i < n; i++ {
|
||||
for range n {
|
||||
wg.Add(1)
|
||||
mu.RLock()
|
||||
go func() {
|
||||
|
||||
+22
-22
@@ -14,7 +14,7 @@ type mutex interface {
|
||||
}
|
||||
|
||||
func HammerMutex(m mutex, loops int, cdone chan bool) {
|
||||
for i := 0; i < loops; i++ {
|
||||
for i := range loops {
|
||||
if i%3 == 0 {
|
||||
if m.TryLock() {
|
||||
m.Unlock()
|
||||
@@ -41,10 +41,10 @@ func TestMutex(t *testing.T) {
|
||||
m.Unlock()
|
||||
|
||||
c := make(chan bool)
|
||||
for i := 0; i < 10; i++ {
|
||||
for range 10 {
|
||||
go HammerMutex(m, 1000, c)
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
for range 10 {
|
||||
<-c
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func TestMutexUncontended(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
|
||||
// Lock and unlock the mutex a few times.
|
||||
for i := 0; i < 3; i++ {
|
||||
for range 3 {
|
||||
mu.Lock()
|
||||
mu.Unlock()
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func TestMutexConcurrent(t *testing.T) {
|
||||
var fail atomic.Uint32
|
||||
|
||||
const n = 10
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
j := i
|
||||
go func() {
|
||||
// Delay a bit.
|
||||
@@ -129,12 +129,12 @@ func TestRWMutexUncontended(t *testing.T) {
|
||||
|
||||
// Acquire several read locks.
|
||||
const n = 5
|
||||
for i := 0; i < n; i++ {
|
||||
for range n {
|
||||
mu.RLock()
|
||||
}
|
||||
|
||||
// Release all of the read locks.
|
||||
for i := 0; i < n; i++ {
|
||||
for range n {
|
||||
mu.RUnlock()
|
||||
}
|
||||
|
||||
@@ -150,31 +150,31 @@ func TestRWMutexWriteToRead(t *testing.T) {
|
||||
mu.Lock()
|
||||
|
||||
const n = 3
|
||||
var readAcquires uint32
|
||||
var completed uint32
|
||||
var unlocked uint32
|
||||
var readAcquires atomic.Uint32
|
||||
var completed atomic.Uint32
|
||||
var unlocked atomic.Uint32
|
||||
var bad uint32
|
||||
for i := 0; i < n; i++ {
|
||||
for range n {
|
||||
go func() {
|
||||
// Acquire a read lock.
|
||||
mu.RLock()
|
||||
|
||||
// Verify that the write lock is supposed to be released by now.
|
||||
if atomic.LoadUint32(&unlocked) == 0 {
|
||||
if unlocked.Load() == 0 {
|
||||
// The write lock is still being held.
|
||||
atomic.AddUint32(&bad, 1)
|
||||
}
|
||||
|
||||
// Add ourselves to the read lock counter.
|
||||
atomic.AddUint32(&readAcquires, 1)
|
||||
readAcquires.Add(1)
|
||||
|
||||
// Wait for everything to hold the read lock simultaneously.
|
||||
for atomic.LoadUint32(&readAcquires) < n {
|
||||
for readAcquires.Load() < n {
|
||||
runtime.Gosched()
|
||||
}
|
||||
|
||||
// Notify of completion.
|
||||
atomic.AddUint32(&completed, 1)
|
||||
completed.Add(1)
|
||||
|
||||
// Release the read lock.
|
||||
mu.RUnlock()
|
||||
@@ -182,16 +182,16 @@ func TestRWMutexWriteToRead(t *testing.T) {
|
||||
}
|
||||
|
||||
// Wait a bit for the goroutines to block.
|
||||
for i := 0; i < 3*n; i++ {
|
||||
for range 3 * n {
|
||||
runtime.Gosched()
|
||||
}
|
||||
|
||||
// Release the write lock so that the goroutines acquire read locks.
|
||||
atomic.StoreUint32(&unlocked, 1)
|
||||
unlocked.Store(1)
|
||||
mu.Unlock()
|
||||
|
||||
// Wait for everything to complete.
|
||||
for atomic.LoadUint32(&completed) < n {
|
||||
for completed.Load() < n {
|
||||
runtime.Gosched()
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ func TestRWMutexReadToWrite(t *testing.T) {
|
||||
const n = 3
|
||||
var mu sync.RWMutex
|
||||
var readers uint32
|
||||
for i := 0; i < n; i++ {
|
||||
for range n {
|
||||
mu.RLock()
|
||||
readers++
|
||||
}
|
||||
@@ -230,7 +230,7 @@ func TestRWMutexReadToWrite(t *testing.T) {
|
||||
}()
|
||||
|
||||
// Release the read locks.
|
||||
for i := 0; i < n; i++ {
|
||||
for range n {
|
||||
runtime.Gosched()
|
||||
atomic.AddUint32(&readers, ^uint32(0))
|
||||
mu.RUnlock()
|
||||
@@ -261,10 +261,10 @@ func TestRWMutex(t *testing.T) {
|
||||
m.Unlock()
|
||||
|
||||
c := make(chan bool)
|
||||
for i := 0; i < 10; i++ {
|
||||
for range 10 {
|
||||
go HammerMutex(m, 1000, c)
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
for range 10 {
|
||||
<-c
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ type testItem struct {
|
||||
|
||||
func TestPool(t *testing.T) {
|
||||
p := sync.Pool{
|
||||
New: func() interface{} {
|
||||
New: func() any {
|
||||
return &testItem{}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestWaitGroup(t *testing.T) {
|
||||
const n = 5
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(n)
|
||||
for i := 0; i < n; i++ {
|
||||
for range n {
|
||||
go wg.Done()
|
||||
}
|
||||
|
||||
|
||||
@@ -170,20 +170,6 @@ func (b *B) runN(n int) {
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func min(x, y int64) int64 {
|
||||
if x > y {
|
||||
return y
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func max(x, y int64) int64 {
|
||||
if x < y {
|
||||
return y
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// run1 runs the first iteration of benchFunc. It reports whether more
|
||||
// iterations of this benchmarks should be run.
|
||||
func (b *B) run1() bool {
|
||||
|
||||
+20
-20
@@ -53,7 +53,7 @@ type corpusEntry = struct {
|
||||
Parent string
|
||||
Path string
|
||||
Data []byte
|
||||
Values []interface{}
|
||||
Values []any
|
||||
Generation int
|
||||
IsSeed bool
|
||||
}
|
||||
@@ -61,8 +61,8 @@ type corpusEntry = struct {
|
||||
// Add will add the arguments to the seed corpus for the fuzz test. This will be
|
||||
// a no-op if called after or within the fuzz target, and args must match the
|
||||
// arguments for the fuzz target.
|
||||
func (f *F) Add(args ...interface{}) {
|
||||
var values []interface{}
|
||||
func (f *F) Add(args ...any) {
|
||||
var values []any
|
||||
for i := range args {
|
||||
if t := reflect.TypeOf(args[i]); !supportedTypes[t] {
|
||||
panic(fmt.Sprintf("testing: unsupported type to Add %v", t))
|
||||
@@ -74,23 +74,23 @@ func (f *F) Add(args ...interface{}) {
|
||||
|
||||
// supportedTypes represents all of the supported types which can be fuzzed.
|
||||
var supportedTypes = map[reflect.Type]bool{
|
||||
reflect.TypeOf(([]byte)("")): true,
|
||||
reflect.TypeOf((string)("")): true,
|
||||
reflect.TypeFor[[]byte](): true,
|
||||
reflect.TypeFor[string](): true,
|
||||
reflect.TypeOf((bool)(false)): true,
|
||||
reflect.TypeOf((byte)(0)): true,
|
||||
reflect.TypeOf((rune)(0)): true,
|
||||
reflect.TypeOf((float32)(0)): true,
|
||||
reflect.TypeOf((float64)(0)): true,
|
||||
reflect.TypeOf((int)(0)): true,
|
||||
reflect.TypeOf((int8)(0)): true,
|
||||
reflect.TypeOf((int16)(0)): true,
|
||||
reflect.TypeOf((int32)(0)): true,
|
||||
reflect.TypeOf((int64)(0)): true,
|
||||
reflect.TypeOf((uint)(0)): true,
|
||||
reflect.TypeOf((uint8)(0)): true,
|
||||
reflect.TypeOf((uint16)(0)): true,
|
||||
reflect.TypeOf((uint32)(0)): true,
|
||||
reflect.TypeOf((uint64)(0)): true,
|
||||
reflect.TypeFor[byte](): true,
|
||||
reflect.TypeFor[rune](): true,
|
||||
reflect.TypeFor[float32](): true,
|
||||
reflect.TypeFor[float64](): true,
|
||||
reflect.TypeFor[int](): true,
|
||||
reflect.TypeFor[int8](): true,
|
||||
reflect.TypeFor[int16](): true,
|
||||
reflect.TypeFor[int32](): true,
|
||||
reflect.TypeFor[int64](): true,
|
||||
reflect.TypeFor[uint](): true,
|
||||
reflect.TypeFor[uint8](): true,
|
||||
reflect.TypeFor[uint16](): true,
|
||||
reflect.TypeFor[uint32](): true,
|
||||
reflect.TypeFor[uint64](): true,
|
||||
}
|
||||
|
||||
// Fuzz runs the fuzz function, ff, for fuzz testing. If ff fails for a set of
|
||||
@@ -119,7 +119,7 @@ var supportedTypes = map[reflect.Type]bool{
|
||||
// When fuzzing, F.Fuzz does not return until a problem is found, time runs out
|
||||
// (set with -fuzztime), or the test process is interrupted by a signal. F.Fuzz
|
||||
// should be called exactly once, unless F.Skip or F.Fail is called beforehand.
|
||||
func (f *F) Fuzz(ff interface{}) {
|
||||
func (f *F) Fuzz(ff any) {
|
||||
f.failed = true
|
||||
f.result.N = 0
|
||||
f.result.T = 0
|
||||
|
||||
+19
-19
@@ -137,7 +137,7 @@ func Testing() bool {
|
||||
|
||||
// flushToParent writes c.output to the parent after first writing the header
|
||||
// with the given format and arguments.
|
||||
func (c *common) flushToParent(testName, format string, args ...interface{}) {
|
||||
func (c *common) flushToParent(testName, format string, args ...any) {
|
||||
if c.parent == nil {
|
||||
// The fake top-level test doesn't want a FAIL or PASS banner.
|
||||
// Not quite sure how this works upstream.
|
||||
@@ -157,21 +157,21 @@ func fmtDuration(d time.Duration) string {
|
||||
type TB interface {
|
||||
Cleanup(func())
|
||||
Context() context.Context
|
||||
Error(args ...interface{})
|
||||
Errorf(format string, args ...interface{})
|
||||
Error(args ...any)
|
||||
Errorf(format string, args ...any)
|
||||
Fail()
|
||||
FailNow()
|
||||
Failed() bool
|
||||
Fatal(args ...interface{})
|
||||
Fatalf(format string, args ...interface{})
|
||||
Fatal(args ...any)
|
||||
Fatalf(format string, args ...any)
|
||||
Helper()
|
||||
Log(args ...interface{})
|
||||
Logf(format string, args ...interface{})
|
||||
Log(args ...any)
|
||||
Logf(format string, args ...any)
|
||||
Name() string
|
||||
Setenv(key, value string)
|
||||
Skip(args ...interface{})
|
||||
Skip(args ...any)
|
||||
SkipNow()
|
||||
Skipf(format string, args ...interface{})
|
||||
Skipf(format string, args ...any)
|
||||
Skipped() bool
|
||||
TempDir() string
|
||||
}
|
||||
@@ -238,47 +238,47 @@ func (c *common) log(s string) {
|
||||
// and records the text in the error log. For tests, the text will be printed only if
|
||||
// the test fails or the -test.v flag is set. For benchmarks, the text is always
|
||||
// printed to avoid having performance depend on the value of the -test.v flag.
|
||||
func (c *common) Log(args ...interface{}) { c.log(fmt.Sprintln(args...)) }
|
||||
func (c *common) Log(args ...any) { c.log(fmt.Sprintln(args...)) }
|
||||
|
||||
// Logf formats its arguments according to the format, analogous to Printf, and
|
||||
// records the text in the error log. A final newline is added if not provided. For
|
||||
// tests, the text will be printed only if the test fails or the -test.v flag is
|
||||
// set. For benchmarks, the text is always printed to avoid having performance
|
||||
// depend on the value of the -test.v flag.
|
||||
func (c *common) Logf(format string, args ...interface{}) { c.log(fmt.Sprintf(format, args...)) }
|
||||
func (c *common) Logf(format string, args ...any) { c.log(fmt.Sprintf(format, args...)) }
|
||||
|
||||
// Error is equivalent to Log followed by Fail.
|
||||
func (c *common) Error(args ...interface{}) {
|
||||
func (c *common) Error(args ...any) {
|
||||
c.log(fmt.Sprintln(args...))
|
||||
c.Fail()
|
||||
}
|
||||
|
||||
// Errorf is equivalent to Logf followed by Fail.
|
||||
func (c *common) Errorf(format string, args ...interface{}) {
|
||||
func (c *common) Errorf(format string, args ...any) {
|
||||
c.log(fmt.Sprintf(format, args...))
|
||||
c.Fail()
|
||||
}
|
||||
|
||||
// Fatal is equivalent to Log followed by FailNow.
|
||||
func (c *common) Fatal(args ...interface{}) {
|
||||
func (c *common) Fatal(args ...any) {
|
||||
c.log(fmt.Sprintln(args...))
|
||||
c.FailNow()
|
||||
}
|
||||
|
||||
// Fatalf is equivalent to Logf followed by FailNow.
|
||||
func (c *common) Fatalf(format string, args ...interface{}) {
|
||||
func (c *common) Fatalf(format string, args ...any) {
|
||||
c.log(fmt.Sprintf(format, args...))
|
||||
c.FailNow()
|
||||
}
|
||||
|
||||
// Skip is equivalent to Log followed by SkipNow.
|
||||
func (c *common) Skip(args ...interface{}) {
|
||||
func (c *common) Skip(args ...any) {
|
||||
c.log(fmt.Sprintln(args...))
|
||||
c.SkipNow()
|
||||
}
|
||||
|
||||
// Skipf is equivalent to Logf followed by SkipNow.
|
||||
func (c *common) Skipf(format string, args ...interface{}) {
|
||||
func (c *common) Skipf(format string, args ...any) {
|
||||
c.log(fmt.Sprintf(format, args...))
|
||||
c.SkipNow()
|
||||
}
|
||||
@@ -672,7 +672,7 @@ func (t *T) report() {
|
||||
// Not implemented.
|
||||
func AllocsPerRun(runs int, f func()) (avg float64) {
|
||||
f()
|
||||
for i := 0; i < runs; i++ {
|
||||
for range runs {
|
||||
f()
|
||||
}
|
||||
return 0
|
||||
@@ -688,7 +688,7 @@ type InternalExample struct {
|
||||
// MainStart is meant for use by tests generated by 'go test'.
|
||||
// It is not meant to be called directly and is not subject to the Go 1 compatibility document.
|
||||
// It may change signature from release to release.
|
||||
func MainStart(deps interface{}, tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) *M {
|
||||
func MainStart(deps any, tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) *M {
|
||||
Init()
|
||||
return &M{
|
||||
Tests: tests,
|
||||
|
||||
@@ -71,4 +71,4 @@ func Make[T comparable](value T) Handle[T] {
|
||||
}
|
||||
|
||||
//go:linkname decomposeInterface runtime.decomposeInterface
|
||||
func decomposeInterface(i interface{}) (unsafe.Pointer, unsafe.Pointer)
|
||||
func decomposeInterface(i any) (unsafe.Pointer, unsafe.Pointer)
|
||||
|
||||
Reference in New Issue
Block a user