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

lint/parenthesis-checker.go: add variable types check #31

Merged
merged 7 commits into from
May 9, 2018

Conversation

fexolm
Copy link
Contributor

@fexolm fexolm commented May 9, 2018

No description provided.

@fexolm fexolm requested a review from quasilyte May 9, 2018 11:45
@fexolm fexolm mentioned this pull request May 9, 2018
5 tasks
// TODO improve suggestions for complex cases like (func([](func())))
// TODO improve linter output to write full type, not just place
// whereit could be simplified
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/whereit/where it/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to disable interfacer in megacheck inside our CI script.
Method says validateExpr, but it accepts any ast.Node.
And that is not quite good, because ast.Inspect inside it will never handle anything except expressions.

This is not the first time interfacer bites us.
We should treat it as a suggestion, not obligation that breaks out build.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about s/validateExpr/validateType/?
Types are expressions in AST terms, but not all expressions are types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM, i ll rename it in a moment.
CI script is out of scope of that pr. I ll create issue for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fexolm, agree. Thank you.

}
case *ast.GenDecl:
for _, spec := range decl.Specs {
spec, ok := spec.(*ast.ValueSpec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will panic for specs that are not var or const.
Add this to your tests and linter will (most likely) panic:

type foo int

Copy link
Member

@quasilyte quasilyte May 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you may want to do for now is:

if decl.Tok != token.VAR || decl.Tok != token.CONST {
	continue
}

And only then iterate over decl.Specs assertion all it's elements to *ast.ValueSpec.

Just for the reference:

token.IMPORT  *ImportSpec
token.CONST   *ValueSpec
token.TYPE    *TypeSpec
token.VAR     *ValueSpec

https://golang.org/pkg/go/ast/#GenDecl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you will handle token.TYPE, it's still required to skip token.IMPORT, as import specs are not interesting for your checker.


var badVar [](func())

var badVar2 [5](*int)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add at least one case for const and type declarations.

Copy link
Contributor Author

@fexolm fexolm May 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@quasilyte, could you please show, how this check could be used in type declarations?

Copy link
Member

@quasilyte quasilyte May 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type foo [](func())

For struct types, you need to walk all fields:

type bar struct {
	bad1 [2](int)
	bad2 []([](float64))
}

_ [](func())
)

const _ (int) = 5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add 1 type decl here and make sure that checker does not panic.
We will merge after that and you can continue to work on ast.TypeSpec in a separate PR.

Copy link
Member

@quasilyte quasilyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if it's not crashing after checking testdata.

We'll have real tests that are a part of CI that check for these things in some near future. :)

@fexolm fexolm merged commit dd435ba into master May 9, 2018
@fexolm fexolm deleted the parenthesis_v2 branch May 9, 2018 13:12

ast.Inspect(f.Type, func(n ast.Node) bool {
fmt.Printf("%s\n", nodeString(c.ctx.FileSet, n))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this one.
fmt.Printf("%s\n", nodeString(c.ctx.FileSet, n)) Should be removed.

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

Successfully merging this pull request may close these issues.

None yet

2 participants