-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework linter to use go/analysis - go's static analysis interface Move test data to for convenient use of analysistest
- Loading branch information
Showing
9 changed files
with
138 additions
and
223 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package errorlint | ||
|
||
import ( | ||
"flag" | ||
"sort" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
func NewAnalyzer() *analysis.Analyzer { | ||
return &analysis.Analyzer{ | ||
Name: "errorlint", | ||
Doc: "Source code linter for Go software that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.", | ||
Run: run, | ||
Flags: flagSet, | ||
} | ||
} | ||
|
||
var ( | ||
flagSet flag.FlagSet | ||
checkErrorf bool | ||
) | ||
|
||
func init() { | ||
flagSet.BoolVar(&checkErrorf, "errorf", false, "Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats") | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
lints := []Lint{} | ||
if checkErrorf { | ||
l := LintFmtErrorfCalls(pass.Fset, *pass.TypesInfo) | ||
lints = append(lints, l...) | ||
} | ||
l := LintErrorComparisons(pass.Fset, *pass.TypesInfo) | ||
lints = append(lints, l...) | ||
l = LintErrorTypeAssertions(pass.Fset, *pass.TypesInfo) | ||
lints = append(lints, l...) | ||
sort.Sort(ByPosition(lints)) | ||
|
||
for _, l := range lints { | ||
pass.Report(analysis.Diagnostic{Pos: l.Pos, Message: l.Message}) | ||
} | ||
return nil, 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,25 @@ | ||
package errorlint | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestErrorsAs(t *testing.T) { | ||
analysistest.Run(t, analysistest.TestData(), NewAnalyzer(), "errorsas") | ||
} | ||
|
||
func TestErrorsIs(t *testing.T) { | ||
analysistest.Run(t, analysistest.TestData(), NewAnalyzer(), "errorsis") | ||
} | ||
|
||
func TestFmtErrorf(t *testing.T) { | ||
analyzer := NewAnalyzer() | ||
err := analyzer.Flags.Set("errorf", "true") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
analysistest.Run(t, analysistest.TestData(), analyzer, "fmterrorf") | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.