Skip to content

Commit

Permalink
fix(lint): Fix or ignore lint errors (#2228)
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson authored Sep 30, 2024
1 parent 8f40d1f commit 69d3e0c
Show file tree
Hide file tree
Showing 26 changed files with 76 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ linters:
enable:
- asciicheck
- bodyclose
- copyloopvar
# - dogsled
# - dupl
- errcheck
# - exhaustive
- exportloopref
# - funlen
# - gci
# - gochecknoglobals
Expand Down
4 changes: 0 additions & 4 deletions coll/coll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ func TestLessThan(t *testing.T) {
}

for _, d := range data {
d := d
t.Run(fmt.Sprintf(`LessThan("%s")(<%T>%#v,%#v)==%v`, d.key, d.left, d.left, d.right, d.out), func(t *testing.T) {
assert.Equal(t, d.out, lessThan(d.key)(d.left, d.right))
})
Expand Down Expand Up @@ -462,7 +461,6 @@ func TestSort(t *testing.T) {
}

for _, d := range data {
d := d
t.Run(fmt.Sprintf(`Sort("%s",<%T>)==%#v`, d.key, d.in, d.out), func(t *testing.T) {
out, err := Sort(d.key, d.in)
require.NoError(t, err)
Expand Down Expand Up @@ -569,9 +567,7 @@ func BenchmarkFlatten(b *testing.B) {
},
}
for depth := -1; depth <= 2; depth++ {
depth := depth
for _, d := range data {
d := d
b.Run(fmt.Sprintf("depth%d %T(%v)", depth, d, d), func(b *testing.B) {
for i := 0; i < b.N; i++ {
Flatten(d, depth)
Expand Down
8 changes: 7 additions & 1 deletion coll/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package coll

import (
"fmt"
"math"
"reflect"
)

Expand Down Expand Up @@ -69,7 +70,12 @@ func indexArg(index reflect.Value, cap int) (int, error) {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
x = index.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
x = int64(index.Uint())
val := index.Uint()
if val > math.MaxInt64 {
return -1, fmt.Errorf("cannot index slice/array with %d (too large)", val)
}

x = int64(val)
case reflect.Invalid:
return 0, fmt.Errorf("cannot index slice/array with nil")
default:
Expand Down
7 changes: 5 additions & 2 deletions conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,15 @@ func ToInt64(v interface{}) (int64, error) {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
return val.Int(), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
//nolint:gosec // G115 isn't applicable, this is a Uint32 at most
return int64(val.Uint()), nil
case reflect.Uint, reflect.Uint64:
tv := val.Uint()

// this can overflow and give -1, but IMO this is better than
// returning maxint64
if tv > math.MaxInt64 {
return 0, fmt.Errorf("could not convert %d to int64, would overflow", tv)
}

return int64(tv), nil
case reflect.Float32, reflect.Float64:
return int64(val.Float()), nil
Expand Down
15 changes: 6 additions & 9 deletions conv/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ func TestToInt64(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, int64(3), actual)

actual, err = ToInt64(uint64(math.MaxUint64))
require.NoError(t, err)
assert.Equal(t, int64(-1), actual)

actual, err = ToInt64(uint8(math.MaxUint8))
require.NoError(t, err)
assert.Equal(t, int64(0xFF), actual)
Expand Down Expand Up @@ -198,6 +194,9 @@ func TestToInt64(t *testing.T) {

_, err = ToInt64("foo")
require.Error(t, err)

_, err = ToInt64(uint64(math.MaxUint64))
require.Error(t, err)
})
}

Expand Down Expand Up @@ -226,10 +225,6 @@ func TestToInt(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 42, actual)

actual, err = ToInt(uint64(math.MaxUint64))
require.NoError(t, err)
assert.Equal(t, -1, actual)

actual, err = ToInt(uint8(math.MaxUint8))
require.NoError(t, err)
assert.Equal(t, 0xFF, actual)
Expand Down Expand Up @@ -267,6 +262,9 @@ func TestToInt(t *testing.T) {

_, err = ToInt("foo")
require.Error(t, err)

_, err = ToInt(uint64(math.MaxUint64))
require.Error(t, err)
})
}

Expand Down Expand Up @@ -408,7 +406,6 @@ func TestToString(t *testing.T) {
}

for _, d := range testdata {
d := d
t.Run(fmt.Sprintf("%T/%#v == %s", d.in, d.in, d.out), func(t *testing.T) {
out := ToString(d.in)
assert.Equal(t, d.out, out)
Expand Down
2 changes: 1 addition & 1 deletion conv/evalargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func printableValue(v reflect.Value) (interface{}, bool) {
}

if !v.Type().Implements(errorType) && !v.Type().Implements(fmtStringerType) {
if v.CanAddr() && (reflect.PtrTo(v.Type()).Implements(errorType) || reflect.PtrTo(v.Type()).Implements(fmtStringerType)) {
if v.CanAddr() && (reflect.PointerTo(v.Type()).Implements(errorType) || reflect.PointerTo(v.Type()).Implements(fmtStringerType)) {
v = v.Addr()
} else {
switch v.Kind() {
Expand Down
1 change: 0 additions & 1 deletion crypto/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func TestRSACrypt(t *testing.T) {
}

for _, d := range testdata {
d := d
t.Run(d.name, func(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 2 additions & 4 deletions docs-src/content/functions/conv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ funcs:
Converts the input to an `int64` (64-bit signed integer).
This function attempts to convert most types of input (strings, numbers,
and booleans), but behaviour when the input can not be converted is
undefined and subject to change.
and booleans).
Unconvertable inputs will result in errors.
Expand Down Expand Up @@ -388,8 +387,7 @@ funcs:
Converts the input to a `float64`.
This function attempts to convert most types of input (strings, numbers,
and booleans), but behaviour when the input can not be converted is
undefined and subject to change.
and booleans).
Unconvertable inputs will result in errors.
arguments:
Expand Down
6 changes: 2 additions & 4 deletions docs/content/functions/conv.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,7 @@ $ gomplate -i '{{ conv.ToBools false "blah" 0 }}'
Converts the input to an `int64` (64-bit signed integer).

This function attempts to convert most types of input (strings, numbers,
and booleans), but behaviour when the input can not be converted is
undefined and subject to change.
and booleans).

Unconvertable inputs will result in errors.

Expand Down Expand Up @@ -592,8 +591,7 @@ gomplate -i '{{ conv.ToInts true 0x42 "123,456.99" "1.2345e+3"}}'
Converts the input to a `float64`.

This function attempts to convert most types of input (strings, numbers,
and booleans), but behaviour when the input can not be converted is
undefined and subject to change.
and booleans).

Unconvertable inputs will result in errors.

Expand Down
15 changes: 10 additions & 5 deletions internal/cidr/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func SubnetBig(base netip.Prefix, newBits int, num *big.Int) (netip.Prefix, erro
return netip.Prefix{}, fmt.Errorf("insufficient address space to extend prefix of %d by %d", parentLen, newBits)
}

//nolint:gosec // G115 doesn't apply here
maxNetNum := uint64(1<<uint64(newBits)) - 1
if num.Uint64() > maxNetNum {
return netip.Prefix{}, fmt.Errorf("prefix extension of %d does not accommodate a subnet numbered %d", newBits, num)
Expand All @@ -50,17 +51,19 @@ func HostBig(base netip.Prefix, num *big.Int) (netip.Addr, error) {
hostLen := addrLen - parentLen

maxHostNum := big.NewInt(int64(1))

//nolint:gosec // G115 doesn't apply here
maxHostNum.Lsh(maxHostNum, uint(hostLen))
maxHostNum.Sub(maxHostNum, big.NewInt(1))

numUint64 := big.NewInt(int64(num.Uint64()))
num2 := big.NewInt(num.Int64())
if num.Cmp(big.NewInt(0)) == -1 {
numUint64.Neg(num)
numUint64.Sub(numUint64, big.NewInt(int64(1)))
num.Sub(maxHostNum, numUint64)
num2.Neg(num)
num2.Sub(num2, big.NewInt(int64(1)))
num.Sub(maxHostNum, num2)
}

if numUint64.Cmp(maxHostNum) == 1 {
if num2.Cmp(maxHostNum) == 1 {
return netip.Addr{}, fmt.Errorf("prefix of %d does not accommodate a host numbered %d", parentLen, num)
}

Expand Down Expand Up @@ -93,6 +96,8 @@ func intToIP(ipInt *big.Int, bits int) netip.Addr {

func insertNumIntoIP(ip netip.Addr, bigNum *big.Int, prefixLen int) netip.Addr {
ipInt, totalBits := ipToInt(ip)

//nolint:gosec // G115 isn't relevant here
bigNum.Lsh(bigNum, uint(totalBits-prefixLen))
ipInt.Or(ipInt, bigNum)
return intToIP(ipInt, totalBits)
Expand Down
1 change: 0 additions & 1 deletion internal/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ func TestApplyEnvVars(t *testing.T) {
}

for i, d := range data {
d := d
t.Run(fmt.Sprintf("applyEnvVars_%s_%s/%d", d.env, d.value, i), func(t *testing.T) {
t.Setenv(d.env, d.value)

Expand Down
1 change: 0 additions & 1 deletion internal/conv/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func BenchmarkInterfaceSlice(b *testing.B) {
}

for _, d := range data {
d := d
b.Run(fmt.Sprintf("%T(%v)", d, d), func(b *testing.B) {
for i := 0; i < b.N; i++ {
InterfaceSlice(d)
Expand Down
4 changes: 0 additions & 4 deletions internal/datafs/wdfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func TestResolveLocalPath_NonWindows(t *testing.T) {
}

for _, td := range testdata {
td := td
t.Run(td.path, func(t *testing.T) {
root, path, err := ResolveLocalPath(fsys, td.path)
require.NoError(t, err)
Expand Down Expand Up @@ -225,7 +224,6 @@ func TestResolveLocalPath_Windows(t *testing.T) {
}

for _, td := range testdata {
td := td
t.Run(td.path, func(t *testing.T) {
root, path, err := ResolveLocalPath(fsys, td.path)
require.NoError(t, err)
Expand Down Expand Up @@ -286,7 +284,6 @@ func TestWdFS_ResolveLocalPath_Windows(t *testing.T) {
}

for _, td := range testdata {
td := td
t.Run(td.path, func(t *testing.T) {
root, path, err := resolveLocalPath(volname, td.path)
require.NoError(t, err)
Expand Down Expand Up @@ -324,7 +321,6 @@ func TestWin32PathType(t *testing.T) {
}

for _, td := range testdata {
td := td
t.Run(td.path, func(t *testing.T) {
assert.Equal(t, td.expected, win32PathType(td.path))
})
Expand Down
1 change: 0 additions & 1 deletion internal/funcs/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func TestDefault(t *testing.T) {
}

for _, d := range data {
d := d
t.Run(fmt.Sprintf("%T/%#v empty==%v", d.val, d.val, d.empty), func(t *testing.T) {
t.Parallel()

Expand Down
9 changes: 0 additions & 9 deletions internal/funcs/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func TestIsIntFloatNum(t *testing.T) {

m := MathFuncs{}
for _, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("%T(%#v)", tt.in, tt.in), func(t *testing.T) {
t.Parallel()

Expand All @@ -239,7 +238,6 @@ func BenchmarkIsFloat(b *testing.B) {

m := MathFuncs{}
for _, n := range data {
n := n
b.Run(fmt.Sprintf("%T(%v)", n, n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
m.IsFloat(n)
Expand All @@ -264,7 +262,6 @@ func TestMax(t *testing.T) {
{int64(255), []interface{}{"14", "0xff", -5}},
}
for _, d := range data {
d := d
t.Run(fmt.Sprintf("%v==%v", d.n, d.expected), func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -308,7 +305,6 @@ func TestMin(t *testing.T) {
{int64(-5), []interface{}{"14", "0xff", -5}},
}
for _, d := range data {
d := d
t.Run(fmt.Sprintf("%v==%v", d.n, d.expected), func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -358,7 +354,6 @@ func TestContainsFloat(t *testing.T) {
{[]interface{}{"NaN"}, true},
}
for _, d := range data {
d := d
t.Run(fmt.Sprintf("%v==%v", d.n, d.expected), func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -386,7 +381,6 @@ func TestCeil(t *testing.T) {
{-1.9, -1},
}
for _, d := range data {
d := d
t.Run(fmt.Sprintf("%v==%v", d.n, d.a), func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -425,7 +419,6 @@ func TestFloor(t *testing.T) {
{-1.9, -2.},
}
for _, d := range data {
d := d
t.Run(fmt.Sprintf("%v==%v", d.n, d.a), func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -468,7 +461,6 @@ func TestRound(t *testing.T) {
{-4.5, -5},
}
for _, d := range data {
d := d
t.Run(fmt.Sprintf("%v==%v", d.n, d.a), func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -510,7 +502,6 @@ func TestAbs(t *testing.T) {
{-2, int64(2)},
}
for _, d := range data {
d := d
t.Run(fmt.Sprintf("%#v==%v", d.n, d.a), func(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 2 additions & 0 deletions internal/funcs/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ func (f *NetFuncs) CIDRNetmask(prefix interface{}) (netip.Addr, error) {
// fill an appropriately sized byte slice with as many 1s as prefix bits
b := make([]byte, p.Addr().BitLen()/8)
for i := 0; i < p.Bits(); i++ {
//nolint:gosec // G115 is not applicable, the value was checked at parse
// time
b[i/8] |= 1 << uint(7-i%8)
}

Expand Down
Loading

0 comments on commit 69d3e0c

Please sign in to comment.