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

### Summary

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.

Signed-off-by: weifanglab <weifanglab@outlook.com>
This commit is contained in:
weifanglab
2026-07-07 11:12:38 +08:00
committed by GitHub
parent 863b35db7f
commit 7a4e1b6034
15 changed files with 41 additions and 141 deletions
+2 -8
View File
@@ -18,6 +18,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"
)
@@ -713,14 +714,7 @@ func TestPretokenWithNumbers(t *testing.T) {
t.Run("num=false filters single digits", func(t *testing.T) {
result := d.Pretoken("5", false, true)
// Single digit should be filtered when num=false
found := false
for _, r := range result {
if r == "5" {
found = true
break
}
}
if found {
if slices.Contains(result, "5") {
t.Error("Single digit should be filtered when num=false")
}
})
+2 -8
View File
@@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"sort"
"testing"
)
@@ -122,14 +123,7 @@ func TestSynsets(t *testing.T) {
names[i] = s.Name
}
for _, expectedName := range tt.checkNames {
found := false
for _, name := range names {
if name == expectedName {
found = true
break
}
}
if !found {
if !slices.Contains(names, expectedName) {
t.Errorf("Synsets(%q, %q) did not contain expected synset %q, got %v",
tt.lemma, tt.pos, expectedName, names)
}