Repository: benhoyt/goawk
Pull request #224 (2024-03-09 02:54:19)

Fix a few minor things staticcheck found
No there are no errors with the following command:

    staticcheck -checks=inherit,-ST1001 ./...

ST1001 is "should not use dot imports".


Patch:
diff --git a/goawk.go b/goawk.go
index cfea158..c80b59f 100644
--- a/goawk.go
+++ b/goawk.go
@@ -366,8 +366,10 @@ argsLoop:
 
 	// Run the program!
 	interpreter, err := interp.New(prog)
+	if err != nil {
+		errorExit(err)
+	}
 	status, err := interpreter.Execute(config)
-
 	if err != nil {
 		errorExit(err)
 	}
diff --git a/internal/resolver/toposort.go b/internal/resolver/toposort.go
index 745b012..311ae3e 100644
--- a/internal/resolver/toposort.go
+++ b/internal/resolver/toposort.go
@@ -57,7 +57,6 @@ func topoSort(graph map[string]map[string]struct{}) []string {
 		permMarks[n] = struct{}{}
 		delete(unmarked, n)
 		sorted = append(sorted, n)
-		return
 	}
 
 	for len(unmarked) > 0 {
diff --git a/interp/io.go b/interp/io.go
index 2652ae5..51a8b39 100644
--- a/interp/io.go
+++ b/interp/io.go
@@ -646,6 +646,7 @@ func (p *interp) ensureFields() {
 
 	switch {
 	case p.inputMode == CSVMode || p.inputMode == TSVMode:
+		// Normally fields have already been parsed by csvSplitter
 		if p.reparseCSV {
 			scanner := bufio.NewScanner(strings.NewReader(p.line))
 			scanner.Buffer(nil, maxRecordLength)
@@ -659,8 +660,6 @@ func (p *interp) ensureFields() {
 			if !scanner.Scan() {
 				p.fields = nil
 			}
-		} else {
-			// Normally fields have already been parsed by csvSplitter
 		}
 	case p.fieldSep == " ":
 		// FS space (default) means split fields on any whitespace
diff --git a/interp/iostream.go b/interp/iostream.go
index 572cdf6..34bca21 100644
--- a/interp/iostream.go
+++ b/interp/iostream.go
@@ -16,7 +16,7 @@ const (
 )
 
 var (
-	doubleCloseError = errors.New("close: stream already closed")
+	errDoubleClose = errors.New("close: stream already closed")
 )
 
 // firstError returns the first non-nil error or nil if all errors are nil.
@@ -89,7 +89,7 @@ func newOutFileStream(wc io.WriteCloser, size int) outputStream {
 
 func (s *outFileStream) Close() error {
 	if s.closed {
-		return doubleCloseError
+		return errDoubleClose
 	}
 	s.closed = true
 	flushErr := s.Writer.Flush()
@@ -130,7 +130,7 @@ func newOutCmdStream(cmd *exec.Cmd) (outputStream, error) {
 
 func (s *outCmdStream) Close() error {
 	if s.closed {
-		return doubleCloseError
+		return errDoubleClose
 	}
 	s.closed = true
 	flushErr := s.Writer.Flush()
@@ -154,7 +154,7 @@ func newOutNullStream() outputStream { return &outNullStream{io.Discard, false}
 func (s outNullStream) Flush() error { return nil }
 func (s *outNullStream) Close() error {
 	if s.closed {
-		return doubleCloseError
+		return errDoubleClose
 	}
 	s.closed = true
 	return nil
@@ -173,7 +173,7 @@ func newInFileStream(rc io.ReadCloser) inputStream {
 
 func (s *inFileStream) Close() error {
 	if s.closed {
-		return doubleCloseError
+		return errDoubleClose
 	}
 	s.closed = true
 	if err := s.ReadCloser.Close(); err != nil {
@@ -210,7 +210,7 @@ func newInCmdStream(cmd *exec.Cmd) (inputStream, error) {
 
 func (s *inCmdStream) Close() error {
 	if s.closed {
-		return doubleCloseError
+		return errDoubleClose
 	}
 	s.closed = true
 	closeErr := s.ReadCloser.Close()
diff --git a/parser/parser.go b/parser/parser.go
index ebd31cb..cf74983 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -158,7 +158,7 @@ func (p *parser) program() *ast.Program {
 		p.optionalNewlines()
 		switch p.tok {
 		case EOF:
-			break
+			// End of file
 		case BEGIN:
 			p.next()
 			prog.Begin = append(prog.Begin, p.stmtsBrace())


Test patch:
diff --git a/goawk_test.go b/goawk_test.go
index 77679a8..d2c0ec3 100644
--- a/goawk_test.go
+++ b/goawk_test.go
@@ -483,11 +483,6 @@ func runAWKs(t *testing.T, testArgs []string, testStdin, testOutput, testError s
 
 	t.Run("awk", func(t *testing.T) {
 		t.Helper()
-		var args []string
-		if strings.Contains(awkExe, "gawk") {
-			args = append(args, "--posix")
-		}
-		args = append(args, testArgs...)
 		cmd := exec.Command(awkExe, testArgs...)
 		if runtime.GOOS != "windows" {
 			cmd.Env = []string{"LC_ALL=en_US.UTF-8"}
diff --git a/internal/parseutil/filereader_test.go b/internal/parseutil/filereader_test.go
index 24c13ad..19f5f4d 100644
--- a/internal/parseutil/filereader_test.go
+++ b/internal/parseutil/filereader_test.go
@@ -129,7 +129,7 @@ print x
 			// test result source
 			source := string(fr.Source())
 			for _, file := range tst.files {
-				if strings.Index(source, file.source) < 0 {
+				if !strings.Contains(source, file.source) {
 					t.Errorf("Source() is incorrect")
 				}
 			}
diff --git a/interp/fuzz_unexported_test.go b/interp/fuzz_unexported_test.go
index abd3a75..bf22323 100644
--- a/interp/fuzz_unexported_test.go
+++ b/interp/fuzz_unexported_test.go
@@ -68,7 +68,7 @@ func parseFloatHelper(s string) (float64, error) {
 	if s == "+nan" || s == "-nan" {
 		return math.NaN(), nil
 	}
-	if strings.Contains(s, "0x") && strings.IndexAny(s, "pP") < 0 {
+	if strings.Contains(s, "0x") && !strings.ContainsAny(s, "pP") {
 		s += "p0"
 	}
 	return strconv.ParseFloat(s, 64)
diff --git a/interp/interp_test.go b/interp/interp_test.go
index 630aff0..9079aaf 100644
--- a/interp/interp_test.go
+++ b/interp/interp_test.go
@@ -1388,7 +1388,7 @@ BEGIN {
 			}},
 		{`BEGIN { 8 }`, "", "", `native function "r" returns more than two values`,
 			map[string]interface{}{
-				"r": func() (int, error, int) { return 0, nil, 0 },
+				"r": func() (int, int, int) { return 0, 0, 0 },
 			}},
 		{`BEGIN { print f(), f(1, 2) }`, "", "", `parse error at 1:20: "f" called with more arguments than declared`,
 			map[string]interface{}{
diff --git a/interp/iostream_test.go b/interp/iostream_test.go
index 8955f36..59c7cc4 100644
--- a/interp/iostream_test.go
+++ b/interp/iostream_test.go
@@ -64,7 +64,7 @@ func checkDoubleClose(t *testing.T, sc streamCloser) {
 		t.Fatal(err)
 	}
 	exitCode := sc.ExitCode()
-	if err := sc.Close(); !errors.Is(err, doubleCloseError) {
+	if err := sc.Close(); !errors.Is(err, errDoubleClose) {
 		t.Error("expected stream.Close() to return error on double close")
 	}
 	if sc.ExitCode() != exitCode {