Skip to content

Commit 1d0412c

Browse files
committed
all: 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 ./... ``` Signed-off-by: Sebastien Binet <binet@cern.ch>
1 parent 9515ee8 commit 1d0412c

28 files changed

+72
-69
lines changed

align.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func Align(plots [][]*Plot, t draw.Tiles, dc draw.Canvas) [][]draw.Canvas {
2525
}
2626

2727
// Create the initial tiles.
28-
for j := 0; j < t.Rows; j++ {
28+
for j := range t.Rows {
2929
if len(plots[j]) != t.Cols {
3030
panic(fmt.Errorf("plot: plots row %d columns (%d) != tiles columns (%d)", j, len(plots[j]), t.Rows))
3131
}
3232

3333
o[j] = make([]draw.Canvas, len(plots[j]))
34-
for i := 0; i < t.Cols; i++ {
34+
for i := range t.Cols {
3535
o[j][i] = t.At(dc, i, j)
3636
}
3737
}

align_example_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
func ExampleAlign() {
1919
const rows, cols = 4, 3
2020
plots := make([][]*plot.Plot, rows)
21-
for j := 0; j < rows; j++ {
21+
for j := range rows {
2222
plots[j] = make([]*plot.Plot, cols)
23-
for i := 0; i < cols; i++ {
23+
for i := range cols {
2424
if i == 0 && j == 2 {
2525
// This shows what happens when there are nil plots.
2626
continue
@@ -70,8 +70,8 @@ func ExampleAlign() {
7070
}
7171

7272
canvases := plot.Align(plots, t, dc)
73-
for j := 0; j < rows; j++ {
74-
for i := 0; i < cols; i++ {
73+
for j := range rows {
74+
for i := range cols {
7575
if plots[j][i] != nil {
7676
plots[j][i].Draw(canvases[j][i])
7777
}

palette/brewer/brewer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func GetPalette(typ PaletteType, name string, colors int) (palette.Palette, erro
125125
)
126126
switch typ {
127127
case TypeAny:
128-
var pt interface{}
128+
var pt any
129129
pt, nameOk = all[name]
130130
if !nameOk {
131131
break

palette/brewer/palettes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4752,7 +4752,7 @@ var (
47524752
"YlOrBr": YlOrBr,
47534753
"YlOrRd": YlOrRd,
47544754
}
4755-
all = map[string]interface{}{
4755+
all = map[string]any{
47564756
"BrBG": BrBG,
47574757
"PiYG": PiYG,
47584758
"PRGn": PRGn,

palette/moreland/example_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func Example() {
5353
YOffset: -50,
5454
Data: mat.NewDense(100, 100, nil),
5555
}
56-
for i := 0; i < 100; i++ {
57-
for j := 0; j < 100; j++ {
56+
for i := range 100 {
57+
for j := range 100 {
5858
x := float64(i-50) / 10
5959
y := float64(j-50) / 10
6060
v := math.Sin(x*x+y*y) / (x*x + y*y)

palette/moreland/luminance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (l luminance) Palette(n int) palette.Palette {
178178
delta := (l.max - l.min) / float64(n-1)
179179
var v float64
180180
c := make([]color.Color, n)
181-
for i := 0; i < n; i++ {
181+
for i := range n {
182182
v = l.min + float64(delta*float64(i))
183183
var err error
184184
c[i], err = l.At(v)

plotter/barchart_example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func ExampleBarChart_positiveNegative() {
165165
data1 := make(plotter.Values, n)
166166
data2 := make(plotter.Values, n)
167167
net := make(plotter.XYs, n) // net = data1 + data2
168-
for i := 0; i < n; i++ {
168+
for i := range n {
169169
data1[i] = rnd.Float64()*2 - 1
170170
data2[i] = rnd.Float64()*2 - 1
171171
net[i].X = data1[i] + data2[i]

plotter/boxplot_example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func ExampleBoxPlot() {
2424
uniform := make(plotter.ValueLabels, n)
2525
normal := make(plotter.ValueLabels, n)
2626
expon := make(plotter.ValueLabels, n)
27-
for i := 0; i < n; i++ {
27+
for i := range n {
2828
uniform[i].Value = rnd.Float64()
2929
uniform[i].Label = fmt.Sprintf("%4.4f", uniform[i].Value)
3030
normal[i].Value = rnd.NormFloat64()

plotter/colorbar.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (l *ColorBar) Plot(c draw.Canvas, p *plot.Plot) {
6262
Min: image.Point{X: 0, Y: 0},
6363
Max: image.Point{X: 1, Y: colors},
6464
})
65-
for i := 0; i < colors; i++ {
65+
for i := range colors {
6666
color, err := l.ColorMap.At(l.ColorMap.Min() + delta*float64(i))
6767
if err != nil {
6868
panic(err)
@@ -75,7 +75,7 @@ func (l *ColorBar) Plot(c draw.Canvas, p *plot.Plot) {
7575
Min: image.Point{X: 0, Y: 0},
7676
Max: image.Point{X: colors, Y: 1},
7777
})
78-
for i := 0; i < colors; i++ {
78+
for i := range colors {
7979
color, err := l.ColorMap.At(l.ColorMap.Min() + delta*float64(i))
8080
if err != nil {
8181
panic(err)

plotter/conrec.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func conrec(g GridXYZ, heights []float64, fn conrecLine) {
7676
)
7777

7878
c, r := g.Dims()
79-
for i := 0; i < c-1; i++ {
80-
for j := 0; j < r-1; j++ {
79+
for i := range c - 1 {
80+
for j := range r - 1 {
8181
dmin := math.Min(
8282
math.Min(g.Z(i, j), g.Z(i, j+1)),
8383
math.Min(g.Z(i+1, j), g.Z(i+1, j+1)),
@@ -92,7 +92,7 @@ func conrec(g GridXYZ, heights []float64, fn conrecLine) {
9292
continue
9393
}
9494

95-
for k := 0; k < len(heights); k++ {
95+
for k := range heights {
9696
if heights[k] < dmin || dmax < heights[k] {
9797
continue
9898
}

plotter/contour.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package plotter
77
import (
88
"image/color"
99
"math"
10+
"slices"
1011
"sort"
1112

1213
"gonum.org/v1/plot"
@@ -64,8 +65,8 @@ func NewContour(g GridXYZ, levels []float64, p palette.Palette) *Contour {
6465
default:
6566
min, max = math.Inf(1), math.Inf(-1)
6667
c, r := g.Dims()
67-
for i := 0; i < c; i++ {
68-
for j := 0; j < r; j++ {
68+
for i := range c {
69+
for j := range r {
6970
v := g.Z(i, j)
7071
if math.IsNaN(v) {
7172
continue
@@ -98,8 +99,8 @@ var defaultQuantiles = []float64{0.01, 0.05, 0.25, 0.5, 0.75, 0.95, 0.99}
9899
func quantilesR7(g GridXYZ, p []float64) []float64 {
99100
c, r := g.Dims()
100101
data := make([]float64, 0, c*r)
101-
for i := 0; i < c; i++ {
102-
for j := 0; j < r; j++ {
102+
for i := range c {
103+
for j := range r {
103104
if v := g.Z(i, j); !math.IsNaN(v) {
104105
data = append(data, v)
105106
}
@@ -267,8 +268,8 @@ func (h *Contour) DataRange() (xmin, xmax, ymin, ymax float64) {
267268
func (h *Contour) GlyphBoxes(plt *plot.Plot) []plot.GlyphBox {
268269
c, r := h.GridXYZ.Dims()
269270
b := make([]plot.GlyphBox, 0, r*c)
270-
for i := 0; i < c; i++ {
271-
for j := 0; j < r; j++ {
271+
for i := range c {
272+
for j := range r {
272273
b = append(b, plot.GlyphBox{
273274
X: plt.X.Norm(h.GridXYZ.X(i)),
274275
Y: plt.Y.Norm(h.GridXYZ.Y(j)),
@@ -651,7 +652,7 @@ func (c *contour) exciseQuick(conts contourSet) {
651652
conts[&contour{
652653
z: c.z,
653654
backward: path{wp[i]},
654-
forward: append(path(nil), wp[i+1:j+1]...),
655+
forward: slices.Clone(wp[i+1 : j+1]),
655656
}] = struct{}{}
656657
wp = append(wp[:i], wp[j:]...)
657658
j = i + 1

plotter/contour_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"math"
1111
"reflect"
12+
"slices"
1213
"sort"
1314
"testing"
1415

@@ -457,8 +458,8 @@ func TestExciseLoops(t *testing.T) {
457458
for i, test := range loopTests {
458459
gotSet := make(contourSet)
459460
c := &contour{
460-
backward: append(path(nil), test.c.backward...),
461-
forward: append(path(nil), test.c.forward...),
461+
backward: slices.Clone(test.c.backward),
462+
forward: slices.Clone(test.c.forward),
462463
}
463464
gotSet[c] = struct{}{}
464465
c.exciseLoops(gotSet, quick)

plotter/field.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ type Field struct {
6666
func NewField(f FieldXY) *Field {
6767
max := math.Inf(-1)
6868
c, r := f.Dims()
69-
for i := 0; i < c; i++ {
70-
for j := 0; j < r; j++ {
69+
for i := range c {
70+
for j := range r {
7171
v := f.Vector(i, j)
7272
d := math.Hypot(v.X, v.Y)
7373
if math.IsNaN(d) {
@@ -93,7 +93,7 @@ func (f *Field) Plot(c draw.Canvas, plt *plot.Plot) {
9393
trX, trY := plt.Transforms(&c)
9494

9595
cols, rows := f.FieldXY.Dims()
96-
for i := 0; i < cols; i++ {
96+
for i := range cols {
9797
var right, left float64
9898
switch i {
9999
case 0:
@@ -111,7 +111,7 @@ func (f *Field) Plot(c draw.Canvas, plt *plot.Plot) {
111111
left = -(f.FieldXY.X(i) - f.FieldXY.X(i-1)) / 2
112112
}
113113

114-
for j := 0; j < rows; j++ {
114+
for j := range rows {
115115
var up, down float64
116116
switch j {
117117
case 0:
@@ -200,8 +200,8 @@ func (f *Field) DataRange() (xmin, xmax, ymin, ymax float64) {
200200
func (f *Field) GlyphBoxes(plt *plot.Plot) []plot.GlyphBox {
201201
c, r := f.FieldXY.Dims()
202202
b := make([]plot.GlyphBox, 0, r*c)
203-
for i := 0; i < c; i++ {
204-
for j := 0; j < r; j++ {
203+
for i := range c {
204+
for j := range r {
205205
b = append(b, plot.GlyphBox{
206206
X: plt.X.Norm(f.FieldXY.X(i)),
207207
Y: plt.Y.Norm(f.FieldXY.Y(j)),

plotter/heat.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func NewHeatMap(g GridXYZ, p palette.Palette) *HeatMap {
8181
default:
8282
min, max = math.Inf(1), math.Inf(-1)
8383
c, r := g.Dims()
84-
for i := 0; i < c; i++ {
85-
for j := 0; j < r; j++ {
84+
for i := range c {
85+
for j := range r {
8686
v := g.Z(i, j)
8787
if math.IsNaN(v) {
8888
continue
@@ -120,8 +120,8 @@ func (h *HeatMap) plotRasterized(c draw.Canvas, plt *plot.Plot) {
120120

121121
pal := h.Palette.Colors()
122122
ps := float64(len(pal)-1) / (h.Max - h.Min)
123-
for i := 0; i < cols; i++ {
124-
for j := 0; j < rows; j++ {
123+
for i := range cols {
124+
for j := range rows {
125125
var col color.Color
126126
switch v := h.GridXYZ.Z(i, j); {
127127
case v < h.Min:
@@ -161,7 +161,7 @@ func (h *HeatMap) plotVectorized(c draw.Canvas, plt *plot.Plot) {
161161

162162
var pa vg.Path
163163
cols, rows := h.GridXYZ.Dims()
164-
for i := 0; i < cols; i++ {
164+
for i := range cols {
165165
var right, left float64
166166
switch i {
167167
case 0:
@@ -179,7 +179,7 @@ func (h *HeatMap) plotVectorized(c draw.Canvas, plt *plot.Plot) {
179179
left = -(h.GridXYZ.X(i) - h.GridXYZ.X(i-1)) / 2
180180
}
181181

182-
for j := 0; j < rows; j++ {
182+
for j := range rows {
183183
var up, down float64
184184
switch j {
185185
case 0:
@@ -258,8 +258,8 @@ func (h *HeatMap) DataRange() (xmin, xmax, ymin, ymax float64) {
258258
func (h *HeatMap) GlyphBoxes(plt *plot.Plot) []plot.GlyphBox {
259259
c, r := h.GridXYZ.Dims()
260260
b := make([]plot.GlyphBox, 0, r*c)
261-
for i := 0; i < c; i++ {
262-
for j := 0; j < r; j++ {
261+
for i := range c {
262+
for j := range r {
263263
b = append(b, plot.GlyphBox{
264264
X: plt.X.Norm(h.GridXYZ.X(i)),
265265
Y: plt.Y.Norm(h.GridXYZ.Y(j)),

plotter/histogram.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func binPoints(xys XYer, n int) (bins []HistogramBin, width float64) {
186186
xmin, xmax := Range(XValues{xys})
187187
if n <= 0 {
188188
m := 0.0
189-
for i := 0; i < xys.Len(); i++ {
189+
for i := range xys.Len() {
190190
_, y := xys.XY(i)
191191
m += math.Max(y, 1.0)
192192
}
@@ -207,7 +207,7 @@ func binPoints(xys XYer, n int) (bins []HistogramBin, width float64) {
207207
bins[i].Max = xmin + float64(i+1)*w
208208
}
209209

210-
for i := 0; i < xys.Len(); i++ {
210+
for i := range xys.Len() {
211211
x, y := xys.XY(i)
212212
bin := int((x - xmin) / w)
213213
if x == xmax {

plotter/histogram_example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func ExampleHistogram() {
3131

3232
n := 10000
3333
vals := make(plotter.Values, n)
34-
for i := 0; i < n; i++ {
34+
for i := range n {
3535
vals[i] = rnd.NormFloat64()
3636
}
3737

plotter/image.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ func (img *Image) transformFor(p *plot.Plot) image.Image {
8686
}
8787
b := img.img.Bounds()
8888
o := image.NewNRGBA64(b)
89-
for c := 0; c < img.cols; c++ {
89+
for c := range img.cols {
9090
// Find the equivalent image column after applying axis transforms.
9191
cTrans := int(p.X.Norm(img.x(c)) * float64(img.cols))
9292
// Find the equivalent column of the previous image column after applying
9393
// axis transforms.
9494
cPrevTrans := int(p.X.Norm(img.x(maxInt(c-1, 0))) * float64(img.cols))
95-
for r := 0; r < img.rows; r++ {
95+
for r := range img.rows {
9696
// Find the equivalent image row after applying axis transforms.
9797
rTrans := int(p.Y.Norm(img.y(r)) * float64(img.rows))
9898
// Find the equivalent row of the previous image row after applying

plotter/plotter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type Valuer interface {
5555
func Range(vs Valuer) (min, max float64) {
5656
min = math.Inf(1)
5757
max = math.Inf(-1)
58-
for i := 0; i < vs.Len(); i++ {
58+
for i := range vs.Len() {
5959
v := vs.Value(i)
6060
min = math.Min(min, v)
6161
max = math.Max(max, v)
@@ -93,7 +93,7 @@ func CopyValues(vs Valuer) (Values, error) {
9393
return nil, ErrNoData
9494
}
9595
cpy := make(Values, vs.Len())
96-
for i := 0; i < vs.Len(); i++ {
96+
for i := range vs.Len() {
9797
cpy[i] = vs.Value(i)
9898
if err := CheckFloats(cpy[i]); err != nil {
9999
return nil, err

plotter/polygon_example_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ func ExamplePolygon_hexagons() {
121121
for i, xmin := range xstart {
122122
ymin := ystart[i]
123123
x := xmin
124-
for ix := 0; ix < nx; ix++ {
124+
for range nx {
125125
y := ymin
126-
for iy := 0; iy < ny; iy++ {
126+
for range ny {
127127
var poly *plotter.Polygon
128128
poly, err = plotter.NewPolygon(hex(x, y, r))
129129
if err != nil {

plotter/quartile_example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func ExampleQuartPlot() {
2222
uniform := make(plotter.Values, n)
2323
normal := make(plotter.Values, n)
2424
expon := make(plotter.Values, n)
25-
for i := 0; i < n; i++ {
25+
for i := range n {
2626
uniform[i] = rnd.Float64()
2727
normal[i] = rnd.NormFloat64()
2828
expon[i] = rnd.ExpFloat64()

plotter/rotation_example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func Example_rotation() {
2424
// Sin creates a sine curve.
2525
sin := func(n int, xmax float64) plotter.XYs {
2626
xy := make(plotter.XYs, n)
27-
for i := 0; i < n; i++ {
27+
for i := range n {
2828
xy[i].X = xmax / float64(n) * float64(i)
2929
xy[i].Y = math.Sin(xy[i].X) * 100
3030
}

0 commit comments

Comments
 (0)