Skip to content
This repository was archived by the owner on May 9, 2021. It is now read-only.

Rewrite lintElses check to handle multiple else ifs#181

Closed
zimmski wants to merge 1 commit intogolang:masterfrom
zimmski:fix-else-outdenting
Closed

Rewrite lintElses check to handle multiple else ifs#181
zimmski wants to merge 1 commit intogolang:masterfrom
zimmski:fix-else-outdenting

Conversation

@zimmski
Copy link
Copy Markdown
Contributor

@zimmski zimmski commented Dec 12, 2015

As suggested by @dsymonds this PR handles multiple else-ifs in lintElses. The suggestion for outdenting an else block is only shown if the if block has a return as last statement, no else-ifs and of course an else block.The PR also adds some test cases to make the behavior clearer.

Comment thread lint.go Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

revert. map[T]bool is the most common way to represent a set, and the space savings for using map[T]struct{} are smaller than people tend to assume.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@dsymonds
Copy link
Copy Markdown
Contributor

The code here is meant to completely ignore else if blocks, mostly because their control flow is a lot harder to see than just if+else. Can you make this change correctly ignore an else if chain, rather than trying to flag the dangling else when all the previous blocks return?

@zimmski zimmski force-pushed the fix-else-outdenting branch 4 times, most recently from 936d006 to 8fe77f2 Compare October 28, 2016 20:46
@zimmski
Copy link
Copy Markdown
Contributor Author

zimmski commented Oct 28, 2016

@dsnet: I resolved @dsymonds concerns and adapted the whole PR to be smaller since most cases are not needed anymore because of the behavior change. Please take a look.

@zimmski
Copy link
Copy Markdown
Contributor Author

zimmski commented Apr 9, 2017

@dsnet Any update on this?

@JKobyP
Copy link
Copy Markdown

JKobyP commented Aug 16, 2017

Any updates on this?

@zimmski
Copy link
Copy Markdown
Contributor Author

zimmski commented Nov 29, 2017

@dsymonds since this came up with #354 again. Could you please take another look at this PR? I would be really sad if the invested time would be wasted. (I guess since it is that old a CI rerun would be also a good idea)

@andybons
Copy link
Copy Markdown
Member

@alandonovan can you take a look at this?

Copy link
Copy Markdown
Contributor

@alandonovan alandonovan left a comment

Choose a reason for hiding this comment

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

Code looks fine to me, but I'm not really an owner of this project.

Comment thread lint.go

outdentElse := true

// The else-block should only be outdented if the if block ends with a return statement.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there are reason to detect 'return' but not other control statements such as 'continue' and 'break'?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The original code handles only "returns". I guess it would be better to add "continue" and "break" in another PR unless you really want me to?

Comment thread lint.go Outdated
if _, ok := ifStmt.Else.(*ast.BlockStmt); !ok {
// only care about elses without conditions

if _, ok := ignore[ifStmt]; ok {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's quite normal to use a map[T]bool containing only true values to represent a set of T, and idiomatic to test for membership of such a set using just m[k]. Please restore the previous check.

@andybons
Copy link
Copy Markdown
Member

@zimmski ping

@andybons
Copy link
Copy Markdown
Member

@googlebot cla?

@zimmski zimmski force-pushed the fix-else-outdenting branch from 653ab5c to 793a850 Compare February 21, 2018 20:49
@zimmski
Copy link
Copy Markdown
Contributor Author

zimmski commented Feb 21, 2018

Funny enough, I did not receive any notifications for @alandonovan comments but got @andybons comments right away? So just in case my comments for the code are not going through:
a.) I made the one change @alandonovan requested, rebased to the latest master and pushed the newest version.
b.) Regarding adding support for "continue" and "break" statements: The original intention of this PR was to fix the else-if problems. Adding these statements is fine with me, but if it is up to me, I would add them in another PR.

@gopherbot
Copy link
Copy Markdown

This PR (HEAD: 793a850) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/#/c/lint/+/96076 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Copy Markdown

This PR is being closed because golang.org/cl/96076 has been abandoned.

Thank you for submitting this patch! As proposed[1], we are freezing and deprecating golint. There's no drop-in replacement to golint per se, but you should find that Staticcheck[2] works well in encouraging good Go code, much like golint did in the past, since it also includes style checks. There's always gofmt and "go vet" too, of course.

If you would like to contribute further, I'd encourage you to engage Staticcheck's issue tracker[3] or look at vet's open issues[4], as they are both actively maintained. If you have an idea that doesn't fit into either of those tools, you could look at other Go linters[5], or write your own - these days it's fairly straightforward with go/analysis[6].

To help avoid confusion, I'm closing all CLs before we freeze the repository. If you have any feedback, you can leave a comment on the proposal thread where it was decided to deprecate golint - though note that the proposal has been accepted for nearly a year. Thanks!

[1] golang/go#38968
[2] https://staticcheck.io/
[3] https://github.com/dominikh/go-tools/issues
[4] https://github.com/golang/go/issues?q=is%3Aissue+is%3Aopen+cmd%2Fvet+in%3Atitle
[5] https://github.com/golangci/awesome-go-linters
[6] https://pkg.go.dev/golang.org/x/tools/go/analysis

@gopherbot gopherbot closed this May 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants