Skip to content

Commit

Permalink
internal/gogrep: allow $*_ (optnode) in valuespec type (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
quasilyte committed Oct 22, 2021
1 parent cc6b8c7 commit 028d651
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
10 changes: 8 additions & 2 deletions analyzer/testdata/src/regression/issue291.go
Expand Up @@ -3,21 +3,27 @@ package regression
type explicitIntType int

const (
ZeroExplicit explicitIntType = iota // want `\Qavoid use of iota for constant types`
ZeroExplicit explicitIntType = iota // want `\Qgood, have explicit type`
OneExplicit
TwoExplicit
)

type implicitIntType int

// Matched due to the #295.
const typedIotaWithoutGroup int = iota // want `\Qgood, have explicit type`

const (
ZeroImplicit = iota // want `\Qavoid use of iota for constant types`
ZeroImplicit = iota // want `\Qavoid use of iota without explicit type`
OneImplicit
TwoImplicit
)

type noIotaIntType int

// Matched due to the #295.
const iotaWithoutGroup = iota // want `\Qavoid use of iota without explicit type`

const (
ZeroNoIota = 0
OneNoIota = 1
Expand Down
9 changes: 7 additions & 2 deletions analyzer/testdata/src/regression/rules.go
Expand Up @@ -35,8 +35,13 @@ func issue192(m dsl.Matcher) {
}

func issue291(m dsl.Matcher) {
m.Match(`$_ $_ = $iota`, `const ( $_ = $iota; $*_ )`).
m.Match(`const ( $_ = $iota; $*_ )`).
Where(m["iota"].Text == "iota").
At(m["iota"]).
Report("avoid use of iota for constant types")
Report("avoid use of iota without explicit type")

m.Match(`const ( $_ $_ = $iota; $*_ )`).
Where(m["iota"].Text == "iota").
At(m["iota"]).
Report("good, have explicit type")
}
2 changes: 1 addition & 1 deletion internal/gogrep/compile.go
Expand Up @@ -231,7 +231,7 @@ func (c *compiler) compileValueSpec(spec *ast.ValueSpec) {
}
c.emitInstOp(opEnd)
if spec.Type != nil {
c.compileExpr(spec.Type)
c.compileOptExpr(spec.Type)
}
if len(spec.Values) != 0 {
for _, v := range spec.Values {
Expand Down
11 changes: 11 additions & 0 deletions internal/gogrep/compile_test.go
Expand Up @@ -356,6 +356,17 @@ func TestCompileWildcard(t *testing.T) {
` • Ident s`,
` • OptNode`,
},

`const $_ $*_ = iota`: {
`ConstDecl`,
` • TypedValueInitSpec`,
` • • Node`,
` • • End`,
` • • OptNode`,
` • • Ident iota`,
` • • End`,
` • End`,
},
})

for i := range tests {
Expand Down
2 changes: 1 addition & 1 deletion internal/gogrep/match.go
Expand Up @@ -529,7 +529,7 @@ func (m *matcher) matchNodeWithInst(state *MatcherState, inst instruction, n ast
m.matchIdentSlice(state, n.Names) && m.matchNode(state, n.Type)
case opTypedValueInitSpec:
n, ok := n.(*ast.ValueSpec)
return ok && len(n.Values) != 0 && n.Type != nil &&
return ok && len(n.Values) != 0 &&
m.matchIdentSlice(state, n.Names) && m.matchNode(state, n.Type) && m.matchExprSlice(state, n.Values)

case opTypeSpec:
Expand Down
8 changes: 8 additions & 0 deletions internal/gogrep/match_test.go
Expand Up @@ -803,6 +803,14 @@ func TestMatch(t *testing.T) {
{`func $_($_) {}`, 0, `package p; func f() {}`},

// Gen decl.
{`const $_ $*_ = iota`, 1, `const foo = iota`},
{`const $_ $*_ = iota`, 1, `const foo int = iota`},
{`const $_ $*_ = iota`, 1, `const (foo = iota)`},
{`const $_ $*_ = iota`, 1, `const (foo int = iota)`},
{`const $_ $*_ = iota`, 0, `const foo int = 0`},
{`const $_ $*_ = iota`, 0, `var foo = iota`},
{`const $_ $_ = iota`, 1, `const foo int = iota`},
{`const $_ $_ = iota`, 0, `const foo = iota`},
{`const $x = $y`, 1, `const a = b`},
{`const $x = $y`, 1, `const (a = b)`},
{`const $x = $y`, 0, "const (a = b\nc = d)"},
Expand Down

0 comments on commit 028d651

Please sign in to comment.