Skip to content

GODRIVER-3293 Upgrade golangci-lint to 1.59.1 and fix lint errors. #1729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
run:
timeout: 5m
skip-dirs-use-default: false
skip-dirs:
- (^|/)vendor($|/)
- (^|/)testdata($|/)
- (^|/)etc($|/)
# Disable all linters for "golang.org/x/exp/rand" package in internal/rand.
- internal/rand

linters:
disable-all: true
Expand Down Expand Up @@ -35,11 +28,7 @@ linters:

linters-settings:
errcheck:
exclude: .errcheck-excludes
gocritic:
enabled-checks:
# Detects suspicious append result assignments. E.g. "b := append(a, 1, 2, 3)"
- appendAssign
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it covered by default in the new version?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is! I got a warning that the config is redundant:

WARN [linters_context] gocritic: no need to enable check "appendAssign": it's already enabled 

exclude-functions: .errcheck-excludes
govet:
disable:
- cgocall
Expand All @@ -55,6 +44,14 @@ linters-settings:
]

issues:
exclude-dirs-use-default: false
exclude-dirs:
- (^|/)testdata($|/)
- (^|/)etc($|/)
# Disable all linters for copied third-party code.
- internal/rand
- internal/aws
- internal/assert
exclude-use-default: false
exclude:
# Add all default excluded issues except issues related to exported types/functions not having
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ repos:
exclude: ^(vendor)

- repo: https://github.com/golangci/golangci-lint
rev: v1.55.1
rev: v1.59.1
hooks:
- id: golangci-lint
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ doc:
fmt:
go fmt ./...

# NOTE: A golangci-lint version is also pinned in .pre-commit-config.yaml. Make
# sure to change it there to keep it in-sync with what's used here!
.PHONY: install-golangci-lint
install-golangci-lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1

# Lint with various GOOS and GOARCH targets to catch static analysis failures that may only affect
# specific operating systems or architectures. For example, staticcheck will only check for 64-bit
Expand Down
2 changes: 1 addition & 1 deletion bson/bsoncodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ExampleValueEncoder() {
}

