-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from dtcaciuc/analyzer
Provide go/analysis analyzer instance
- Loading branch information
Showing
15 changed files
with
438 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package errcheck | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/token" | ||
"reflect" | ||
"regexp" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
var Analyzer = &analysis.Analyzer{ | ||
Name: "errcheck", | ||
Doc: "check for unchecked errors", | ||
Run: runAnalyzer, | ||
ResultType: reflect.TypeOf(Result{}), | ||
} | ||
|
||
var ( | ||
argBlank bool | ||
argAsserts bool | ||
argExcludeFile string | ||
argExcludeOnly bool | ||
) | ||
|
||
func init() { | ||
Analyzer.Flags.BoolVar(&argBlank, "blank", false, "if true, check for errors assigned to blank identifier") | ||
Analyzer.Flags.BoolVar(&argAsserts, "assert", false, "if true, check for ignored type assertion results") | ||
Analyzer.Flags.StringVar(&argExcludeFile, "exclude", "", "Path to a file containing a list of functions to exclude from checking") | ||
Analyzer.Flags.BoolVar(&argExcludeOnly, "excludeonly", false, "Use only excludes from exclude file") | ||
} | ||
|
||
func runAnalyzer(pass *analysis.Pass) (interface{}, error) { | ||
|
||
exclude := map[string]bool{} | ||
if !argExcludeOnly { | ||
for _, name := range DefaultExcludedSymbols { | ||
exclude[name] = true | ||
} | ||
} | ||
if argExcludeFile != "" { | ||
excludes, err := ReadExcludes(argExcludeFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("Could not read exclude file: %v\n", err) | ||
} | ||
for _, name := range excludes { | ||
exclude[name] = true | ||
} | ||
} | ||
|
||
var allErrors []UncheckedError | ||
for _, f := range pass.Files { | ||
v := &visitor{ | ||
typesInfo: pass.TypesInfo, | ||
fset: pass.Fset, | ||
blank: argBlank, | ||
asserts: argAsserts, | ||
exclude: exclude, | ||
ignore: map[string]*regexp.Regexp{}, // deprecated & not used | ||
lines: make(map[string][]string), | ||
errors: nil, | ||
} | ||
|
||
ast.Walk(v, f) | ||
|
||
for _, err := range v.errors { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: token.Pos(int(f.Pos()) + err.Pos.Offset), | ||
Message: "unchecked error", | ||
}) | ||
} | ||
|
||
allErrors = append(allErrors, v.errors...) | ||
} | ||
|
||
return Result{UncheckedErrors: allErrors}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package errcheck | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"io/ioutil" | ||
"strings" | ||
) | ||
|
||
var ( | ||
// DefaultExcludedSymbols is a list of symbol names that are usually excluded from checks by default. | ||
// | ||
// Note, that they still need to be explicitly copied to Checker.Exclusions.Symbols | ||
DefaultExcludedSymbols = []string{ | ||
// bytes | ||
"(*bytes.Buffer).Write", | ||
"(*bytes.Buffer).WriteByte", | ||
"(*bytes.Buffer).WriteRune", | ||
"(*bytes.Buffer).WriteString", | ||
|
||
// fmt | ||
"fmt.Errorf", | ||
"fmt.Print", | ||
"fmt.Printf", | ||
"fmt.Println", | ||
"fmt.Fprint(*bytes.Buffer)", | ||
"fmt.Fprintf(*bytes.Buffer)", | ||
"fmt.Fprintln(*bytes.Buffer)", | ||
"fmt.Fprint(*strings.Builder)", | ||
"fmt.Fprintf(*strings.Builder)", | ||
"fmt.Fprintln(*strings.Builder)", | ||
"fmt.Fprint(os.Stderr)", | ||
"fmt.Fprintf(os.Stderr)", | ||
"fmt.Fprintln(os.Stderr)", | ||
|
||
// io | ||
"(*io.PipeReader).CloseWithError", | ||
"(*io.PipeWriter).CloseWithError", | ||
|
||
// math/rand | ||
"math/rand.Read", | ||
"(*math/rand.Rand).Read", | ||
|
||
// strings | ||
"(*strings.Builder).Write", | ||
"(*strings.Builder).WriteByte", | ||
"(*strings.Builder).WriteRune", | ||
"(*strings.Builder).WriteString", | ||
|
||
// hash | ||
"(hash.Hash).Write", | ||
} | ||
) | ||
|
||
// ReadExcludes reads an excludes file, a newline delimited file that lists | ||
// patterns for which to allow unchecked errors. | ||
// | ||
// Lines that start with two forward slashes are considered comments and are ignored. | ||
// | ||
func ReadExcludes(path string) ([]string, error) { | ||
var excludes []string | ||
|
||
buf, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
scanner := bufio.NewScanner(bytes.NewReader(buf)) | ||
|
||
for scanner.Scan() { | ||
name := scanner.Text() | ||
// Skip comments and empty lines. | ||
if strings.HasPrefix(name, "//") || name == "" { | ||
continue | ||
} | ||
excludes = append(excludes, name) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return excludes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package errcheck | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestReadExcludes(t *testing.T) { | ||
expectedExcludes := []string{ | ||
"hello()", | ||
"world()", | ||
} | ||
t.Logf("expectedExcludes: %#v", expectedExcludes) | ||
excludes, err := ReadExcludes("testdata/excludes.txt") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Logf("excludes: %#v", excludes) | ||
if !reflect.DeepEqual(expectedExcludes, excludes) { | ||
t.Fatal("excludes did not match expectedExcludes") | ||
} | ||
} | ||
|
||
func TestReadEmptyExcludes(t *testing.T) { | ||
excludes, err := ReadExcludes("testdata/empty_excludes.txt") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(excludes) != 0 { | ||
t.Fatalf("expected empty excludes, got %#v", excludes) | ||
} | ||
} | ||
|
||
func TestReadExcludesMissingFile(t *testing.T) { | ||
_, err := ReadExcludes("testdata/missing_file") | ||
if err == nil { | ||
t.Fatal("expected non-nil err, got nil") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(custom_exclude.ErrorMakerInterfaceWrapper).MakeNilError |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.