-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
149 additions
and
22 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 |
---|---|---|
@@ -1,2 +1 @@ | ||
linters: | ||
enable-all: true | ||
linters: {} |
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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
repos: | ||
- repo: https://github.com/golangci/golangci-lint | ||
rev: v1.17.1 | ||
rev: v1.44.2 | ||
hooks: | ||
- id: golangci-lint | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.1.0 | ||
hooks: | ||
- id: trailing-whitespace |
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
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
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
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,52 @@ | ||
package analyzer | ||
|
||
import ( | ||
"flag" | ||
"go/ast" | ||
|
||
"github.com/ashanbrown/makezero/makezero" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
type analyzer struct { | ||
always bool | ||
} | ||
|
||
// NewAnalyzer returns a go/analysis-compatible analyzer | ||
// Set "-always" to report any non-empty slice initialization. | ||
func NewAnalyzer() *analysis.Analyzer { | ||
var flags flag.FlagSet | ||
a := analyzer{} | ||
flags.BoolVar(&a.always, "always", false, "report any non-empty slice initializations, regardless of intention") | ||
return &analysis.Analyzer{ | ||
Name: "makezero", | ||
Doc: "detect unintended non-empty slice initializations", | ||
Run: a.runAnalysis, | ||
Flags: flags, | ||
} | ||
} | ||
|
||
func (a *analyzer) runAnalysis(pass *analysis.Pass) (interface{}, error) { | ||
linter := makezero.NewLinter(a.always) | ||
nodes := make([]ast.Node, 0, len(pass.Files)) | ||
for _, f := range pass.Files { | ||
nodes = append(nodes, f) | ||
} | ||
issues, err := linter.Run(pass.Fset, pass.TypesInfo, nodes...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reportIssues(pass, issues) | ||
return nil, nil | ||
} | ||
|
||
func reportIssues(pass *analysis.Pass, issues []makezero.Issue) { | ||
for _, i := range issues { | ||
diag := analysis.Diagnostic{ | ||
Pos: i.Pos(), | ||
Message: i.Details(), | ||
Category: "restriction", | ||
} | ||
pass.Report(diag) | ||
} | ||
} |
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,24 @@ | ||
package analyzer_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ashanbrown/makezero/pkg/analyzer" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestAppend(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
a := analyzer.NewAnalyzer() | ||
analysistest.Run(t, testdata, a, "./append") | ||
} | ||
|
||
func TestAlways(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
a := analyzer.NewAnalyzer() | ||
err := a.Flags.Set("always", "true") | ||
if err != nil { | ||
t.Fatalf("expected no error but got %q", err) | ||
} | ||
analysistest.Run(t, testdata, a, "./always") | ||
} |
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,8 @@ | ||
package always | ||
|
||
import "fmt" | ||
|
||
func Foo() { | ||
x := make([]int, 5) // want "slice `x` does not have non-zero initial length" | ||
fmt.Println(x) | ||
} |
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,9 @@ | ||
package append | ||
|
||
import "fmt" | ||
|
||
func Foo() { | ||
x := make([]int, 5) | ||
x = append(x, 1) // want "append to slice `x` with non-zero initialized length" | ||
fmt.Println(x) | ||
} |