Add linter checks to GHA & Fix code to pass lints

Add linter checks to GHA & Fix code to pass lints.
The linters added are:
- whitespace: Tool for detection of leading and trailing whitespace
- gosec: Inspects source code for security problems
- gci: Gci control golang package import order and make it always deterministic
- misspell: Finds commonly misspelled English words in comments
- gomnd: An analyzer to detect magic numbers

The file utils/utils.go is excluded from the checks of gomnd, as uses magic numbers through the code
This commit is contained in:
arnaucube
2020-08-27 18:10:05 +02:00
parent fe8431edfa
commit fd1e9c25ee
27 changed files with 95 additions and 75 deletions

View File

@@ -1,3 +1,4 @@
//nolint:gomnd
package utils
import (
@@ -33,9 +34,7 @@ func (fl16 *Float16) BigInt() *big.Int {
res := m.Mul(m, exp)
if e5 != 0 && e.Cmp(big.NewInt(0)) != 0 {
res.Add(res, exp.Div(exp, big.NewInt(2)))
}
return res
}
@@ -56,11 +55,9 @@ func floorFix2Float(_f *big.Int) Float16 {
s := big.NewInt(0).Rsh(m, 10)
for s.Cmp(zero) != 0 {
m.Div(m, ten)
s.Rsh(m, 10)
e++
}
return Float16(m.Int64() | e<<11)
@@ -98,13 +95,11 @@ func NewFloat16(f *big.Int) (Float16, error) {
d3 := big.NewInt(0).Abs(fi3.Sub(fi3, f))
if d.Cmp(d3) == 1 {
res = fl3
}
// Do rounding check
if res.BigInt().Cmp(f) == 0 {
return res, nil
}
return res, ErrRoundingLoss

View File

@@ -8,7 +8,6 @@ import (
)
func TestConversions(t *testing.T) {
testVector := map[Float16]string{
0x307B: "123000000",
0x1DC6: "454500",
@@ -24,7 +23,6 @@ func TestConversions(t *testing.T) {
}
for test := range testVector {
fix := test.BigInt()
assert.Equal(t, fix.String(), testVector[test])
@@ -37,13 +35,10 @@ func TestConversions(t *testing.T) {
fx2 := fl.BigInt()
assert.Equal(t, fx2.String(), testVector[test])
}
}
func TestFloorFix2Float(t *testing.T) {
testVector := map[string]Float16{
"87999990000000000": 0x776f,
"87950000000000001": 0x776f,
@@ -52,16 +47,13 @@ func TestFloorFix2Float(t *testing.T) {
}
for test := range testVector {
bi := big.NewInt(0)
bi.SetString(test, 10)
testFloat := NewFloat16Floor(bi)
assert.Equal(t, testFloat, testVector[test])
}
}
func TestConversionLosses(t *testing.T) {
@@ -94,7 +86,6 @@ func TestConversionLosses(t *testing.T) {
assert.Equal(t, ErrRoundingLoss, err)
c = b.BigInt()
assert.NotEqual(t, c, a)
}
func BenchmarkFloat16(b *testing.B) {