Skip to content

Commit 502a3a6

Browse files
committed
analysis/facts: split each analysis into its own package
Closes gh-1029 (cherry picked from commit 9d57f24)
1 parent 87269a9 commit 502a3a6

File tree

19 files changed

+132
-115
lines changed

19 files changed

+132
-115
lines changed

analysis/code/code.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"go/types"
1111
"strings"
1212

13-
"honnef.co/go/tools/analysis/facts"
13+
"honnef.co/go/tools/analysis/facts/generated"
14+
"honnef.co/go/tools/analysis/facts/purity"
15+
"honnef.co/go/tools/analysis/facts/tokenfile"
1416
"honnef.co/go/tools/go/ast/astutil"
1517
"honnef.co/go/tools/go/types/typeutil"
1618
"honnef.co/go/tools/pattern"
@@ -195,7 +197,7 @@ func IsCallToAny(pass *analysis.Pass, node ast.Node, names ...string) bool {
195197
}
196198

197199
func File(pass *analysis.Pass, node Positioner) *ast.File {
198-
m := pass.ResultOf[facts.TokenFile].(map[*token.File]*ast.File)
200+
m := pass.ResultOf[tokenfile.Analyzer].(map[*token.File]*ast.File)
199201
return m[pass.Fset.File(node.Pos())]
200202
}
201203

