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

Refactor to use go/analysis #25

Merged
merged 12 commits into from
Apr 10, 2023

Conversation

zalenskivolt
Copy link
Contributor

a first go at using go/analysis as suggested in #24

@zalenskivolt zalenskivolt force-pushed the use-go-analysis branch 2 times, most recently from d017ec7 to df6fe66 Compare April 8, 2023 00:39
Pos: s.Pos(),
End: s.End(),
Message: fmt.Sprintf("naked return in func `%s` with %d lines of code", funName, fun.funcLength),
SuggestedFixes: []analysis.SuggestedFix{{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

SuggestedFixes do not yet get picked up by golangci-lint, but it's planned,
golangci/golangci-lint#1779 (comment):

I think that it's the right thing for a linter to support SuggestedFixes (even if SuggestedFixes is still an experimental API), I also believe that we have to remove our custom implementation.

Update code to use package nakedret, to be possibly to import
Comment on lines +13 to +16
func main() {
analyzer := nakedret.NakedReturnAnalyzer(DefaultLines)
singlechecker.Main(analyzer)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved main() to cmd/nakedret/main.go so nakedret code can use package nakedret and be imported

@alexkohler
Copy link
Owner

I'm seeing a build failure - I think we'd need to bump up the Go version here:

go-version: 1.13.x

Mind bumping this up? Happy to merge after that

@zalenskivolt
Copy link
Contributor Author

Bumped to 1.18

@zalenskivolt
Copy link
Contributor Author

Bumped to 1.18

The build had this error:

Error: ../../../go/pkg/mod/golang.org/x/[email protected]/go/gcexportdata/gcexportdata.go:97:9: undefined: io.ReadAll
note: module requires Go 1.18

but bumping to for example go1.16 also seems to work fine. Bumping to go1.17 adds the // indirect lines in go.mod, and bumping to go1.18 cleans up most of go.sum. I guess 1.18 is fine for most projects by now.

nakedret.go Outdated
MaxLength uint
}

func (n *NakedReturnRunner) run(pass *analysis.Pass) (interface{}, error) {

Choose a reason for hiding this comment

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

We can use any instead of interface{} because of the Go 1.18.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated to use any

nakedret.go Outdated

stack []funcInfo

Choose a reason for hiding this comment

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

Maybe it's better to usefunctions as the slice's name? Because stack is unclear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree, also thought about this.

updated

nakedret_test.go Outdated
}

testdata := filepath.Join(wd, "testdata")
log.Printf("Running tests on %s", testdata)

Choose a reason for hiding this comment

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

Maybe t.Log instead of log.Printf?

Btw., do we really need this log here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed, not really needed

Consider functions that end their definition on the same line as they start as one-liners, not zero-liners.
Comment on lines +305 to +307
if length == 0 {
// consider functions that finish on the same line as they start as single line functions, not zero lines!
length = 1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added check to handle one-liners

@@ -6,7 +6,7 @@ nakedret is a Go static analysis tool to find naked returns in functions greater
Install Nakedret via go install:

```cmd
go install github.com/alexkohler/nakedret@latest
go install github.com/alexkohler/nakedret/cmd/nakedret@latest
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From some local testing, it seems like the command-line tool now has to be installed with
go install github.com/alexkohler/nakedret/cmd/nakedret (with cmd/nakedret added)
but this is the same as for example goimports
go install golang.org/x/tools/cmd/goimports@latest

I'm not really sure what are best practices for things like this, it looks like many projects use both cmd/x and the root directory. goimports and golangci-lint both use cmd/x as well

Copy link
Owner

Choose a reason for hiding this comment

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

From some local testing, it seems like the command-line tool now has to be installed with
go install github.com/alexkohler/nakedret/cmd/nakedret (with cmd/nakedret added)

Hm, would this break folks that are running things like go install github.com/alexkohler/nakedret@latest in CI? (not saying that's the best practice ever 😅)

I'm not really sure what are best practices for things like this, it looks like many projects use both cmd/x and the root directory. goimports and golangci-lint both use cmd/x as well

Seems like cmd/x might be the most idiomatic thing to do, but I don't really have strong feelings - I'd be fine with cmd/ or cmd/x 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, would this break folks that are running things like go install github.com/alexkohler/nakedret@latest in CI? (not saying that's the best practice ever 😅)

that's true, but I guess then you would have the option to either use a specific version if you don't want to update anything, or update the reference?

maybe even update the README to refer to golangci-lint as the primary way to run it? it's just so much easier to all kinds of linting in one place… still good to be able to run each individual linter as a tool of course!

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, I think most workflows should be using golangci-lint. I think this is a positive change that'll reduce some tech debt over in golangci-lint, so even if this ends up breaking some go install github.com/alexkohler/nakedret@latest calls, it's probably worth it IMO.

nakedret.go Outdated
Comment on lines 285 to 286
//v.pass.Reportf(s.Pos(), "%v naked returns on %v line function", funName, fun.funcLength)
//v.pass.Reportf(s.Pos(), "naked return in func `%s` with %d lines of code", funName, fun.funcLength)
Copy link
Owner

Choose a reason for hiding this comment

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

Were these intentionally left around?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

they were the original message and the updated message before suggested fixes.

removed

@@ -6,7 +6,7 @@ nakedret is a Go static analysis tool to find naked returns in functions greater
Install Nakedret via go install:

```cmd
go install github.com/alexkohler/nakedret@latest
go install github.com/alexkohler/nakedret/cmd/nakedret@latest
Copy link
Owner

Choose a reason for hiding this comment

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

From some local testing, it seems like the command-line tool now has to be installed with
go install github.com/alexkohler/nakedret/cmd/nakedret (with cmd/nakedret added)

Hm, would this break folks that are running things like go install github.com/alexkohler/nakedret@latest in CI? (not saying that's the best practice ever 😅)

I'm not really sure what are best practices for things like this, it looks like many projects use both cmd/x and the root directory. goimports and golangci-lint both use cmd/x as well

Seems like cmd/x might be the most idiomatic thing to do, but I don't really have strong feelings - I'd be fine with cmd/ or cmd/x 👍

@alexandear
Copy link

We could also write in README.md that it's possible to run nakedret as go vet tool:

go vet -vettool=$(which nakedret) ./...

@zalenskivolt
Copy link
Contributor Author

We could also write in README.md that it's possible to run nakedret as go vet tool:

I pushed a short sentence to the README.

@zalenskivolt
Copy link
Contributor Author

Happy to merge after that

@alexkohler Feel feel to push any final updates if you're mostly happy! I enabled the allow edits by maintainers 😄

@alexkohler
Copy link
Owner

Thanks again for the PR 😄

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.

4 participants