Suggest moving short variable declarations to their own line if needed to keep normal code flow outdented.
diff --git a/lint.go b/lint.go
index 8da6ea5..5fa3840 100644
--- a/lint.go
+++ b/lint.go
@@ -658,9 +658,19 @@
if len(ifStmt.Body.List) == 0 {
return true
}
+ shortDecl := false // does the if statement have a ":=" initialization statement?
+ if ifStmt.Init != nil {
+ if as, ok := ifStmt.Init.(*ast.AssignStmt); ok && as.Tok == token.DEFINE {
+ shortDecl = true
+ }
+ }
lastStmt := ifStmt.Body.List[len(ifStmt.Body.List)-1]
if _, ok := lastStmt.(*ast.ReturnStmt); ok {
- f.errorf(ifStmt.Else, 1, "if block ends with a return statement, so drop this else and outdent its block")
+ extra := ""
+ if shortDecl {
+ extra = " (move short variable declaration to its own line if necessary)"
+ }
+ f.errorf(ifStmt.Else, 1, "if block ends with a return statement, so drop this else and outdent its block"+extra)
}
return true
})
diff --git a/testdata/else.go b/testdata/else.go
index d5f6e6c..515c043 100644
--- a/testdata/else.go
+++ b/testdata/else.go
@@ -13,3 +13,11 @@
}
return false
}
+
+func g(f func() bool) string {
+ if ok := f(); ok {
+ return "it's okay"
+ } else { // MATCH /if.*return.*else.*outdent.*short.*var.*declaration/
+ return "it's NOT okay!"
+ }
+}