From 2c780f940d1e6d36ae424e2fb9afda28701813a7 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 5 Mar 2024 00:02:40 +0100 Subject: [PATCH] Update golangci-lint --- .github/workflows/main.yml | 2 +- .golangci.yml | 52 +++++++++++++++++-- bigxy/big_cga.go | 18 +++---- encoding/ewkbhex/ewkbhex_test.go | 3 +- encoding/geojson/geojson.go | 8 +-- encoding/igc/decode.go | 2 +- encoding/wkb/sql_example_test.go | 1 + encoding/wkb/wkb.go | 3 +- encoding/wkbhex/wkbhex_test.go | 5 +- encoding/wkt/lex.go | 2 +- encoding/wkt/lex_errors.go | 4 +- encoding/wkt/wkt_test.go | 10 ++-- linearring.go | 2 +- sorting/sorting.go | 7 +-- xy/internal/hcoords/hcoords.go | 6 +-- .../robustdeterminate/robust_determinate.go | 6 +-- xy/line_centroid_examples_test.go | 1 + 17 files changed, 88 insertions(+), 44 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 127b293f..48e28305 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -4,7 +4,7 @@ on: push: env: GOFUMPT_VERSION: 0.3.1 - GOLANGCI_LINT_VERSION: 1.49.0 + GOLANGCI_LINT_VERSION: 1.56.2 jobs: linux: runs-on: ubuntu-latest diff --git a/.golangci.yml b/.golangci.yml index b7c2cc23..fa6a0504 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,18 +1,28 @@ linters: enable: + - asasalint - asciicheck + - bidichk - bodyclose - - depguard + - containedctx + - contextcheck + - decorder - dogsled - dupl + - dupword - durationcheck - errcheck + - errchkjson - errorlint + - execinquery - exportloopref - forbidigo - forcetypeassert - gci + - ginkgolinter + - gocheckcompilerdirectives - gochecknoinits + - gochecksumtype - gocritic - godot - goerr113 @@ -24,33 +34,58 @@ linters: - goprintffuncname - gosec - gosimple + - gosmopolitan - govet + - grouper - importas + - inamedparam - ineffassign + - interfacebloat + - ireturn + - loggercheck - makezero + - mirror - misspell + - musttag - nakedret - nilerr + - nilnil - noctx - nolintlint + - nosprintfhostport + - perfsprint - prealloc - predeclared - promlinter + - protogetter + - reassign - revive + - rowserrcheck + - sloglint + - spancheck + - sqlclosecheck - staticcheck - stylecheck + - tagalign - tagliatelle + - tenv + - testableexamples + - testifylint - thelper - tparallel - typecheck - unconvert - unparam - unused + - usestdlibvars - whitespace + - zerologlint disable: - cyclop + - depguard + - errname # FIXME renaming ErrXxx to XxxError will require a major version bump - exhaustive - - exhaustivestruct + - exhaustruct - funlen - gochecknoglobals - gocognit @@ -60,12 +95,15 @@ linters: - goheader - gomnd - lll + - maintidx - nestif - nlreturn + - nonamedreturns - paralleltest - - rowserrcheck # https://github.com/golangci/golangci-lint/issues/2649 - - sqlclosecheck # https://github.com/golangci/golangci-lint/issues/2649 + - paralleltest + - testpackage - testpackage + - varnamelen - wastedassign # https://github.com/golangci/golangci-lint/issues/2649 - wrapcheck - wsl @@ -78,6 +116,12 @@ linters-settings: - prefix(github.com/twpayne/go-geom) goimports: local-prefixes: github.com/twpayne/go-geom + ireturn: + allow: + - error + - stdlib + - ^github\.com/twpayne/go-geom\.T$ + - ^github\.com/twpayne/go-kml/v3\.Element$ issues: exclude-rules: diff --git a/bigxy/big_cga.go b/bigxy/big_cga.go index 7318d561..ab32a3a2 100644 --- a/bigxy/big_cga.go +++ b/bigxy/big_cga.go @@ -17,16 +17,16 @@ import ( // relative round-off error in big.Float precision numbers var dpSafeEpsilon = 1e-15 -// OrientationIndex returns the index of the direction of the point point relative to -// a vector specified by vectorOrigin-vectorEnd +// OrientationIndex returns the index of the direction of point relative +// to a vector specified by vectorOrigin-vectorEnd // -// vectorOrigin - the origin point of the vector -// vectorEnd - the final point of the vector -// point - the point to compute the direction to +// vectorOrigin - the origin point of the vector vectorEnd - the final point of +// the vector point - the point to compute the direction to // -// Returns CounterClockwise if point is counter-clockwise (left) from vectorOrigin-vectorEnd -// Returns Clockwise if point is clockwise (right) from vectorOrigin-vectorEnd -// Returns Collinear if point is collinear with vectorOrigin-vectorEnd +// Returns CounterClockwise if point is counter-clockwise (left) from +// vectorOrigin-vectorEnd. Returns Clockwise if point is clockwise (right) from +// vectorOrigin-vectorEnd. Returns Collinear if point is collinear with +// vectorOrigin-vectorEnd. func OrientationIndex(vectorOrigin, vectorEnd, point geom.Coord) orientation.Type { // fast filter for orientation index // avoids use of slow extended-precision arithmetic in many cases @@ -52,7 +52,7 @@ func OrientationIndex(vectorOrigin, vectorEnd, point geom.Coord) orientation.Typ } // Intersection computes the intersection point of the two lines using math.big.Float arithmetic. -// The lines are considered infinate in length. For example, (0,0), (1, 0) and (2, 1) (2, 2) will have intersection of (2, 0) +// The lines are considered infinite in length. For example, (0,0), (1, 0) and (2, 1) (2, 2) will have intersection of (2, 0) // Currently does not handle case of parallel lines. func Intersection(line1Start, line1End, line2Start, line2End geom.Coord) geom.Coord { var denom1, denom2, denom, tmp1, tmp2 big.Float diff --git a/encoding/ewkbhex/ewkbhex_test.go b/encoding/ewkbhex/ewkbhex_test.go index 3a0c491c..ef31346e 100644 --- a/encoding/ewkbhex/ewkbhex_test.go +++ b/encoding/ewkbhex/ewkbhex_test.go @@ -2,7 +2,6 @@ package ewkbhex import ( "encoding/hex" - "fmt" "testing" "github.com/alecthomas/assert/v2" @@ -215,7 +214,7 @@ func Test(t *testing.T) { xdr: "0020000003000010e60000000100000005c05314cc94c5c52c40440619d4a35817c05314cbfce5c52c4044061ca33b581bc05314c9819fc5294044061b580f5817c05314ca197fc52b4044061891335816c05314cc94c5c52c40440619d4a35817", }, } { - t.Run(fmt.Sprintf("ndr:%s", tc.ndr), func(t *testing.T) { + t.Run("ndr:"+tc.ndr, func(t *testing.T) { test(t, tc.g, tc.xdr, tc.ndr) }) } diff --git a/encoding/geojson/geojson.go b/encoding/geojson/geojson.go index f1143d90..748fe3ac 100644 --- a/encoding/geojson/geojson.go +++ b/encoding/geojson/geojson.go @@ -28,7 +28,7 @@ func (e ErrDimensionalityTooLow) Error() string { type ErrUnsupportedType string func (e ErrUnsupportedType) Error() string { - return fmt.Sprintf("geojson: unsupported type: %s", string(e)) + return "geojson: unsupported type: " + string(e) } // CRS is a deprecated field but still populated in some programs (e.g. PostGIS). @@ -313,7 +313,7 @@ func EncodeGeometryWithBBox() EncodeGeometryOption { // EncodeGeometryWithCRS adds the crs field to the Geometry GeoJSON encoding. func EncodeGeometryWithCRS(crs *CRS) EncodeGeometryOption { return EncodeGeometryOption{ - onGeometryHandler: func(g *Geometry, t geom.T, opts ...EncodeGeometryOption) error { + onGeometryHandler: func(g *Geometry, _ geom.T, _ ...EncodeGeometryOption) error { var err error g.CRS = crs return err @@ -332,7 +332,7 @@ func EncodeGeometryWithMaxDecimalDigits(maxDecimalDigits int) EncodeGeometryOpti // Encode encodes g as a GeoJSON geometry. func Encode(g geom.T, opts ...EncodeGeometryOption) (*Geometry, error) { if g == nil { - return nil, nil + return nil, nil //nolint:nilnil } ret, err := encode(g, opts...) if err != nil { @@ -351,7 +351,7 @@ func Encode(g geom.T, opts ...EncodeGeometryOption) (*Geometry, error) { // encode encodes the geometry assuming it is not nil. func encode(g geom.T, opts ...EncodeGeometryOption) (*Geometry, error) { if g == nil { - return nil, nil + return nil, nil //nolint:nilnil } switch g := g.(type) { case *geom.Point: diff --git a/encoding/igc/decode.go b/encoding/igc/decode.go index 21b8fd65..e2ae47af 100644 --- a/encoding/igc/decode.go +++ b/encoding/igc/decode.go @@ -204,7 +204,7 @@ func (p *parser) parseB(line string) error { func (p *parser) parseH(line string) error { m := hRegexp.FindStringSubmatch(line) if m == nil { - return fmt.Errorf("invalid H record") + return errors.New("invalid H record") } header := Header{ Source: m[1], diff --git a/encoding/wkb/sql_example_test.go b/encoding/wkb/sql_example_test.go index 9ff6bbe2..bd993115 100644 --- a/encoding/wkb/sql_example_test.go +++ b/encoding/wkb/sql_example_test.go @@ -160,6 +160,7 @@ func Example_value_different_shapes() { } for _, s := range shapes { + s := s result, err := db.Exec(`INSERT INTO objects (name, location) VALUES (?, ?);`, s.Name, &s.Geom) if err != nil { log.Fatal(err) diff --git a/encoding/wkb/wkb.go b/encoding/wkb/wkb.go index 481cb090..b12f91bc 100644 --- a/encoding/wkb/wkb.go +++ b/encoding/wkb/wkb.go @@ -8,6 +8,7 @@ package wkb import ( "bytes" "encoding/binary" + "errors" "fmt" "io" @@ -265,7 +266,7 @@ func Write(w io.Writer, byteOrder binary.ByteOrder, g geom.T, opts ...wkbcommon. case wkbcommon.EmptyPointHandlingNaN: return wkbcommon.WriteEmptyPointAsNaN(w, byteOrder, g.Stride()) case wkbcommon.EmptyPointHandlingError: - return fmt.Errorf("cannot encode empty Point in WKB") + return errors.New("cannot encode empty Point in WKB") default: return fmt.Errorf("cannot encode empty Point in WKB (unknown option: %d)", wkbcommon.EmptyPointHandlingNaN) } diff --git a/encoding/wkbhex/wkbhex_test.go b/encoding/wkbhex/wkbhex_test.go index 8ada33e4..cd42e240 100644 --- a/encoding/wkbhex/wkbhex_test.go +++ b/encoding/wkbhex/wkbhex_test.go @@ -1,7 +1,6 @@ package wkbhex import ( - "fmt" "testing" "github.com/alecthomas/assert/v2" @@ -154,7 +153,7 @@ func Test(t *testing.T) { }, } { if tc.ndr != "" { - t.Run(fmt.Sprintf("ndr:%s", tc.ndr), func(t *testing.T) { + t.Run("ndr:"+tc.ndr, func(t *testing.T) { t.Run("encode", func(t *testing.T) { got, err := Encode(tc.g, wkb.NDR, tc.opts...) assert.NoError(t, err) @@ -168,7 +167,7 @@ func Test(t *testing.T) { }) } if tc.xdr != "" { - t.Run(fmt.Sprintf("xdr:%s", tc.xdr), func(t *testing.T) { + t.Run("xdr:"+tc.xdr, func(t *testing.T) { t.Run("encode", func(t *testing.T) { got, err := Encode(tc.g, wkb.XDR, tc.opts...) assert.NoError(t, err) diff --git a/encoding/wkt/lex.go b/encoding/wkt/lex.go index e0cb293d..e7b59ed5 100644 --- a/encoding/wkt/lex.go +++ b/encoding/wkt/lex.go @@ -384,7 +384,7 @@ func (l *wktLex) nextScannedPointMustBeEmpty() bool { // setLexError is called by Lex when a lexing (tokenizing) error is detected. func (l *wktLex) setLexError(expectedTokType string) { - l.Error(fmt.Sprintf("invalid %s", expectedTokType)) + l.Error("invalid " + expectedTokType) } // setParseError is called when a context-sensitive error is detected during parsing. diff --git a/encoding/wkt/lex_errors.go b/encoding/wkt/lex_errors.go index bf9055f3..9df396cd 100644 --- a/encoding/wkt/lex_errors.go +++ b/encoding/wkt/lex_errors.go @@ -59,11 +59,11 @@ func (e *SyntaxError) Error() string { snippet := e.wkt[snipStart:snipEnd] snippet = strings.ReplaceAll(snippet, "\t", " ") err += strLinePrefix + snippet + strLineSuffix - err += fmt.Sprintf("%s^", strings.Repeat(" ", len(strLinePrefix)+snipPos)) + err += strings.Repeat(" ", len(strLinePrefix)+snipPos) + "^" // Print a hint, if applicable. if e.hint != "" { - err += fmt.Sprintf("\nHINT: %s", e.hint) + err += "\nHINT: " + e.hint } return err diff --git a/encoding/wkt/wkt_test.go b/encoding/wkt/wkt_test.go index ab2265ea..f055e95d 100644 --- a/encoding/wkt/wkt_test.go +++ b/encoding/wkt/wkt_test.go @@ -939,12 +939,12 @@ LINE 1: POINT Z M (1 1 1 1) desc: "invalid geometry type split over multiple lines", input: `POINT Z - Z ( + M ( 0 0 )`, expectedErrStr: `syntax error: invalid keyword at line 3, pos 7 -LINE 3: Z ( +LINE 3: M ( ^`, }, { @@ -964,9 +964,9 @@ LINE 1: ..., 0 0, 0 0, 0 0, 0 0, 0 0, 0 0} // ParseError { desc: "invalid point", - input: "POINT POINT", - expectedErrStr: `syntax error: unexpected POINT, expecting '(' at line 1, pos 6 -LINE 1: POINT POINT + input: "POINT P", + expectedErrStr: `syntax error: invalid keyword at line 1, pos 6 +LINE 1: POINT P ^`, }, { diff --git a/linearring.go b/linearring.go index 44fb7ab7..d46dae57 100644 --- a/linearring.go +++ b/linearring.go @@ -19,7 +19,7 @@ func NewLinearRingFlat(layout Layout, flatCoords []float64) *LinearRing { return g } -// Area returns the the area. +// Area returns the area. func (g *LinearRing) Area() float64 { return doubleArea1(g.flatCoords, 0, len(g.flatCoords), g.stride) / 2 } diff --git a/sorting/sorting.go b/sorting/sorting.go index 1ceffdd9..69e373f1 100644 --- a/sorting/sorting.go +++ b/sorting/sorting.go @@ -2,10 +2,11 @@ package sorting import "github.com/twpayne/go-geom" -// FlatCoord is a sort.Interface implementation that will result in sorting the wrapped coords based on the -// the comparator function +// FlatCoord is a sort.Interface implementation that will result in sorting the +// wrapped coords based on the comparator function // -// Note: this data struct cannot be used with its 0 values. it must be constructed using NewFlatCoordSorting +// Note: this data struct cannot be used with its 0 values. it must be +// constructed using NewFlatCoordSorting type FlatCoord struct { isLess IsLess coords []float64 diff --git a/xy/internal/hcoords/hcoords.go b/xy/internal/hcoords/hcoords.go index 83734fb3..d8ba8af9 100644 --- a/xy/internal/hcoords/hcoords.go +++ b/xy/internal/hcoords/hcoords.go @@ -1,7 +1,7 @@ package hcoords import ( - "fmt" + "errors" "math" "github.com/twpayne/go-geom" @@ -31,11 +31,11 @@ func GetIntersection(line1End1, line1End2, line2End1, line2End2 geom.Coord) (geo yIntersection := y / w if math.IsNaN(xIntersection) || math.IsNaN(yIntersection) { - return nil, fmt.Errorf("intersection cannot be calculated using the h-coords implementation") + return nil, errors.New("intersection cannot be calculated using the h-coords implementation") } if math.IsInf(xIntersection, 0) || math.IsInf(yIntersection, 0) { - return nil, fmt.Errorf("intersection cannot be calculated using the h-coords implementation") + return nil, errors.New("intersection cannot be calculated using the h-coords implementation") } return geom.Coord{xIntersection, yIntersection}, nil diff --git a/xy/internal/robustdeterminate/robust_determinate.go b/xy/internal/robustdeterminate/robust_determinate.go index 4c20d7d7..a9730479 100644 --- a/xy/internal/robustdeterminate/robust_determinate.go +++ b/xy/internal/robustdeterminate/robust_determinate.go @@ -81,8 +81,7 @@ func SignOfDet2x2(x1, y1, x2, y2 float64) Sign { */ if 0.0 < y1 { if 0.0 < y2 { - if y1 <= y2 { - } else { + if y1 > y2 { sign = Negative swap = x1 x1 = x2 @@ -145,8 +144,7 @@ func SignOfDet2x2(x1, y1, x2, y2 float64) Sign { */ if 0.0 < x1 { if 0.0 < x2 { - if x1 <= x2 { - } else { + if x1 > x2 { return sign } } else { diff --git a/xy/line_centroid_examples_test.go b/xy/line_centroid_examples_test.go index b05e7680..c5eb623d 100644 --- a/xy/line_centroid_examples_test.go +++ b/xy/line_centroid_examples_test.go @@ -20,6 +20,7 @@ func ExampleLinearRingsCentroid() { line2 := geom.NewLinearRingFlat(geom.XY, []float64{10, 10, 11, 11, 13, 13, 10, 10}) centroid := xy.LinearRingsCentroid(line1, line2) fmt.Println(centroid) + // Output: [6.499999999999999 6.499999999999999] } func ExampleMultiLineCentroid() {