Skip to content

Commit

Permalink
Merge pull request #6 from karamaru-alpha/fix/karamaru/quit-init
Browse files Browse the repository at this point in the history
refactor: remove init function
  • Loading branch information
karamaru-alpha committed Mar 3, 2024
2 parents d5889e2 + 35f3e20 commit 4ee136b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/copyloopvar/main.go
Expand Up @@ -6,4 +6,4 @@ import (
"github.com/karamaru-alpha/copyloopvar"
)

func main() { singlechecker.Main(copyloopvar.Analyzer) }
func main() { singlechecker.Main(copyloopvar.NewAnalyzer()) }
26 changes: 12 additions & 14 deletions copyloopvar.go
Expand Up @@ -10,21 +10,19 @@ import (
"golang.org/x/tools/go/ast/inspector"
)

var Analyzer = &analysis.Analyzer{
Name: "copyloopvar",
Doc: "copyloopvar is a linter detects places where loop variables are copied",
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}

var (
ignoreAlias bool
)
var ignoreAlias bool

func init() {
Analyzer.Flags.BoolVar(&ignoreAlias, "ignore-alias", false, "ignore aliasing of loop variables")
func NewAnalyzer() *analysis.Analyzer {
analyzer := &analysis.Analyzer{
Name: "copyloopvar",
Doc: "copyloopvar is a linter detects places where loop variables are copied",
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
analyzer.Flags.BoolVar(&ignoreAlias, "ignore-alias", false, "ignore aliasing of loop variables")
return analyzer
}

func run(pass *analysis.Pass) (any, error) {
Expand Down
8 changes: 4 additions & 4 deletions copyloopvar_test.go
Expand Up @@ -10,15 +10,15 @@ import (
func TestAnalyzer(t *testing.T) {
t.Run("basic", func(t *testing.T) {
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
analysistest.Run(t, testdata, Analyzer, "basic")
analysistest.Run(t, testdata, NewAnalyzer(), "basic")
})

t.Run("ignore-alias", func(t *testing.T) {
err := Analyzer.Flags.Set("ignore-alias", "true")
if err != nil {
analyzer := NewAnalyzer()
if err := analyzer.Flags.Set("ignore-alias", "true"); err != nil {
t.Error(err)
}
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
analysistest.Run(t, testdata, Analyzer, "ignorealias")
analysistest.Run(t, testdata, analyzer, "ignorealias")
})
}

0 comments on commit 4ee136b

Please sign in to comment.