@@ -208,9 +210,9 @@ func IsGenerated(pass *analysis.Pass, pos token.Pos) bool {
208210

209211
// Generator returns the generator that generated the file containing
210212
// pos. It ignores //line directives.
211-
func Generator(pass *analysis.Pass, pos token.Pos) (facts.Generator, bool) {
213+
func Generator(pass *analysis.Pass, pos token.Pos) (generated.Generator, bool) {
212214
file := pass.Fset.PositionFor(pos, false).Filename
213-
m := pass.ResultOf[facts.Generated].(map[string]facts.Generator)
215+
m := pass.ResultOf[generated.Analyzer].(map[string]generated.Generator)
214216
g, ok := m[file]
215217
return g, ok
216218
}
@@ -220,7 +222,7 @@ func Generator(pass *analysis.Pass, pos token.Pos) (facts.Generator, bool) {
220222
// syntactic check, meaning that any function call may have side
221223
// effects, regardless of the called function's body. Otherwise,
222224
// purity will be consulted to determine the purity of function calls.
223-
func MayHaveSideEffects(pass *analysis.Pass, expr ast.Expr, purity facts.PurityResult) bool {
225+
func MayHaveSideEffects(pass *analysis.Pass, expr ast.Expr, purity purity.Result) bool {
224226
switch expr := expr.(type) {
225227
case *ast.BadExpr:
226228
return true

analysis/facts/deprecated.go renamed to analysis/facts/deprecated/deprecated.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package facts
1+
package deprecated
22

33
import (
44
"go/ast"
@@ -15,17 +15,17 @@ type IsDeprecated struct{ Msg string }
1515
func (*IsDeprecated) AFact() {}
1616
func (d *IsDeprecated) String() string { return "Deprecated: " + d.Msg }
1717

18-
type DeprecatedResult struct {
18+
type Result struct {
1919
Objects map[types.Object]*IsDeprecated
2020
Packages map[*types.Package]*IsDeprecated
2121
}
2222

23-
var Deprecated = &analysis.Analyzer{
23+
var Analyzer = &analysis.Analyzer{
2424
Name: "fact_deprecated",
2525
Doc: "Mark deprecated objects",
2626
Run: deprecated,
2727
FactTypes: []analysis.Fact{(*IsDeprecated)(nil)},
28-
ResultType: reflect.TypeOf(DeprecatedResult{}),
28+
ResultType: reflect.TypeOf(Result{}),
2929
}
3030

3131
func deprecated(pass *analysis.Pass) (interface{}, error) {
@@ -129,7 +129,7 @@ func deprecated(pass *analysis.Pass) (interface{}, error) {
129129
ast.Inspect(f, fn)
130130
}
131131

132-
out := DeprecatedResult{
132+
out := Result{
133133
Objects: map[types.Object]*IsDeprecated{},
134134
Packages: map[*types.Package]*IsDeprecated{},
135135
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package deprecated
2+
3+
import (
4+
"testing"
5+
6+
"golang.org/x/tools/go/analysis/analysistest"
7+
)
8+
9+
func TestDeprecated(t *testing.T) {
10+
analysistest.Run(t, analysistest.TestData(), Analyzer, "Deprecated")
11+
}

analysis/facts/directives.go renamed to analysis/facts/directives/directives.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package facts
1+
package directives
22

33
import (
44
"reflect"
@@ -11,7 +11,7 @@ func directives(pass *analysis.Pass) (interface{}, error) {
1111
return lint.ParseDirectives(pass.Files, pass.Fset), nil
1212
}
1313

14-
var Directives = &analysis.Analyzer{
14+
var Analyzer = &analysis.Analyzer{
1515
Name: "directives",
1616
Doc: "extracts linter directives",
1717
Run: directives,

analysis/facts/facts_test.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

analysis/facts/generated.go renamed to analysis/facts/generated/generated.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package facts
1+
package generated
22

33
import (
44
"bufio"
@@ -78,7 +78,7 @@ func isGenerated(path string) (Generator, bool) {
7878
return 0, false
7979
}
8080

81-
var Generated = &analysis.Analyzer{
81+
var Analyzer = &analysis.Analyzer{
8282
Name: "isgenerated",
8383
Doc: "annotate file names that have been code generated",
8484
Run: func(pass *analysis.Pass) (interface{}, error) {

analysis/facts/purity.go renamed to analysis/facts/purity/purity.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package facts
1+
package purity
22

33
import (
44
"go/types"
@@ -16,15 +16,15 @@ type IsPure struct{}
1616
func (*IsPure) AFact() {}
1717
func (d *IsPure) String() string { return "is pure" }
1818

19-
type PurityResult map[*types.Func]*IsPure
19+
type Result map[*types.Func]*IsPure
2020

21-
var Purity = &analysis.Analyzer{
21+
var Analyzer = &analysis.Analyzer{
2222
Name: "fact_purity",
2323
Doc: "Mark pure functions",
2424
Run: purity,
2525
Requires: []*analysis.Analyzer{buildir.Analyzer},
2626
FactTypes: []analysis.Fact{(*IsPure)(nil)},
27-
ResultType: reflect.TypeOf(PurityResult{}),
27+
ResultType: reflect.TypeOf(Result{}),
2828
}
2929

3030
var pureStdlib = map[string]struct{}{
@@ -170,7 +170,7 @@ func purity(pass *analysis.Pass) (interface{}, error) {
170170
check(fn)
171171
}
172172

173-
out := PurityResult{}
173+
out := Result{}
174174
for _, fact := range pass.AllObjectFacts() {
175175
out[fact.Object.(*types.Func)] = fact.Fact.(*IsPure)
176176
}

analysis/facts/purity/purity_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package purity
2+
3+
import (
4+
"testing"
5+
6+
"golang.org/x/tools/go/analysis/analysistest"
7+
)
8+
9+
func TestPurity(t *testing.T) {
10+
analysistest.Run(t, analysistest.TestData(), Analyzer, "Purity")
11+
}

analysis/facts/token.go renamed to analysis/facts/tokenfile/token.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package facts
1+
package tokenfile
22

33
import (
44
"go/ast"
@@ -8,7 +8,7 @@ import (
88
"golang.org/x/tools/go/analysis"
99
)
1010

11-
var TokenFile = &analysis.Analyzer{
11+
var Analyzer = &analysis.Analyzer{
1212
Name: "tokenfileanalyzer",
1313
Doc: "creates a mapping of *token.File to *ast.File",
1414
Run: func(pass *analysis.Pass) (interface{}, error) {

analysis/report/report.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strconv"
1111
"strings"
1212

13-
"honnef.co/go/tools/analysis/facts"
13+
"honnef.co/go/tools/analysis/facts/generated"
1414
"honnef.co/go/tools/go/ast/astutil"
1515

1616
"golang.org/x/tools/go/analysis"
@@ -172,7 +172,7 @@ func Report(pass *analysis.Pass, node Positioner, message string, opts ...Option
172172

173173
file := DisplayPosition(pass.Fset, node.Pos()).Filename
174174
if cfg.FilterGenerated {
175-
m := pass.ResultOf[facts.Generated].(map[string]facts.Generator)
175+
m := pass.ResultOf[generated.Analyzer].(map[string]generated.Generator)
176176
if _, ok := m[file]; ok {
177177
return
178178
}

internal/sharedcheck/lint.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88

99
"honnef.co/go/tools/analysis/code"
1010
"honnef.co/go/tools/analysis/edit"
11-
"honnef.co/go/tools/analysis/facts"
11+
"honnef.co/go/tools/analysis/facts/generated"
12+
"honnef.co/go/tools/analysis/facts/tokenfile"
1213
"honnef.co/go/tools/analysis/report"
1314
"honnef.co/go/tools/go/ast/astutil"
1415
"honnef.co/go/tools/go/ir"
@@ -118,7 +119,7 @@ func RedundantTypeInDeclarationChecker(verb string, flagHelpfulTypes bool) *anal
118119
}
119120

120121
gen, _ := code.Generator(pass, decl.Pos())
121-
if gen == facts.Cgo {
122+
if gen == generated.Cgo {
122123
// TODO(dh): remove this exception once we can use UsesCgo
123124
return
124125
}
@@ -203,6 +204,6 @@ func RedundantTypeInDeclarationChecker(verb string, flagHelpfulTypes bool) *anal
203204

204205
return &analysis.Analyzer{
205206
Run: fn,
206-
Requires: []*analysis.Analyzer{facts.Generated, inspect.Analyzer, facts.TokenFile, facts.Generated},
207+
Requires: []*analysis.Analyzer{generated.Analyzer, inspect.Analyzer, tokenfile.Analyzer},
207208
}
208209
}

quickfix/analysis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package quickfix
22

33
import (
4-
"honnef.co/go/tools/analysis/facts"
4+
"honnef.co/go/tools/analysis/facts/tokenfile"
55
"honnef.co/go/tools/analysis/lint"
66
"honnef.co/go/tools/internal/sharedcheck"
77

@@ -40,7 +40,7 @@ var Analyzers = lint.InitializeAnalyzers(Docs, map[string]*analysis.Analyzer{
4040
},
4141
"QF1008": {
4242
Run: CheckExplicitEmbeddedSelector,
43-
Requires: []*analysis.Analyzer{inspect.Analyzer, facts.TokenFile},
43+
Requires: []*analysis.Analyzer{inspect.Analyzer, tokenfile.Analyzer},
4444
},
4545
"QF1009": {
4646
Run: CheckTimeEquality,

0 commit comments

Comments
 (0)