From 313302b9c8ba7e34a2c0df36f16bb5a3a4daca0e Mon Sep 17 00:00:00 2001
From: Eduard Urbach <admin@akyoto.dev>
Date: Thu, 30 Jan 2025 22:23:38 +0100
Subject: [PATCH] Improved code style

---
 src/asm/MemoryLabel.go            | 2 +-
 src/expression/Expression_test.go | 7 ++-----
 src/expression/bench_test.go      | 2 +-
 src/macho/MachO.go                | 4 ++--
 src/pe/Constants.go               | 2 +-
 src/pe/EXE.go                     | 6 +++---
 src/token/Count.go                | 2 +-
 src/token/List_test.go            | 4 ++--
 src/token/bench_test.go           | 2 +-
 src/x64/memoryAccessDynamic.go    | 4 +---
 tests/examples_test.go            | 2 +-
 tests/programs_test.go            | 2 +-
 12 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/src/asm/MemoryLabel.go b/src/asm/MemoryLabel.go
index 96850a9..0d4ebf7 100644
--- a/src/asm/MemoryLabel.go
+++ b/src/asm/MemoryLabel.go
@@ -6,8 +6,8 @@ import (
 
 // MemoryLabel operates with a memory address and a number.
 type MemoryLabel struct {
-	Address Memory
 	Label   string
+	Address Memory
 }
 
 // String returns a human readable version.
diff --git a/src/expression/Expression_test.go b/src/expression/Expression_test.go
index 1e03ba8..64a09ba 100644
--- a/src/expression/Expression_test.go
+++ b/src/expression/Expression_test.go
@@ -1,7 +1,7 @@
 package expression_test
 
 import (
-	"fmt"
+	"errors"
 	"testing"
 
 	"git.akyoto.dev/cli/q/src/expression"
@@ -110,14 +110,11 @@ func TestParse(t *testing.T) {
 	}
 
 	for _, test := range tests {
-		test := test
-
 		t.Run(test.Name, func(t *testing.T) {
 			src := []byte(test.Expression)
 			tokens := token.Tokenize(src)
 			expr := expression.Parse(tokens)
 			defer expr.Reset()
-
 			assert.NotNil(t, expr)
 			assert.Equal(t, expr.String(src), test.Result)
 		})
@@ -150,7 +147,7 @@ func TestEachLeaf(t *testing.T) {
 	assert.DeepEqual(t, leaves, []string{"1", "2", "3", "4", "5", "6", "7", "8"})
 
 	err = expr.EachLeaf(func(leaf *expression.Expression) error {
-		return fmt.Errorf("error")
+		return errors.New("error")
 	})
 
 	assert.NotNil(t, err)
diff --git a/src/expression/bench_test.go b/src/expression/bench_test.go
index c6266c3..ec63729 100644
--- a/src/expression/bench_test.go
+++ b/src/expression/bench_test.go
@@ -11,7 +11,7 @@ func BenchmarkExpression(b *testing.B) {
 	src := []byte("(1+2-3*4)+(5*6-7+8)")
 	tokens := token.Tokenize(src)
 
-	for i := 0; i < b.N; i++ {
+	for range b.N {
 		expression.Parse(tokens)
 	}
 }
diff --git a/src/macho/MachO.go b/src/macho/MachO.go
index 21b5b19..c463694 100644
--- a/src/macho/MachO.go
+++ b/src/macho/MachO.go
@@ -117,8 +117,8 @@ func Write(writer io.Writer, code []byte, data []byte) {
 	binary.Write(writer, binary.LittleEndian, &m.DataHeader)
 	binary.Write(writer, binary.LittleEndian, &m.UnixThread)
 
-	writer.Write(bytes.Repeat([]byte{0x00}, int(codePadding)))
+	writer.Write(bytes.Repeat([]byte{0x00}, codePadding))
 	writer.Write(code)
-	writer.Write(bytes.Repeat([]byte{0x00}, int(dataPadding)))
+	writer.Write(bytes.Repeat([]byte{0x00}, dataPadding))
 	writer.Write(data)
 }
diff --git a/src/pe/Constants.go b/src/pe/Constants.go
index c77bea7..00b470e 100644
--- a/src/pe/Constants.go
+++ b/src/pe/Constants.go
@@ -31,7 +31,7 @@ const (
 	IMAGE_FILE_EXECUTABLE_IMAGE        = 0x0002
 	IMAGE_FILE_LINE_NUMS_STRIPPED      = 0x0004
 	IMAGE_FILE_LOCAL_SYMS_STRIPPED     = 0x0008
-	IMAGE_FILE_AGGRESIVE_WS_TRIM       = 0x0010
+	IMAGE_FILE_AGGRESSIVE_WS_TRIM      = 0x0010
 	IMAGE_FILE_LARGE_ADDRESS_AWARE     = 0x0020
 	IMAGE_FILE_BYTES_REVERSED_LO       = 0x0080
 	IMAGE_FILE_32BIT_MACHINE           = 0x0100
diff --git a/src/pe/EXE.go b/src/pe/EXE.go
index ea856b5..238b3a7 100644
--- a/src/pe/EXE.go
+++ b/src/pe/EXE.go
@@ -189,11 +189,11 @@ func Write(writer io.Writer, code []byte, data []byte, dlls dll.List) {
 	binary.Write(writer, binary.LittleEndian, &pe.OptionalHeader64)
 	binary.Write(writer, binary.LittleEndian, &pe.Sections)
 
-	writer.Write(bytes.Repeat([]byte{0x00}, int(codePadding)))
+	writer.Write(bytes.Repeat([]byte{0x00}, codePadding))
 	writer.Write(code)
-	writer.Write(bytes.Repeat([]byte{0x00}, int(dataPadding)))
+	writer.Write(bytes.Repeat([]byte{0x00}, dataPadding))
 	writer.Write(data)
-	writer.Write(bytes.Repeat([]byte{0x00}, int(importsPadding)))
+	writer.Write(bytes.Repeat([]byte{0x00}, importsPadding))
 	binary.Write(writer, binary.LittleEndian, &imports)
 	binary.Write(writer, binary.LittleEndian, &dllData)
 	binary.Write(writer, binary.LittleEndian, &dllImports)
diff --git a/src/token/Count.go b/src/token/Count.go
index cf460a3..3dbcd38 100644
--- a/src/token/Count.go
+++ b/src/token/Count.go
@@ -5,7 +5,7 @@ func Count(tokens []Token, buffer []byte, kind Kind, name string) uint8 {
 	count := uint8(0)
 
 	for _, t := range tokens {
-		if t.Kind == Identifier && t.Text(buffer) == name {
+		if t.Kind == kind && t.Text(buffer) == name {
 			count++
 		}
 	}
diff --git a/src/token/List_test.go b/src/token/List_test.go
index 71fbf84..249ca17 100644
--- a/src/token/List_test.go
+++ b/src/token/List_test.go
@@ -1,7 +1,7 @@
 package token_test
 
 import (
-	"fmt"
+	"errors"
 	"testing"
 
 	"git.akyoto.dev/cli/q/src/token"
@@ -32,7 +32,7 @@ func TestSplit(t *testing.T) {
 	assert.DeepEqual(t, parameters, []string{"1+2", "3*4", "5*6", "7+8"})
 
 	err = tokens.Split(func(parameter token.List) error {
-		return fmt.Errorf("error")
+		return errors.New("error")
 	})
 
 	assert.NotNil(t, err)
diff --git a/src/token/bench_test.go b/src/token/bench_test.go
index acc6359..a502019 100644
--- a/src/token/bench_test.go
+++ b/src/token/bench_test.go
@@ -19,7 +19,7 @@ func bench(n int) func(b *testing.B) {
 	return func(b *testing.B) {
 		input := bytes.Repeat(line, n)
 
-		for i := 0; i < b.N; i++ {
+		for range b.N {
 			token.Tokenize(input)
 		}
 	}
diff --git a/src/x64/memoryAccessDynamic.go b/src/x64/memoryAccessDynamic.go
index 8400351..c9e323c 100644
--- a/src/x64/memoryAccessDynamic.go
+++ b/src/x64/memoryAccessDynamic.go
@@ -18,9 +18,7 @@ func memoryAccessDynamic(code []byte, opCode8 byte, opCode32 byte, destination c
 	}
 
 	if offset == RSP {
-		tmp := offset
-		offset = destination
-		destination = tmp
+		offset, destination = destination, offset
 	}
 
 	if numBytes == 8 {
diff --git a/tests/examples_test.go b/tests/examples_test.go
index bb9e4fe..f889c1d 100644
--- a/tests/examples_test.go
+++ b/tests/examples_test.go
@@ -48,7 +48,7 @@ func BenchmarkExamples(b *testing.B) {
 		b.Run(test.Name, func(b *testing.B) {
 			compiler := build.New(filepath.Join("..", "examples", test.Name))
 
-			for i := 0; i < b.N; i++ {
+			for range b.N {
 				_, err := compiler.Run()
 				assert.Nil(b, err)
 			}
diff --git a/tests/programs_test.go b/tests/programs_test.go
index dfeba4d..23fc44a 100644
--- a/tests/programs_test.go
+++ b/tests/programs_test.go
@@ -82,7 +82,7 @@ func BenchmarkPrograms(b *testing.B) {
 		b.Run(test.Name, func(b *testing.B) {
 			compiler := build.New(filepath.Join("programs", test.Name+".q"))
 
-			for i := 0; i < b.N; i++ {
+			for range b.N {
 				_, err := compiler.Run()
 				assert.Nil(b, err)
 			}