Skip to content

Commit a483f6a

Browse files
rsnedalan-strohm
andauthored
Fix identified linter errors preventing errorlint, exhaustive, inamedparam, and staticcheck from running and enable those checks. (#153)
Co-authored-by: Alan Strohm <[email protected]>
1 parent ce8b781 commit a483f6a

27 files changed

+81
-72
lines changed

.golangci.yml

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ linters:
44
# Commented entries here are not enabled by default, but that we should
55
# add once the relevant lines are fixed up.
66

7-
# - errorlint # found 2 cases to be fixed.
8-
# - exhaustive # found some cases that should be fixed.
7+
- errorlint
8+
- exhaustive
99
# - exhaustruct # found some structs which missed fields in initializing.
10-
# - inamedparam # fix missing named param
10+
- inamedparam
1111
# - makezero # fix the unallocated elements.
1212
# - misspell # fix spelling
1313
# - nlreturn # fix these missing blank line
@@ -68,8 +68,6 @@ linters:
6868
# Triggers on most tests for failing to call paralleltest.
6969
# We don't have a need to use this so keep it disabled.
7070
- paralleltest
71-
# Enable once outstanding lint bugs are fixed.
72-
- staticcheck
7371
# This triggers on every _test file saying they should be separate
7472
# parallel packages e.g. s2->s2_test package. We do not plan to ever
7573
# reshuffle the tests into a separate package.

r3/vector.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (v Vector) Ortho() Vector {
108108
ov.Z = 1
109109
case YAxis:
110110
ov.X = 1
111-
default:
111+
case ZAxis:
112112
ov.Y = 1
113113
}
114114
return v.Cross(ov).Normalize()

s2/cap.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func FullCap() Cap {
109109

110110
// IsValid reports whether the Cap is considered valid.
111111
func (c Cap) IsValid() bool {
112-
return c.center.Vector.IsUnit() && c.radius <= s1.StraightChordAngle
112+
return c.center.IsUnit() && c.radius <= s1.StraightChordAngle
113113
}
114114

115115
// IsEmpty reports whether the cap is empty, i.e. it contains no points.
@@ -391,7 +391,7 @@ func (c Cap) intersects(cell Cell, vertices [4]Point) bool {
391391
sin2Angle := c.radius.Sin2()
392392
for k := 0; k < 4; k++ {
393393
edge := cell.Edge(k).Vector
394-
dot := c.center.Vector.Dot(edge)
394+
dot := c.center.Dot(edge)
395395
if dot > 0 {
396396
// The center is in the interior half-space defined by the edge. We do not need
397397
// to consider these edges, since if the cap intersects this edge then it also

s2/cap_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func TestCapContainsCell(t *testing.T) {
487487
if got, want := covering.ContainsCell(rootCell), capFace == face; got != want {
488488
t.Errorf("Cap(%v).ContainsCell(%v) = %t; want = %t", covering, rootCell, got, want)
489489
}
490-
if got, want := covering.ContainsCell(edgeCell), center.Vector.Dot(edgeCell.id.Point().Vector) > 0.1; got != want {
490+
if got, want := covering.ContainsCell(edgeCell), center.Dot(edgeCell.id.Point().Vector) > 0.1; got != want {
491491
t.Errorf("Cap(%v).ContainsCell(%v) = %t; want = %t", covering, edgeCell, got, want)
492492
}
493493
if got, want := covering.ContainsCell(edgeCell), covering.IntersectsCell(edgeCell); got != want {
@@ -552,7 +552,7 @@ func TestCapIntersectsCell(t *testing.T) {
552552
if got, want := covering.IntersectsCell(edgeCell), covering.ContainsCell(edgeCell); got != want {
553553
t.Errorf("Cap(%v).IntersectsCell(%v) = %t; want = %t", covering, edgeCell, got, want)
554554
}
555-
if got, want := covering.IntersectsCell(cornerCell), center.Vector.Dot(cornerCell.id.Point().Vector) > 0; got != want {
555+
if got, want := covering.IntersectsCell(cornerCell), center.Dot(cornerCell.id.Point().Vector) > 0; got != want {
556556
t.Errorf("Cap(%v).IntersectsCell(%v) = %t; want = %t", covering, cornerCell, got, want)
557557
}
558558

@@ -561,7 +561,7 @@ func TestCapIntersectsCell(t *testing.T) {
561561
if got, want := bulging.IntersectsCell(rootCell), capFace != antiFace; got != want {
562562
t.Errorf("Cap(%v).IntersectsCell(%v) = %t; want = %t", bulging, rootCell, got, want)
563563
}
564-
if got, want := bulging.IntersectsCell(edgeCell), center.Vector.Dot(edgeCell.id.Point().Vector) > 0.1; got != want {
564+
if got, want := bulging.IntersectsCell(edgeCell), center.Dot(edgeCell.id.Point().Vector) > 0.1; got != want {
565565
t.Errorf("Cap(%v).IntersectsCell(%v) = %t; want = %t", bulging, edgeCell, got, want)
566566
}
567567
if bulging.IntersectsCell(cornerCell) {

s2/cell_index_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func verifyCellIndexRangeIterators(t *testing.T, desc string, index *CellIndex)
138138
if start != it2.StartID() {
139139
t.Errorf("%s: it2.StartID() = %v, want %v", desc, it2.StartID(), start)
140140
}
141-
if 0 != prevStart {
141+
if prevStart != 0 {
142142
t.Errorf("%s: prevStart = %v, want %v", desc, prevStart, 0)
143143
}
144144
}

s2/cellid.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func CellIDFromString(s string) CellID {
361361
}
362362
id := CellIDFromFace(face)
363363
for i := 2; i < len(s); i++ {
364-
var childPos byte = s[i] - '0'
364+
var childPos = s[i] - '0'
365365
// Bytes are non-negative.
366366
if childPos > 3 {
367367
return CellID(0)

s2/cellunion_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ func TestCellUnionFromUnionDiffIntersection(t *testing.T) {
818818
u := CellUnionFromIntersectionWithCellID(xcells, yid)
819819
for _, xid := range xcells {
820820
if xid.Contains(yid) {
821-
if !(len(u) == 1 && u[0] == yid) {
821+
if !(len(u) == 1 && u[0] == yid) { // nolint staticcheck - DeMorgan's doesn't work here.
822822
t.Errorf("CellUnionFromIntersectionWithCellID(%v, %v) = %v with len: %d, want len of 1.", xcells, yid, u, len(u))
823823
}
824824
} else if yid.Contains(xid) {

s2/crossing_edge_query.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,20 @@ func (c *CrossingEdgeQuery) getCellsForEdge(a, b Point) {
277277
// 3. edgeRoot does not intersect any index cells. In this case there
278278
// is nothing to do.
279279
relation := c.iter.LocateCellID(edgeRoot)
280-
if relation == Indexed {
281-
// edgeRoot is an index cell or is contained by an index cell (case 1).
280+
switch relation {
281+
case Indexed:
282+
// edgeRoot is an index cell or is contained by an
283+
// index cell (case 1).
282284
c.cells = append(c.cells, c.iter.IndexCell())
283-
} else if relation == Subdivided {
284-
// edgeRoot is subdivided into one or more index cells (case 2). We
285-
// find the cells intersected by AB using recursive subdivision.
285+
case Subdivided:
286+
// edgeRoot is subdivided into one or more index cells
287+
// (case 2). We find the cells intersected by AB using
288+
// recursive subdivision.
286289
if !edgeRoot.isFace() {
287290
pcell = PaddedCellFromCellID(edgeRoot, 0)
288291
}
289292
c.computeCellsIntersected(pcell, edgeBound)
293+
case Disjoint:
290294
}
291295
}
292296
}

s2/edge_clipping.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ func FaceSegments(a, b Point) []FaceSegment {
564564
// Complete the current segment by finding the point where AB
565565
// exits the current face.
566566
z := faceXYZtoUVW(face, ab)
567-
n := pointUVW{z.Vector}
567+
n := pointUVW(z)
568568

569569
exitAxis := n.exitAxis()
570570
segment.b = n.exitPoint(exitAxis)
@@ -605,7 +605,7 @@ func moveOriginToValidFace(face int, a, ab Point, aUV r2.Point) (int, r2.Point)
605605

606606
// Otherwise check whether the normal AB even intersects this face.
607607
z := faceXYZtoUVW(face, ab)
608-
n := pointUVW{z.Vector}
608+
n := pointUVW(z)
609609
if n.intersectsFace() {
610610
// Check whether the point where the line AB exits this face is on the
611611
// wrong side of A (by more than the acceptable error tolerance).

s2/edge_crosser.go

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ func (e *EdgeCrosser) EdgeOrVertexChainCrossing(d Point) bool {
154154
return false
155155
case Cross:
156156
return true
157+
case MaybeCross:
158+
// fall through
157159
}
158160
return VertexCrossing(e.a, e.b, c, d)
159161
}

s2/edge_crossings.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ func EdgeOrVertexCrossing(a, b, c, d Point) bool {
152152
return false
153153
case Cross:
154154
return true
155-
default:
156-
return VertexCrossing(a, b, c, d)
155+
case MaybeCross:
156+
// Fall through to the final return.
157157
}
158+
return VertexCrossing(a, b, c, d)
158159
}
159160

160161
// Intersection returns the intersection point of two edges AB and CD that cross

s2/edge_distances.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func UpdateMinInteriorDistance(x, a, b Point, minDist s1.ChordAngle) (s1.ChordAn
9696
func Project(x, a, b Point) Point {
9797
aXb := a.PointCross(b)
9898
// Find the closest point to X along the great circle through AB.
99-
p := x.Sub(aXb.Mul(x.Dot(aXb.Vector) / aXb.Vector.Norm2()))
99+
p := x.Sub(aXb.Mul(x.Dot(aXb.Vector) / aXb.Norm2()))
100100

101101
// If this point is on the edge AB, then it's the closest point.
102102
if Sign(aXb, a, Point{p}) && Sign(Point{p}, b, aXb) {
@@ -146,7 +146,7 @@ func InterpolateAtDistance(ax s1.Angle, a, b Point) Point {
146146
// result is always perpendicular to A, even if A=B or A=-B, but it is not
147147
// necessarily unit length. (We effectively normalize it below.)
148148
normal := a.PointCross(b)
149-
tangent := normal.Vector.Cross(a.Vector)
149+
tangent := normal.Cross(a.Vector)
150150

151151
// Now compute the appropriate linear combination of A and "tangent". With
152152
// infinite precision the result would always be unit length, but we

s2/edge_distances_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ func TestEdgeDistancesInterpolate(t *testing.T) {
427427
test.a, test.b, r, got, test.want)
428428
}
429429
}
430-
if r.Radians() >= 0 && r.Radians() < 0.99*math.Pi {
430+
if r.Radians() >= 0 && r.Radians() < 0.99*math.Pi { // nolint staticcheck
431+
// TODO(rsned): Remove nolint when this test case is added.
431432
// We don't have the parallel ChordAngle variants of
432433
// PointOnLine/Ray/Left/Right
433434
// so the second tests are not added here.
@@ -478,7 +479,7 @@ func TestEdgeDistancesRepeatedInterpolation(t *testing.T) {
478479
for j := 0; j < 1000; j++ {
479480
a = Interpolate(0.01, a, b)
480481
}
481-
if !a.Vector.IsUnit() {
482+
if !a.IsUnit() {
482483
t.Errorf("repeated Interpolate(%v, %v, %v) calls did not stay unit length for", 0.01, a, b)
483484
}
484485
}
@@ -725,7 +726,7 @@ func TestEdgeDistancesEdgePairMinDistance(t *testing.T) {
725726
actualA, actualB := EdgePairClosestPoints(test.a0, test.a1, test.b0, test.b1)
726727
if test.wantA == zero {
727728
// either end point works.
728-
if !(actualA == test.a0 || actualA == test.a1) {
729+
if actualA != test.a0 && actualA != test.a1 {
729730
t.Errorf("EdgePairClosestPoints(%v, %v, %v, %v) = %v, want %v or %v", test.a0, test.a1, test.b0, test.b1, actualA, test.a0, test.a1)
730731
}
731732
} else {
@@ -736,7 +737,7 @@ func TestEdgeDistancesEdgePairMinDistance(t *testing.T) {
736737

737738
if test.wantB == zero {
738739
// either end point works.
739-
if !(actualB == test.b0 || actualB == test.b1) {
740+
if actualB != test.b0 && actualB != test.b1 {
740741
t.Errorf("EdgePairClosestPoints(%v, %v, %v, %v) = %v, want %v or %v", test.a0, test.a1, test.b0, test.b1, actualB, test.b0, test.b1)
741742
}
742743
} else {

s2/edge_query_test.go

+12-16
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,14 @@ func testEdgeQueryWithGenerator(t *testing.T,
242242
var indexCaps []Cap
243243
var indexes []*ShapeIndex
244244
for i := 0; i < numIndexes; i++ {
245-
// TODO(rsned): Replace with:
246-
// r := rand.New(rand.NewSource(i))
247-
rand.Seed(int64(i))
248-
indexCaps = append(indexCaps, CapFromCenterAngle(randomPoint(), testCapRadius))
245+
r := rand.New(rand.NewSource(i))
246+
indexCaps = append(indexCaps, CapFromCenterAngle(randomPoint(r), testCapRadius))
249247
indexes = append(indexes, NewShapeIndex())
250248
gen(indexCaps[i], numEdges, indexes[i])
251249
}
252250
253251
for i := 0; i < numQueries; i++ {
254-
// TODO(rsned): Replace with:
255-
// r := rand.New(rand.NewSource(i))
256-
rand.Seed(int64(i))
252+
r := rand.New(rand.NewSource(i))
257253
iIndex := randomUniformInt(numIndexes)
258254
indexCap := indexCaps[iIndex]
259255
@@ -273,23 +269,23 @@ func testEdgeQueryWithGenerator(t *testing.T,
273269
// Occasionally we don't set any limit on the number of result edges.
274270
// (This may return all edges if we also don't set a distance limit.)
275271
if oneIn(5) {
276-
opts.MaxResults(1 + randomUniformInt(10))
272+
opts.MaxResults(1 + randomUniformInt(10, r))
277273
}
278274
279275
// We set a distance limit 1/3 of the time.
280276
if oneIn(3) {
281-
opts.DistanceLimit(s1.ChordAngleFromAngle(s1.Angle(randomFloat64()) * queryRadius))
277+
opts.DistanceLimit(s1.ChordAngleFromAngle(s1.Angle(randomFloat64(r)) * queryRadius))
282278
}
283279
if oneIn(2) {
284280
// Choose a maximum error whose logarithm is uniformly distributed over
285281
// a reasonable range, except that it is sometimes zero.
286-
opts.MaxError(s1.ChordAngleFromAngle(s1.Angle(math.Pow(1e-4, randomFloat64()) * queryRadius.Radians())))
282+
opts.MaxError(s1.ChordAngleFromAngle(s1.Angle(math.Pow(1e-4, randomFloat64(r)) * queryRadius.Radians())))
287283
}
288284
opts.IncludeInteriors(oneIn(2))
289285
290286
query := newQueryFunc(indexes[iIndex], opts)
291287
292-
switch randomUniformInt(4) {
288+
switch randomUniformInt(4, r) {
293289
case 0:
294290
// Find the edges furthest from a given point.
295291
point := samplePointFromCap(queryCap)
@@ -299,22 +295,22 @@ func testEdgeQueryWithGenerator(t *testing.T,
299295
// Find the edges furthest from a given edge.
300296
a := samplePointFromCap(queryCap)
301297
b := samplePointFromCap(
302-
CapFromCenterAngle(a, s1.Angle(math.Pow(1e-4, randomFloat64()))*queryRadius))
298+
CapFromCenterAngle(a, s1.Angle(math.Pow(1e-4, randomFloat64(r)))*queryRadius))
303299
target := NewMaxDistanceToEdgeTarget(Edge{a, b})
304300
testFindEdges(target, query)
305301
306302
case 2:
307303
// Find the edges furthest from a given cell.
308304
minLevel := MaxDiagMetric.MinLevel(queryRadius.Radians())
309-
level := minLevel + randomUniformInt(MaxLevel-minLevel+1)
305+
level := minLevel + randomUniformInt(MaxLevel-minLevel+1, r)
310306
a := samplePointFromCap(queryCap)
311307
cell := CellFromCellID(cellIDFromPoint(a).Parent(level))
312308
target := NewMaxDistanceToCellTarget(cell)
313309
testFindEdges(target, query)
314310
315311
case 3:
316312
// Use another one of the pre-built indexes as the target.
317-
jIndex := randomUniformInt(numIndexes)
313+
jIndex := randomUniformInt(numIndexes, r)
318314
target := NewMaxDistanceToShapeIndexTarget(indexes[jIndex])
319315
target.setIncludeInteriors(oneIn(2))
320316
testFindEdges(target, query)
@@ -438,9 +434,9 @@ func generateEdgeQueryWithTargets(opts *edgeQueryBenchmarkOptions, query *EdgeQu
438434

439435
// Set a specific seed to allow repeatability
440436
// Replace with r := rand.New(rand.NewSource(opts.randomSeed)) and pass through.
441-
rand.Seed(opts.randomSeed)
437+
r := rand.New(rand.NewSource(opts.randomSeed))
442438
opts.randomSeed++
443-
indexCap := CapFromCenterAngle(randomPoint(), opts.radiusKm)
439+
indexCap := CapFromCenterAngle(randomPoint(r), opts.radiusKm)
444440

445441
query.Reset()
446442
queryIndex.Reset()

s2/encode_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import (
2727
)
2828

2929
type encodableRegion interface {
30-
Encode(io.Writer) error
30+
Encode(w io.Writer) error
3131
}
3232

3333
type decodableRegion interface {
34-
Decode(io.Reader) error
34+
Decode(r io.Reader) error
3535
}
3636

3737
const (

s2/lax_polygon.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,15 @@ func LaxPolygonFromPolygon(p *Polygon) *LaxPolygon {
9292
func LaxPolygonFromPoints(loops [][]Point) *LaxPolygon {
9393
p := &LaxPolygon{}
9494
p.numLoops = len(loops)
95-
if p.numLoops == 0 {
95+
switch p.numLoops {
96+
case 0:
9697
p.numVerts = 0
9798
p.vertices = nil
98-
} else if p.numLoops == 1 {
99+
case 1:
99100
p.numVerts = len(loops[0])
100101
p.vertices = make([]Point, p.numVerts)
101102
copy(p.vertices, loops[0])
102-
} else {
103+
default:
103104
p.cumulativeVertices = make([]int, p.numLoops+1)
104105
numVertices := 0
105106
for i, loop := range loops {

s2/loop_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ func TestLoopRelations(t *testing.T) {
13501350
a1 := cloneLoop(test.a)
13511351
a1.Invert()
13521352
testLoopNestedPair(t, a1, test.b)
1353-
} else if !(test.contains || test.contained || test.covers) {
1353+
} else if !test.contains && !test.contained && !test.covers {
13541354
// Given loops A and B such that both A and its complement
13551355
// intersect both B and its complement, test various
13561356
// identities involving these four loops.

s2/max_distance_targets_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ func TestDistanceTargetMaxShapeIndexTargetCapBound(t *testing.T) {
436436
pTest := randomPoint()
437437
// Check points outside of cap to be away from maxDistance's zero().
438438
if !c.ContainsPoint(pTest) {
439-
var curDist distance = inf
439+
var curDist = inf
440440
var ok bool
441441
if curDist, ok = target.updateDistanceToPoint(pTest, curDist); !ok {
442442
t.Errorf("updateDistanceToPoint failed, but should have succeeded")

s2/point.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func OrderedCCW(a, b, c, o Point) bool {
123123

124124
// Distance returns the angle between two points.
125125
func (p Point) Distance(b Point) s1.Angle {
126-
return p.Vector.Angle(b.Vector)
126+
return p.Angle(b.Vector)
127127
}
128128

129129
// ApproxEqual reports whether the two points are similar enough to be equal.
@@ -133,7 +133,7 @@ func (p Point) ApproxEqual(other Point) bool {
133133

134134
// approxEqual reports whether the two points are within the given epsilon.
135135
func (p Point) approxEqual(other Point, eps s1.Angle) bool {
136-
return p.Vector.Angle(other.Vector) <= eps
136+
return p.Angle(other.Vector) <= eps
137137
}
138138

139139
// ChordAngleBetweenPoints constructs a ChordAngle corresponding to the distance
@@ -254,7 +254,7 @@ func Ortho(a Point) Point {
254254
temp.Z = 1
255255
case r3.YAxis:
256256
temp.X = 1
257-
default:
257+
case r3.ZAxis:
258258
temp.Y = 1
259259
}
260260
return Point{a.Cross(temp).Normalize()}

s2/polygon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ func (p *Polygon) Validate() error {
447447
for i, l := range p.loops {
448448
// Check for loop errors that don't require building a ShapeIndex.
449449
if err := l.findValidationErrorNoIndex(); err != nil {
450-
return fmt.Errorf("loop %d: %v", i, err)
450+
return fmt.Errorf("loop %d: %w", i, err)
451451
}
452452
// Check that no loop is empty, and that the full loop only appears in the
453453
// full polygon.

0 commit comments

Comments
 (0)