all: run go fix -slicescontains

This commit is contained in:
Aliaksandr Valialkin
2026-02-18 15:14:14 +01:00
parent 4d4c1ff72e
commit 32eac31416
14 changed files with 27 additions and 90 deletions

View File

@@ -2,6 +2,7 @@ package config
import (
"fmt"
"slices"
"strings"
"github.com/VictoriaMetrics/VictoriaLogs/lib/logstorage"
@@ -80,12 +81,8 @@ func (t *Type) ValidateExpr(expr string) error {
if err != nil {
return fmt.Errorf("cannot obtain labels from LogsQL expr: %q, err: %w", expr, err)
}
for i := range labels {
// VictoriaLogs inserts `_time` field as a label in result when query with `stats by (_time:step)`,
// making the result meaningless and may lead to cardinality issues.
if labels[i] == "_time" {
return fmt.Errorf("bad LogsQL expr: %q, err: cannot contain time buckets stats pipe `stats by (_time:step)`", expr)
}
if slices.Contains(labels, "_time") {
return fmt.Errorf("bad LogsQL expr: %q, err: cannot contain time buckets stats pipe `stats by (_time:step)`", expr)
}
default:
return fmt.Errorf("unknown datasource type=%q", t.Name)

View File

@@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"math"
"slices"
"sort"
"sync"
"sync/atomic"
@@ -832,12 +833,7 @@ func GraphiteTags(qt *querytracer.Tracer, filter string, limit int, deadline sea
}
func hasString(a []string, s string) bool {
for _, x := range a {
if x == s {
return true
}
}
return false
return slices.Contains(a, s)
}
// LabelValues returns label values matching the given labelName and sq until the given deadline.

View File

@@ -6,6 +6,7 @@ import (
"math"
"net/http"
"runtime"
"slices"
"strconv"
"strings"
"sync"
@@ -1004,14 +1005,7 @@ func removeEmptyValuesAndTimeseries(tss []netstorage.Result) []netstorage.Result
dst := tss[:0]
for i := range tss {
ts := &tss[i]
hasNaNs := false
for _, v := range ts.Values {
if math.IsNaN(v) {
hasNaNs = true
break
}
}
if !hasNaNs {
if !slices.ContainsFunc(ts.Values, math.IsNaN) {
// Fast path: nothing to remove.
if len(ts.Values) > 0 {
dst = append(dst, *ts)

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"math"
"regexp"
"slices"
"sort"
"strings"
"sync"
@@ -1935,14 +1936,7 @@ func dropStaleNaNs(funcName string, values []float64, timestamps []int64) ([]flo
return values, timestamps
}
// Remove Prometheus staleness marks, so non-default rollup functions don't hit NaN values.
hasStaleSamples := false
for _, v := range values {
if decimal.IsStaleNaN(v) {
hasStaleSamples = true
break
}
}
if !hasStaleSamples {
if !slices.ContainsFunc(values, decimal.IsStaleNaN) {
// Fast path: values have no Prometheus staleness marks.
return values, timestamps
}

View File

@@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"slices"
"strings"
)
@@ -19,12 +20,7 @@ func Usage(s string) {
}
func hasHelpFlag(args []string) bool {
for _, arg := range args {
if isHelpArg(arg) {
return true
}
}
return false
return slices.ContainsFunc(args, isHelpArg)
}
func isHelpArg(arg string) bool {

View File

@@ -7,6 +7,7 @@ import (
"flag"
"fmt"
"net/url"
"slices"
"strings"
"sync"
"time"
@@ -327,14 +328,7 @@ func shouldCollectServiceByTags(filterTags, tags []string) bool {
return true
}
for _, filterTag := range filterTags {
hasTag := false
for _, tag := range tags {
if tag == filterTag {
hasTag = true
break
}
}
if !hasTag {
if !slices.Contains(tags, filterTag) {
return false
}
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"slices"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutil"
@@ -116,12 +117,7 @@ func (eps *Endpoints) getTargetLabels(gw *groupWatcher) []*promutil.Labels {
// Append labels for skipped ports on seen pods.
portSeen := func(port int, ports []int) bool {
for _, p := range ports {
if p == port {
return true
}
}
return false
return slices.Contains(ports, port)
}
appendPodMetadata := func(p *Pod, c *Container, seen []int, isInit bool) {
for _, cp := range c.Ports {

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"slices"
"strconv"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutil"
@@ -66,12 +67,7 @@ func (eps *EndpointSlice) getTargetLabels(gw *groupWatcher) []*promutil.Labels {
// Append labels for skipped ports on seen pods.
portSeen := func(port int, ports []int) bool {
for _, p := range ports {
if p == port {
return true
}
}
return false
return slices.Contains(ports, port)
}
appendPodMetadata := func(p *Pod, c *Container, seen []int, isInit bool) {
for _, cp := range c.Ports {

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/url"
"slices"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
@@ -14,12 +15,7 @@ import (
var validURLSchemes = []string{"http", "https", "socks5", "tls+socks5"}
func isURLSchemeValid(scheme string) bool {
for _, vs := range validURLSchemes {
if scheme == vs {
return true
}
}
return false
return slices.Contains(validURLSchemes, scheme)
}
// URL implements YAML.Marshaler and yaml.Unmarshaler interfaces for url.URL.

View File

@@ -3,6 +3,7 @@ package regexutil
import (
"regexp"
"regexp/syntax"
"slices"
"strings"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
@@ -118,12 +119,7 @@ func (pr *PromRegex) MatchString(s string) bool {
if len(pr.orValues) > 0 {
// Fast path - pr contains only alternate strings such as 'foo|bar|baz'
for _, v := range pr.orValues {
if s == v {
return true
}
}
return false
return slices.Contains(pr.orValues, s)
}
// Fall back to slow path by matching the original regexp.

View File

@@ -922,12 +922,7 @@ func testIndexDBCheckTSIDByName(db *indexDB, mns []MetricName, tsids []TSID, tim
}
func testHasTSID(tsids []TSID, tsid *TSID) bool {
for i := range tsids {
if tsids[i] == *tsid {
return true
}
}
return false
return slices.Contains(tsids, *tsid)
}
func TestGetRegexpForGraphiteNodeQuery(t *testing.T) {

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"runtime"
"slices"
"sort"
"strings"
"sync"
@@ -373,12 +374,7 @@ func (mn *MetricName) setAllTags(prefix string, skipTags []string, src *MetricNa
}
func containsString(a []string, s string) bool {
for _, x := range a {
if x == s {
return true
}
}
return false
return slices.Contains(a, s)
}
func hasTag(tags []string, key []byte) bool {

View File

@@ -724,12 +724,7 @@ func checkLabelNames(lns []string, lnsExpected map[string]bool) error {
return fmt.Errorf("unexpected number of label names found; got %d; want at least %d; lns=%q, lnsExpected=%v", len(lns), len(lnsExpected), lns, lnsExpected)
}
hasItem := func(s string, lns []string) bool {
for _, labelName := range lns {
if s == labelName {
return true
}
}
return false
return slices.Contains(lns, s)
}
for labelName := range lnsExpected {
if !hasItem(labelName, lns) {

View File

@@ -2,6 +2,7 @@ package uint64set
import (
"math/bits"
"slices"
"sort"
"sync"
"sync/atomic"
@@ -891,12 +892,7 @@ func (b *bucket16) has(x uint16) bool {
}
func (b *bucket16) hasInSmallPool(x uint16) bool {
for _, v := range b.smallPool[:b.smallPoolLen] {
if v == x {
return true
}
}
return false
return slices.Contains(b.smallPool[:b.smallPoolLen], x)
}
func (b *bucket16) del(x uint16) bool {