build: modernize Go usage

This commit modernizes Go usage. This was done with:

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

Then files needed to be `go fmt`ed and a few comments needed to be
restored.

The modernizations include replacing

- if/else conditional assignment by a call to the built-in min or max functions added in go1.21
- sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } by a call to slices.Sort(s), added in go1.21
- interface{} by the 'any' type added in go1.18
- append([]T(nil), s...) by slices.Clone(s) or slices.Concat(s), added in go1.21
- loop around an m[k]=v map update by a call to one of the Collect, Copy, Clone, or Insert functions from the maps package, added in go1.21
- []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), added in go1.19
- append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), added in go1.21
- a 3-clause for i := 0; i < n; i++ {} loop by for i := range n {}, added in go1.22
This commit is contained in:
Nick Craig-Wood
2025-02-26 21:08:12 +00:00
parent 431386085f
commit 401cf81034
206 changed files with 755 additions and 953 deletions

View File

@@ -17,6 +17,7 @@ import (
"path"
"path/filepath"
"reflect"
"slices"
"sort"
"strconv"
"strings"
@@ -320,12 +321,7 @@ type Opt struct {
// returns true if x is found in ss
func stringsContains(x string, ss []string) bool {
for _, s := range ss {
if x == s {
return true
}
}
return false
return slices.Contains(ss, x)
}
// toUpperASCII returns a copy of the string s with all Unicode
@@ -484,7 +480,7 @@ func Run(t *testing.T, opt *Opt) {
}
v := reflect.ValueOf(ft).Elem()
vType := v.Type()
for i := 0; i < v.NumField(); i++ {
for i := range v.NumField() {
vName := vType.Field(i).Name
if stringsContains(vName, opt.UnimplementableFsMethods) {
continue
@@ -1068,7 +1064,7 @@ func Run(t *testing.T, opt *Opt) {
var err error
var objs []fs.Object
var dirs []fs.Directory
for i := 0; i < 2; i++ {
for range 2 {
dir, _ := path.Split(fileName)
dir = dir[:len(dir)-1]
objs, dirs, err = walk.GetAll(ctx, f, dir, true, -1)
@@ -2370,18 +2366,12 @@ func Run(t *testing.T, opt *Opt) {
setUploadCutoffer, _ := f.(SetUploadCutoffer)
minChunkSize := opt.ChunkedUpload.MinChunkSize
if minChunkSize < 100 {
minChunkSize = 100
}
minChunkSize := max(opt.ChunkedUpload.MinChunkSize, 100)
if opt.ChunkedUpload.CeilChunkSize != nil {
minChunkSize = opt.ChunkedUpload.CeilChunkSize(minChunkSize)
}
maxChunkSize := 2 * fs.Mebi
if maxChunkSize < 2*minChunkSize {
maxChunkSize = 2 * minChunkSize
}
maxChunkSize := max(2*fs.Mebi, 2*minChunkSize)
if opt.ChunkedUpload.MaxChunkSize > 0 && maxChunkSize > opt.ChunkedUpload.MaxChunkSize {
maxChunkSize = opt.ChunkedUpload.MaxChunkSize
}
@@ -2495,10 +2485,7 @@ func Run(t *testing.T, opt *Opt) {
t.Skipf("%T does not implement SetCopyCutoff", f)
}
minChunkSize := opt.ChunkedUpload.MinChunkSize
if minChunkSize < 100 {
minChunkSize = 100
}
minChunkSize := max(opt.ChunkedUpload.MinChunkSize, 100)
if opt.ChunkedUpload.CeilChunkSize != nil {
minChunkSize = opt.ChunkedUpload.CeilChunkSize(minChunkSize)
}

View File

@@ -57,7 +57,7 @@ type Run struct {
Precision time.Duration
cleanRemote func()
mkdir map[string]bool // whether the remote has been made yet for the fs name
Logf, Fatalf func(text string, args ...interface{})
Logf, Fatalf func(text string, args ...any)
}
// TestMain drives the tests

View File

@@ -30,7 +30,7 @@ var (
// Assume we are run somewhere within the rclone root
func findConfig() (string, error) {
dir := filepath.Join("fstest", "testserver", "init.d")
for i := 0; i < 5; i++ {
for range 5 {
fi, err := os.Stat(dir)
if err == nil && fi.IsDir() {
return filepath.Abs(dir)