-
Notifications
You must be signed in to change notification settings - Fork 15
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
Conversation
d017ec7
to
df6fe66
Compare
df6fe66
to
1803bcf
Compare
1803bcf
to
ca4c318
Compare
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{{ |
There was a problem hiding this comment.
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
func main() { | ||
analyzer := nakedret.NakedReturnAnalyzer(DefaultLines) | ||
singlechecker.Main(analyzer) | ||
} |
There was a problem hiding this comment.
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
I'm seeing a build failure - I think we'd need to bump up the Go version here: nakedret/.github/workflows/build-test.yml Line 12 in c7b0313
Mind bumping this up? Happy to merge after that |
Bumped to 1.18 |
The build had this error:
but bumping to for example go1.16 also seems to work fine. Bumping to go1.17 adds the |
nakedret.go
Outdated
MaxLength uint | ||
} | ||
|
||
func (n *NakedReturnRunner) run(pass *analysis.Pass) (interface{}, error) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
if length == 0 { | ||
// consider functions that finish on the same line as they start as single line functions, not zero lines! | ||
length = 1 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
👍
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
//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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
👍
We could also write in
|
I pushed a short sentence to the README. |
@alexkohler Feel feel to push any final updates if you're mostly happy! I enabled the allow edits by maintainers 😄 |
Thanks again for the PR 😄 |
a first go at using go/analysis as suggested in #24