Skip to content

Commit

Permalink
small fixes on ioutil and error handling (#45)
Browse files Browse the repository at this point in the history
* stop use ioutil - this package is deprecated
* fix some error handling
  • Loading branch information
peczenyj committed Apr 18, 2024
1 parent f651c09 commit b7ec239
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func TestEnd2End(t *testing.T) {

var ctxt context
ctxt.paths = []string{rel}
ctxt.initCheckers()
_ = ctxt.initCheckers()
if err := ctxt.collectAllCandidates(); err != nil {
t.Fatalf("collect candidates: %v", err)
}
ctxt.assignSuggestions()
_ = ctxt.assignSuggestions()
visitWarnings(&ctxt, func(pos token.Position, v *opVariant) {
text := v.op.name + ": " + v.op.suggested.warning
mlist, ok := f.Matchers[pos.Line]
Expand Down
10 changes: 5 additions & 5 deletions internal/end2end/end2end.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"fmt"
"go/token"
"io"
"io/ioutil"
"os"
"regexp"
)

// ParseTestFile parses file at specified path using the parser with default settings.
func ParseTestFile(filename string) (*TestFile, error) {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("read test file: %v", err)
return nil, fmt.Errorf("read test file: %w", err)
}
var p TestParser
return p.parseFile(filename, data)
Expand Down Expand Up @@ -82,9 +82,9 @@ type TestParser struct {

// ParseFile parses everything from r using filename to make associations.
func (p *TestParser) ParseFile(filename string, r io.Reader) (*TestFile, error) {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("read test file: %v", err)
return nil, fmt.Errorf("read test file: %w", err)
}
return p.parseFile(filename, data)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/end2end/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (p *TestParser) parseFile(filename string, data []byte) (*TestFile, error)
case "~":
re, err := regexp.Compile(text)
if err != nil {
return nil, fmt.Errorf("%s:%d: %v", filename, line, err)
return nil, fmt.Errorf("%s:%d: %w", filename, line, err)
}
m.re = re
case "=":
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"flag"
"fmt"
"go/ast"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (ctxt *context) parseFlags() error {

ctxt.flags.targets = flag.Args()
if len(ctxt.flags.targets) == 0 {
return fmt.Errorf("not enough positional args (empty targets list)")
return errors.New("not enough positional args (empty targets list)")
}

if ctxt.flags.shorterErrLocation {
Expand All @@ -106,13 +107,13 @@ func (ctxt *context) parseFlags() error {
func (ctxt *context) resolveTargets() error {
ctxt.paths = gotool.ImportPaths(ctxt.flags.targets)
if len(ctxt.paths) == 0 {
return fmt.Errorf("targets resolved to an empty import paths list")
return errors.New("targets resolved to an empty import paths list")
}

// Filter-out packages using the exclude pattern.
excludeRE, err := regexp.Compile(ctxt.flags.exclude)
if err != nil {
return fmt.Errorf("compiling -exclude regexp: %v", err)
return fmt.Errorf("compiling -exclude regexp: %w", err)
}
paths := ctxt.paths[:0]
for _, path := range ctxt.paths {
Expand Down Expand Up @@ -179,7 +180,7 @@ func (ctxt *context) collectAllCandidates() error {
for _, path := range ctxt.paths {
ctxt.infoPrintf("check %q", path)
if err := ctxt.collectPathCandidates(path); err != nil {
return fmt.Errorf("%s: %v", path, err)
return fmt.Errorf("%s: %w", path, err)
}
}
return nil
Expand Down

0 comments on commit b7ec239

Please sign in to comment.