Skip to content

Commit 025a91f

Browse files
committed
treat err assignments as a simple error check
We considered the following pattern a "simple" error check, where err was declared with `:=` and immediately followed by an if: x, err := f() if err != nil {...} We would then remove the empty line before the error check. However, we wouldn't do the same if x and err were already declared: x, err = f() if err != nil {...} The second form is still relatively common, and there's no good reason for the check to treat the two cases differently. Fixes #271.
1 parent 9a108c1 commit 025a91f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

format/format.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -866,9 +866,9 @@ func (f *fumpter) stmts(list []ast.Stmt) {
866866
continue // not an if following another statement
867867
}
868868
as, ok := list[i-1].(*ast.AssignStmt)
869-
if !ok || as.Tok != token.DEFINE ||
869+
if !ok || (as.Tok != token.DEFINE && as.Tok != token.ASSIGN) ||
870870
!identEqual(as.Lhs[len(as.Lhs)-1], "err") {
871-
continue // not "..., err := ..."
871+
continue // not ", err :=" nor ", err ="
872872
}
873873
be, ok := ifs.Cond.(*ast.BinaryExpr)
874874
if !ok || ifs.Init != nil || ifs.Else != nil {

testdata/script/newline-errcheck.txtar

+11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ func f() {
5353
panic(err)
5454
}
5555
}
56+
57+
n5, err = Do2()
58+
59+
if err != nil {
60+
panic(err)
61+
}
5662
}
5763
-- foo.go.golden --
5864
package p
@@ -101,4 +107,9 @@ func f() {
101107
panic(err)
102108
}
103109
}
110+
111+
n5, err = Do2()
112+
if err != nil {
113+
panic(err)
114+
}
104115
}

0 commit comments

Comments
 (0)