From 719e269cb19dccd54a85344ed65a6aadaf935cb6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 10 Apr 2019 21:39:19 -0700 Subject: [PATCH] minor lint fixes (#182) --- assert/assert.go | 4 ++-- compiler/instructions.go | 2 +- compiler/symbol_scopes.go | 6 +++--- objects/bytes.go | 2 +- objects/conversion.go | 2 +- objects/object_test.go | 12 ++++-------- runtime/vm.go | 4 +--- script/script.go | 9 --------- stdlib/fmt.go | 4 ++-- stdlib/times_test.go | 3 ++- 10 files changed, 17 insertions(+), 31 deletions(-) diff --git a/assert/assert.go b/assert/assert.go index 7fa9623..84d1aaa 100644 --- a/assert/assert.go +++ b/assert/assert.go @@ -106,7 +106,7 @@ func Equal(t *testing.T, expected, actual interface{}, msg ...interface{}) bool return failExpectedActual(t, expected, actual, msg...) } case []byte: - if bytes.Compare(expected, actual.([]byte)) != 0 { + if !bytes.Equal(expected, actual.([]byte)) { return failExpectedActual(t, string(expected), string(actual.([]byte)), msg...) } case []string: @@ -156,7 +156,7 @@ func Equal(t *testing.T, expected, actual interface{}, msg ...interface{}) bool case *objects.ImmutableArray: return equalObjectSlice(t, expected.Value, actual.(*objects.ImmutableArray).Value, msg...) case *objects.Bytes: - if bytes.Compare(expected.Value, actual.(*objects.Bytes).Value) != 0 { + if !bytes.Equal(expected.Value, actual.(*objects.Bytes).Value) { return failExpectedActual(t, string(expected.Value), string(actual.(*objects.Bytes).Value), msg...) } case *objects.Map: diff --git a/compiler/instructions.go b/compiler/instructions.go index 80c88d1..14dde1d 100644 --- a/compiler/instructions.go +++ b/compiler/instructions.go @@ -13,7 +13,7 @@ func MakeInstruction(opcode Opcode, operands ...int) []byte { totalLen += w } - instruction := make([]byte, totalLen, totalLen) + instruction := make([]byte, totalLen) instruction[0] = byte(opcode) offset := 1 diff --git a/compiler/symbol_scopes.go b/compiler/symbol_scopes.go index 15204b3..e0c0d94 100644 --- a/compiler/symbol_scopes.go +++ b/compiler/symbol_scopes.go @@ -6,7 +6,7 @@ type SymbolScope string // List of symbol scopes const ( ScopeGlobal SymbolScope = "GLOBAL" - ScopeLocal = "LOCAL" - ScopeBuiltin = "BUILTIN" - ScopeFree = "FREE" + ScopeLocal SymbolScope = "LOCAL" + ScopeBuiltin SymbolScope = "BUILTIN" + ScopeFree SymbolScope = "FREE" ) diff --git a/objects/bytes.go b/objects/bytes.go index 6710c7c..5159c22 100644 --- a/objects/bytes.go +++ b/objects/bytes.go @@ -57,7 +57,7 @@ func (o *Bytes) Equals(x Object) bool { return false } - return bytes.Compare(o.Value, t.Value) == 0 + return bytes.Equal(o.Value, t.Value) } // IndexGet returns an element (as Int) at a given index. diff --git a/objects/conversion.go b/objects/conversion.go index d7cb3a0..2751413 100644 --- a/objects/conversion.go +++ b/objects/conversion.go @@ -254,7 +254,7 @@ func FromInterface(v interface{}) (Object, error) { case []Object: return &Array{Value: v}, nil case []interface{}: - arr := make([]Object, len(v), len(v)) + arr := make([]Object, len(v)) for i, e := range v { vo, err := FromInterface(e) if err != nil { diff --git a/objects/object_test.go b/objects/object_test.go index 24ad009..31147ec 100644 --- a/objects/object_test.go +++ b/objects/object_test.go @@ -9,8 +9,7 @@ import ( ) func TestObject_TypeName(t *testing.T) { - var o objects.Object - o = &objects.Int{} + var o objects.Object = &objects.Int{} assert.Equal(t, "int", o.TypeName()) o = &objects.Float{} assert.Equal(t, "float", o.TypeName()) @@ -47,8 +46,7 @@ func TestObject_TypeName(t *testing.T) { } func TestObject_IsFalsy(t *testing.T) { - var o objects.Object - o = &objects.Int{Value: 0} + var o objects.Object = &objects.Int{Value: 0} assert.True(t, o.IsFalsy()) o = &objects.Int{Value: 1} assert.False(t, o.IsFalsy()) @@ -95,8 +93,7 @@ func TestObject_IsFalsy(t *testing.T) { } func TestObject_String(t *testing.T) { - var o objects.Object - o = &objects.Int{Value: 0} + var o objects.Object = &objects.Int{Value: 0} assert.Equal(t, "0", o.String()) o = &objects.Int{Value: 1} assert.Equal(t, "1", o.String()) @@ -135,8 +132,7 @@ func TestObject_String(t *testing.T) { } func TestObject_BinaryOp(t *testing.T) { - var o objects.Object - o = &objects.Char{} + var o objects.Object = &objects.Char{} _, err := o.BinaryOp(token.Add, objects.UndefinedValue) assert.Error(t, err) o = &objects.Bool{} diff --git a/runtime/vm.go b/runtime/vm.go index dde52db..68252aa 100644 --- a/runtime/vm.go +++ b/runtime/vm.go @@ -707,9 +707,7 @@ func (v *VM) run() { case objects.Callable: var args []objects.Object - for _, arg := range v.stack[v.sp-numArgs : v.sp] { - args = append(args, arg) - } + args = append(args, v.stack[v.sp-numArgs:v.sp]...) ret, e := callee.Call(args...) v.sp -= numArgs + 1 diff --git a/script/script.go b/script/script.go index cdf3671..2ee67b6 100644 --- a/script/script.go +++ b/script/script.go @@ -183,12 +183,3 @@ func (s *Script) prepCompile() (symbolTable *compiler.SymbolTable, globals []obj return } - -func (s *Script) copyVariables() map[string]*Variable { - vars := make(map[string]*Variable) - for n, v := range s.variables { - vars[n] = v - } - - return vars -} diff --git a/stdlib/fmt.go b/stdlib/fmt.go index 47d7845..a42a6ed 100644 --- a/stdlib/fmt.go +++ b/stdlib/fmt.go @@ -44,7 +44,7 @@ func fmtPrintf(args ...objects.Object) (ret objects.Object, err error) { return nil, nil } - formatArgs := make([]interface{}, numArgs-1, numArgs-1) + formatArgs := make([]interface{}, numArgs-1) for idx, arg := range args[1:] { switch arg := arg.(type) { case *objects.Int, *objects.Float, *objects.Bool, *objects.Char, *objects.String, *objects.Bytes: @@ -89,7 +89,7 @@ func fmtSprintf(args ...objects.Object) (ret objects.Object, err error) { return format, nil // okay to return 'format' directly as String is immutable } - formatArgs := make([]interface{}, numArgs-1, numArgs-1) + formatArgs := make([]interface{}, numArgs-1) for idx, arg := range args[1:] { switch arg := arg.(type) { case *objects.Int, *objects.Float, *objects.Bool, *objects.Char, *objects.String, *objects.Bytes: diff --git a/stdlib/times_test.go b/stdlib/times_test.go index 11551de..a1f7579 100644 --- a/stdlib/times_test.go +++ b/stdlib/times_test.go @@ -30,7 +30,8 @@ func TestTimes(t *testing.T) { module(t, "times").call("month_string", 12).expect("December") module(t, "times").call("date", 1982, 9, 28, 19, 21, 44, 999).expect(time1) - assert.True(t, module(t, "times").call("now").o.(*objects.Time).Value.Sub(time.Now()).Nanoseconds() < 100000000) // within 100ms + nowD := time.Until(module(t, "times").call("now").o.(*objects.Time).Value).Nanoseconds() + assert.True(t, 0 > nowD && nowD > -100000000) // within 100ms parsed, _ := time.Parse(time.RFC3339, "1982-09-28T19:21:44+07:00") module(t, "times").call("parse", time.RFC3339, "1982-09-28T19:21:44+07:00").expect(parsed) module(t, "times").call("unix", 1234325, 94493).expect(time.Unix(1234325, 94493))