func ExampleValueDecoder() {
var _ ValueDecoderFunc = func(dc DecodeContext, vr ValueReader, val reflect.Value) error {
var _ ValueDecoderFunc = func(_ DecodeContext, vr ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Kind() != reflect.String {
return ValueDecoderError{Name: "StringDecodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val}
}
Expand Down
5 changes: 2 additions & 3 deletions bson/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@ func (d Decimal128) BigInt() (*big.Int, int, error) {
// Bits: 1*sign 2*ignored 14*exponent 111*significand.
// Implicit 0b100 prefix in significand.
exp = int(high >> 47 & (1<<14 - 1))
//high = 4<<47 | d.h&(1<<47-1)
// Spec says all of these values are out of range.
high, low = 0, 0
} else {
// Bits: 1*sign 14*exponent 113*significand
exp = int(high >> 49 & (1<<14 - 1))
high = high & (1<<49 - 1)
high &= (1<<49 - 1)
}
exp += MinDecimal128Exp

Expand Down Expand Up @@ -258,7 +257,7 @@ var (

// ParseDecimal128FromBigInt attempts to parse the given significand and exponent into a valid Decimal128 value.
func ParseDecimal128FromBigInt(bi *big.Int, exp int) (Decimal128, bool) {
//copy
// copy
bi = new(big.Int).Set(bi)

q := new(big.Int)
Expand Down
2 changes: 1 addition & 1 deletion bson/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func TestDecoderConfiguration(t *testing.T) {
// independent of the top-level Go value type.
{
description: "DocumentD nested by default",
configure: func(dec *Decoder) {},
configure: func(_ *Decoder) {},
input: bsoncore.NewDocumentBuilder().
AppendDocument("myDocument", bsoncore.NewDocumentBuilder().
AppendString("myString", "test value").
Expand Down
7 changes: 4 additions & 3 deletions bson/default_value_decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,8 @@ func decodeDefault(dc DecodeContext, vr ValueReader, val reflect.Value) ([]refle
var elem reflect.Value
if vDecoder == nil {
elem = val.Index(idx).Elem()
if elem.Kind() != reflect.Ptr || elem.IsNil() {
switch {
case elem.Kind() != reflect.Ptr || elem.IsNil():
valueDecoder, err := dc.LookupDecoder(elem.Type())
if err != nil {
return nil, err
Expand All @@ -1295,12 +1296,12 @@ func decodeDefault(dc DecodeContext, vr ValueReader, val reflect.Value) ([]refle
if err != nil {
return nil, newDecodeError(strconv.Itoa(idx), err)
}
} else if vr.Type() == TypeNull {
case vr.Type() == TypeNull:
if err = vr.ReadNull(); err != nil {
return nil, err
}
elem = reflect.Zero(val.Index(idx).Type())
} else {
default:
e := elem.Elem()
valueDecoder, err := dc.LookupDecoder(e.Type())
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions bson/default_value_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ func TestDefaultValueEncoders(t *testing.T) {
},
{
"WriteArrayElement Error",
bsoncore.Array(buildDocumentArray(func(doc []byte) []byte {
bsoncore.Array(buildDocumentArray(func([]byte) []byte {
return bsoncore.AppendNullElement(nil, "foo")
})),
nil,
Expand All @@ -1085,7 +1085,7 @@ func TestDefaultValueEncoders(t *testing.T) {
},
{
"encodeValue error",
bsoncore.Array(buildDocumentArray(func(doc []byte) []byte {
bsoncore.Array(buildDocumentArray(func([]byte) []byte {
return bsoncore.AppendNullElement(nil, "foo")
})),
nil,
Expand Down
2 changes: 1 addition & 1 deletion bson/extjson_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (ejp *extJSONParser) readValue(t Type) (*extJSONValue, error) {
}

// remove hyphens
uuidNoHyphens := strings.Replace(uuid, "-", "", -1)
uuidNoHyphens := strings.ReplaceAll(uuid, "-", "")
if len(uuidNoHyphens) != 32 {
return nil, fmt.Errorf("$uuid value does not follow RFC 4122 format regarding length and hyphens")
}
Expand Down
13 changes: 6 additions & 7 deletions bson/extjson_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,14 @@ func (ejvw *extJSONValueWriter) WriteArrayEnd() error {

func formatDouble(f float64) string {
var s string
if math.IsInf(f, 1) {
switch {
case math.IsInf(f, 1):
s = "Infinity"
} else if math.IsInf(f, -1) {
case math.IsInf(f, -1):
s = "-Infinity"
} else if math.IsNaN(f) {
case math.IsNaN(f):
s = "NaN"
} else {
default:
// Print exactly one decimalType place for integers; otherwise, print as many are necessary to
// perfectly represent it.
s = strconv.FormatFloat(f, 'G', -1, 64)
Expand Down Expand Up @@ -678,9 +679,7 @@ func (ss sortableString) Less(i, j int) bool {
}

func (ss sortableString) Swap(i, j int) {
oldI := ss[i]
ss[i] = ss[j]
ss[j] = oldI
ss[i], ss[j] = ss[j], ss[i]
}

func sortStringAlphebeticAscending(s string) string {
Expand Down
37 changes: 21 additions & 16 deletions bson/json_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ func (js *jsonScanner) nextToken() (*jsonToken, error) {
return js.scanString()
default:
// check if it's a number
if c == '-' || isDigit(c) {
switch {
case c == '-' || isDigit(c):
return js.scanNumber(c)
} else if c == 't' || c == 'f' || c == 'n' {
case c == 't' || c == 'f' || c == 'n':
// maybe a literal
return js.scanLiteral(c)
} else {
default:
return nil, fmt.Errorf("invalid JSON input. Position: %d. Character: %c", js.pos-1, c)
}
}
Expand Down Expand Up @@ -174,7 +175,7 @@ func getu4(s []byte) rune {
for _, c := range s[:4] {
switch {
case '0' <= c && c <= '9':
c = c - '0'
c -= '0'
case 'a' <= c && c <= 'f':
c = c - 'a' + 10
case 'A' <= c && c <= 'F':
Expand Down Expand Up @@ -325,13 +326,14 @@ func (js *jsonScanner) scanLiteral(first byte) (*jsonToken, error) {

c5, err := js.readNextByte()

if bytes.Equal([]byte("true"), lit) && (isValueTerminator(c5) || errors.Is(err, io.EOF)) {
switch {
case bytes.Equal([]byte("true"), lit) && (isValueTerminator(c5) || errors.Is(err, io.EOF)):
js.pos = int(math.Max(0, float64(js.pos-1)))
return &jsonToken{t: jttBool, v: true, p: p}, nil
} else if bytes.Equal([]byte("null"), lit) && (isValueTerminator(c5) || errors.Is(err, io.EOF)) {
case bytes.Equal([]byte("null"), lit) && (isValueTerminator(c5) || errors.Is(err, io.EOF)):
js.pos = int(math.Max(0, float64(js.pos-1)))
return &jsonToken{t: jttNull, v: nil, p: p}, nil
} else if bytes.Equal([]byte("fals"), lit) {
case bytes.Equal([]byte("fals"), lit):
if c5 == 'e' {
c5, err = js.readNextByte()

Expand Down Expand Up @@ -430,12 +432,13 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
case '}', ']', ',':
s = nssDone
default:
if isWhiteSpace(c) || errors.Is(err, io.EOF) {
switch {
case isWhiteSpace(c) || errors.Is(err, io.EOF):
s = nssDone
} else if isDigit(c) {
case isDigit(c):
s = nssSawIntegerDigits
b.WriteByte(c)
} else {
default:
s = nssInvalid
}
}
Expand All @@ -455,12 +458,13 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
case '}', ']', ',':
s = nssDone
default:
if isWhiteSpace(c) || errors.Is(err, io.EOF) {
switch {
case isWhiteSpace(c) || errors.Is(err, io.EOF):
s = nssDone
} else if isDigit(c) {
case isDigit(c):
s = nssSawFractionDigits
b.WriteByte(c)
} else {
default:
s = nssInvalid
}
}
Expand Down Expand Up @@ -490,12 +494,13 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
case '}', ']', ',':
s = nssDone
default:
if isWhiteSpace(c) || errors.Is(err, io.EOF) {
switch {
case isWhiteSpace(c) || errors.Is(err, io.EOF):
s = nssDone
} else if isDigit(c) {
case isDigit(c):
s = nssSawExponentDigits
b.WriteByte(c)
} else {
default:
s = nssInvalid
}
}
Expand Down
4 changes: 3 additions & 1 deletion bson/mgoregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var allItems = []testItemType{
{M{"_": Undefined{}}, // Obsolete, but still seen in the wild.
"\x06_\x00"},
{M{"_": ObjectID{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B}},
"\x07_\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"}, //technically this is not the same as the original mgo test
"\x07_\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"}, // technically this is not the same as the original mgo test
{M{"_": DBPointer{DB: "testnamespace", Pointer: ObjectID{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B}}},
"\x0C_\x00\x0e\x00\x00\x00testnamespace\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"},
{M{"_": false},
Expand Down Expand Up @@ -1678,6 +1678,8 @@ var jsonIDTests = []struct {

func TestObjectIdJSONMarshaling(t *testing.T) {
for i, test := range jsonIDTests {
test := test // Capture range variable.

t.Run(strconv.Itoa(i), func(t *testing.T) {
if test.marshal {
data, err := json.Marshal(&test.value)
Expand Down
8 changes: 4 additions & 4 deletions bson/registry_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ExampleRegistry_customEncoder() {
negatedIntType := reflect.TypeOf(negatedInt(0))

negatedIntEncoder := func(
ec bson.EncodeContext,
_ bson.EncodeContext,
vw bson.ValueWriter,
val reflect.Value,
) error {
Expand Down Expand Up @@ -85,7 +85,7 @@ func ExampleRegistry_customDecoder() {
lenientBoolType := reflect.TypeOf(lenientBool(true))

lenientBoolDecoder := func(
dc bson.DecodeContext,
_ bson.DecodeContext,
vr bson.ValueReader,
val reflect.Value,
) error {
Expand Down Expand Up @@ -164,7 +164,7 @@ func ExampleRegistry_RegisterKindEncoder() {
// encoder for kind reflect.Int32. That way, even user-defined types with
// underlying type int32 will be encoded as a BSON int64.
int32To64Encoder := func(
ec bson.EncodeContext,
_ bson.EncodeContext,
vw bson.ValueWriter,
val reflect.Value,
) error {
Expand Down Expand Up @@ -223,7 +223,7 @@ func ExampleRegistry_RegisterKindDecoder() {
// "kind" decoder for kind reflect.Int64. That way, we can even decode to
// user-defined types with underlying type int64.
flexibleInt64KindDecoder := func(
dc bson.DecodeContext,
_ bson.DecodeContext,
vr bson.ValueReader,
val reflect.Value,
) error {
Expand Down
Loading
Loading