[lint] lint: fix if/else if/else if/else return handling

23 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Feb 21, 2018, 5:55:01 PM2/21/18
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, Kevin Burke, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

lint: fix if/else if/else if/else return handling

If you have multiple "else if" statements and the last one has
a return, golint will incorrectly warn that the last else can be
dedented, even if the if/first else if blocks don't.

From the issue report: "the problem is that the ignore flag is not
getting propagated to all the else's in a chain because of the early
return." Flipping the order fixes the problem.

Fixes golang/lint#354. Patch was suggested by Ramesh Vyaghrapuri, who
also identified the problem.

Change-Id: I1057ca1d0de91d3ea6085e17d36df5fa16f43684
GitHub-Last-Rev: 41a7731d5ddf2d758e4c9dcc8e91dba1fa57a9a4
GitHub-Pull-Request: golang/lint#355
---
M lint.go
M testdata/else-multi.go
2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/lint.go b/lint.go
index e1a5f30..f6cd78c 100644
--- a/lint.go
+++ b/lint.go
@@ -1039,13 +1039,13 @@
if !ok || ifStmt.Else == nil {
return true
}
- if ignore[ifStmt] {
- return true
- }
if elseif, ok := ifStmt.Else.(*ast.IfStmt); ok {
ignore[elseif] = true
return true
}
+ if ignore[ifStmt] {
+ return true
+ }
if _, ok := ifStmt.Else.(*ast.BlockStmt); !ok {
// only care about elses without conditions
return true
diff --git a/testdata/else-multi.go b/testdata/else-multi.go
index 98f39a3..ed61595 100644
--- a/testdata/else-multi.go
+++ b/testdata/else-multi.go
@@ -16,3 +16,19 @@
}
return false
}
+
+func issue354(x int) bool {
+ switch g {
+ case 0:
+ if x == 0 {
+ log.Print("x is zero")
+ } else if x > 0 {
+ log.Print("greater than 0")
+ } else if x < 0 {
+ return true
+ } else {
+ log.Printf("non-positive x: %d", x)
+ }
+ }
+ return false
+}

To view, visit change 96090. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: lint
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I1057ca1d0de91d3ea6085e17d36df5fa16f43684
Gerrit-Change-Number: 96090
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Kevin Burke <k...@inburke.com>

Daniel Martí (Gerrit)

unread,
May 8, 2021, 4:29:42 PM5/8/21
to Kevin Burke, Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Daniel Martí abandoned this change.

View Change

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] https://github.com/golang/go/issues/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

To view, visit change 96090. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: lint
Gerrit-Branch: master
Gerrit-Change-Id: I1057ca1d0de91d3ea6085e17d36df5fa16f43684
Gerrit-Change-Number: 96090
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Kevin Burke <k...@inburke.com>
Gerrit-MessageType: abandon
Reply all
Reply to author
Forward
0 new messages