Gerrit Bot has uploaded this change for review.
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.
Daniel Martí abandoned this change.
To view, visit change 96090. To unsubscribe, or for help writing mail filters, visit settings.