refactor: use slices.Contains to simplify code (#10121)

There is a [new function](https://pkg.go.dev/slices@go1.21.0#Contains)
added in the go1.21 standard library, which can make the code more
concise and easy to read.
This commit is contained in:
pullmerge
2025-05-23 10:36:06 +00:00
committed by GitHub
parent 2532ac35cf
commit beda37f28b
3 changed files with 12 additions and 24 deletions
+3 -11
View File
@@ -8,6 +8,7 @@ package model
import ( import (
"path/filepath" "path/filepath"
"slices"
"testing" "testing"
"github.com/d4l3k/messagediff" "github.com/d4l3k/messagediff"
@@ -117,20 +118,11 @@ func unifySubsCases() []unifySubsCase {
return cases return cases
} }
func unifyExists(f string, tc unifySubsCase) bool {
for _, e := range tc.exists {
if f == e {
return true
}
}
return false
}
func TestUnifySubs(t *testing.T) { func TestUnifySubs(t *testing.T) {
cases := unifySubsCases() cases := unifySubsCases()
for i, tc := range cases { for i, tc := range cases {
exists := func(f string) bool { exists := func(f string) bool {
return unifyExists(f, tc) return slices.Contains(tc.exists, f)
} }
out := unifySubs(tc.in, exists) out := unifySubs(tc.in, exists)
if diff, equal := messagediff.PrettyDiff(tc.out, out); !equal { if diff, equal := messagediff.PrettyDiff(tc.out, out); !equal {
@@ -146,7 +138,7 @@ func BenchmarkUnifySubs(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for _, tc := range cases { for _, tc := range cases {
exists := func(f string) bool { exists := func(f string) bool {
return unifyExists(f, tc) return slices.Contains(tc.exists, f)
} }
unifySubs(tc.in, exists) unifySubs(tc.in, exists)
} }
+4 -5
View File
@@ -21,6 +21,7 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime" "runtime"
"slices"
"strings" "strings"
stdsync "sync" stdsync "sync"
"sync/atomic" "sync/atomic"
@@ -1804,11 +1805,9 @@ func (m *model) handleAutoAccepts(deviceID protocol.DeviceID, folder protocol.Fo
l.Infof("Failed to auto-accept folder %s from %s due to path conflict", folder.Description(), deviceID) l.Infof("Failed to auto-accept folder %s from %s due to path conflict", folder.Description(), deviceID)
return config.FolderConfiguration{}, false return config.FolderConfiguration{}, false
} else { } else {
for _, device := range cfg.DeviceIDs() { if slices.Contains(cfg.DeviceIDs(), deviceID) {
if device == deviceID { // Already shared nothing todo.
// Already shared nothing todo. return config.FolderConfiguration{}, false
return config.FolderConfiguration{}, false
}
} }
if cfg.Type == config.FolderTypeReceiveEncrypted { if cfg.Type == config.FolderTypeReceiveEncrypted {
if len(ccDeviceInfos.remote.EncryptionPasswordToken) == 0 && len(ccDeviceInfos.local.EncryptionPasswordToken) == 0 { if len(ccDeviceInfos.remote.EncryptionPasswordToken) == 0 && len(ccDeviceInfos.local.EncryptionPasswordToken) == 0 {
+5 -8
View File
@@ -17,6 +17,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime/pprof" "runtime/pprof"
"slices"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@@ -1253,10 +1254,8 @@ func TestAutoAcceptPausedWhenFolderConfigChanged(t *testing.T) {
} else if fcfg.Path != idOther { } else if fcfg.Path != idOther {
t.Error("folder path changed") t.Error("folder path changed")
} else { } else {
for _, dev := range fcfg.DeviceIDs() { if slices.Contains(fcfg.DeviceIDs(), device1) {
if dev == device1 { return
return
}
} }
t.Error("device missing") t.Error("device missing")
} }
@@ -1302,10 +1301,8 @@ func TestAutoAcceptPausedWhenFolderConfigNotChanged(t *testing.T) {
} else if fcfg.Path != idOther { } else if fcfg.Path != idOther {
t.Error("folder path changed") t.Error("folder path changed")
} else { } else {
for _, dev := range fcfg.DeviceIDs() { if slices.Contains(fcfg.DeviceIDs(), device1) {
if dev == device1 { return
return
}
} }
t.Error("device missing") t.Error("device missing")
} }