Skip to content

Commit

Permalink
chore: use range loop (#18176)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jan 11, 2025
1 parent 4cce98c commit f469be6
Show file tree
Hide file tree
Showing 16 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion charger/bender.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (wb *BenderCC) totalEnergy() (float64, error) {
}

var total float64
for l := 0; l < 3; l++ {
for l := range 3 {
total += float64(binary.BigEndian.Uint32(b[4*l:4*(l+1)])) / 1e3
}

Expand Down
2 changes: 1 addition & 1 deletion charger/peblar.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (wb *Peblar) getPhaseValues(reg uint16, divider float64) (float64, float64,
}

var res [3]float64
for i := 0; i < int(wb.phases); i++ {
for i := range int(wb.phases) {
res[i] = float64(binary.BigEndian.Uint32(b[4*i:])) / divider
}

Expand Down
2 changes: 1 addition & 1 deletion charger/vaillant.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (v *Vaillant) print(chapter int, prefix string, zz ...any) {
fmt.Printf("%d.%d. %s\n", chapter, i+1, typ)

if rt.Kind() == reflect.Slice {
for j := 0; j < rv.Len(); j++ {
for j := range rv.Len() {
fmt.Printf("%d.%d.%d. %s %d\n", chapter, i+1, j+1, typ, j)
fmt.Printf("%+v\n", rv.Index(j))
}
Expand Down
2 changes: 1 addition & 1 deletion charger/versicharge.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (wb *Versicharge) CurrentPower() (float64, error) {
}

var sum float64
for i := 0; i < 3; i++ {
for i := range 3 {
sum += float64(binary.BigEndian.Uint16(b[2*i:]))
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/detect/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func workers(log *util.Logger, num int, tasks <-chan string, hits chan<- []tasks.Result) *sync.WaitGroup {
var wg sync.WaitGroup
for i := 0; i < num; i++ {
for range num {
wg.Add(1)
go func() {
workunit(log, tasks, hits)
Expand Down
2 changes: 1 addition & 1 deletion core/planner/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestSlotHasSuccessor(t *testing.T) {
plan[i], plan[j] = plan[j], plan[i]
})

for i := 0; i < len(plan); i++ {
for i := range plan {
if plan[i] != last {
require.True(t, SlotHasSuccessor(plan[i], plan))
}
Expand Down
2 changes: 1 addition & 1 deletion meter/openwb.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewOpenWBFromConfig(other map[string]interface{}) (api.Meter, error) {
}

var curr [3]func() (float64, error)
for i := 0; i < 3; i++ {
for i := range 3 {
current, err := to.FloatGetter(mq("%s/evu/%s%d", cc.Topic, openwb.CurrentTopic, i+1))
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion provider/timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (p *timeseriesProvider) StringGetter() (func() (string, error), error) {
return func() (string, error) {
res := make(api.Rates, 48)
ts := now.BeginningOfHour()
for i := 0; i < 48; i++ {
for i := range 48 {
res[i] = api.Rate{
Start: ts,
End: ts.Add(time.Hour),
Expand Down
4 changes: 2 additions & 2 deletions server/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m *Influx) writeComplexPoint(writer pointWriter, key string, val any, tags
typ := reflect.TypeOf(sv)
val := reflect.ValueOf(sv)

for i := 0; i < typ.NumField(); i++ {
for i := range typ.NumField() {
if f := typ.Field(i); f.IsExported() {
if val.Field(i).IsZero() && omitEmpty(f) {
continue
Expand Down Expand Up @@ -135,7 +135,7 @@ func (m *Influx) writeComplexPoint(writer pointWriter, key string, val any, tags
val := reflect.ValueOf(val)

// loop slice
for i := 0; i < val.Len(); i++ {
for i := range val.Len() {
tags["id"] = strconv.Itoa(i + 1)
writeStruct(val.Index(i).Interface())
}
Expand Down
2 changes: 1 addition & 1 deletion server/modbus/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestConcurrentRead(t *testing.T) {
conn, err := modbus.NewConnection(l.Addr().String(), "", "", 0, modbus.Tcp, uint8(id))
require.NoError(t, err)

for i := 0; i < 50; i++ {
for range 50 {
addr := uint16(rand.Int31n(200) + 1)
qty := uint16(rand.Int31n(32) + 1)

Expand Down
6 changes: 3 additions & 3 deletions server/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (m *MQTT) publishComplex(topic string, retained bool, payload interface{})
m.publishSingleValue(topic, retained, val.Len())

// loop slice
for i := 0; i < val.Len(); i++ {
for i := range val.Len() {
m.publishComplex(fmt.Sprintf("%s/%d", topic, i+1), retained, val.Index(i).Interface())
}

Expand All @@ -99,7 +99,7 @@ func (m *MQTT) publishComplex(topic string, retained bool, payload interface{})
typ := val.Type()

// loop struct
for i := 0; i < typ.NumField(); i++ {
for i := range typ.NumField() {
if f := typ.Field(i); f.IsExported() {
topic := fmt.Sprintf("%s/%s", topic, strings.ToLower(f.Name[:1])+f.Name[1:])

Expand Down Expand Up @@ -278,7 +278,7 @@ func (m *MQTT) Run(site site.API, in <-chan util.Param) {
topic = fmt.Sprintf("%s/vehicles", m.root)
m.publish(topic, true, len(site.Vehicles().Settings()))

for i := 0; i < 10; i++ {
for i := range 10 {
m.publish(fmt.Sprintf("%s/site/pv/%d", m.root, i), true, nil)
m.publish(fmt.Sprintf("%s/site/battery/%d", m.root, i), true, nil)
m.publish(fmt.Sprintf("%s/site/vehicles/%d", m.root, i), true, nil)
Expand Down
2 changes: 1 addition & 1 deletion server/socket_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func encodeSliceAsString(v any) (string, error) {
rv := reflect.ValueOf(v)
res := make([]string, rv.Len())

for i := 0; i < rv.Len(); i++ {
for i := range rv.Len() {
var err error
if res[i], err = encodeAsString(rv.Index(i).Interface()); err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion tariff/fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (t *Fixed) Rates() (api.Rates, error) {
var res api.Rates

start := now.With(t.clock.Now().Local()).BeginningOfDay()
for i := 0; i < 7; i++ {
for i := range 7 {
dow := fixed.Day((int(start.Weekday()) + i) % 7)

zones := t.zones.ForDay(dow)
Expand Down
2 changes: 1 addition & 1 deletion tariff/fixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestFixedSplitZones(t *testing.T) {
tf.clock = clock.NewMock()

var expect api.Rates
for i := 0; i < 7; i++ {
for i := range 7 {
dayStart := now.With(tf.clock.Now()).BeginningOfDay().AddDate(0, 0, i)

// 00:00-05:00 0.1
Expand Down
2 changes: 1 addition & 1 deletion util/logstash/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (l *logger) Size() int64 {
r := l.data
var size int64

for i := 0; i < r.Len(); i++ {
for range r.Len() {
if e, ok := r.Value.(element); ok {
size += int64(len(e))
}
Expand Down
2 changes: 1 addition & 1 deletion util/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func GetNextOccurrence(weekdays []int, timeStr string, tz string) (time.Time, er
}

// Check the next 7 days for a valid match
for i := 0; i < 7; i++ {
for range 7 {
weekday := int(target.Weekday())
if contains(weekdays, weekday) {
return target, nil
Expand Down

0 comments on commit f469be6

Please sign in to comment.