Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for matching iota with implicit types #291

Closed
timkral opened this issue Oct 20, 2021 · 2 comments
Closed

Support for matching iota with implicit types #291

timkral opened this issue Oct 20, 2021 · 2 comments

Comments

@timkral
Copy link
Contributor

timkral commented Oct 20, 2021

I'm looking to find instances in which we are using the iota feature within golang. There are two general cases that I see available:

  1. Explicit Types
type myType int
const (
  ZERO myType = iota
  ONE
)
  1. Implicit Types
type myType int
const (
  ZERO = iota
  ONE
)

For the first case, I wrote the following rule and it works, no problem:

	m.Match(`$_ $_ = $iota;`).
		Where(m["iota"].Text == "iota").
		At(m["iota"]).
		Report(`...`)

However, I'm having trouble writing a rule that covers the second case. I've made several attempts:

  • $_ $*_ = $iota (I recall that the pattern didn't compile)
  • $_ = $iota (I think because the pattern is compiled as an AssignStmt, not a ValueSpec)
  • const ( $_ = $iota ) (This works but only if there's a single value in the list, as soon as I add more, the pattern doesn't match)
  • const ( $_ = $iota; $*_; ) (this fails the matchNodeList check)

Maybe there's a rule pattern that I haven't considered that would work for that second case. Or maybe it's just not supported by the framework. Any insight here would be greatly appreciated. Thank you!

@quasilyte
Copy link
Owner

quasilyte commented Oct 21, 2021

Hello!

This rule should work on tip/recent release:

	m.Match(`const ( $_ = $iota; $*_ )`).
		Where(m["iota"].Text == "iota").
		At(m["iota"]).
		Report(`...`)

If it doesn't work as expected, it needs to be fixed.
But the idea is correct.
I can add tests with that pattern to the analyzer/testdata (or you can do it, contributions are welcome).

@quasilyte
Copy link
Owner

quasilyte commented Oct 21, 2021

$_ = $iota (I think because the pattern is compiled as an AssignStmt, not a ValueSpec)

I think this pattern should match both in the future.
But making a const ( $_ = $iota; $*_ ) is more precise I think.
#292

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants