Description
The suggested fix inserts the assignment right before ifStmt.Pos(). When the if is an else if branch, the insertion lands between else and if, producing code that doesn't compile.
Reproduce
func elseIfDefine() error {
x := 1
if x > 0 {
return nil
} else if err := f(); err != nil {
return err
}
return nil
}
Applying the suggested fix yields:
} else err := f()
if err != nil {
which fails to parse: expected if statement or block, found err.
Affected cases
Possible fix
Don't emit a SuggestedFix when the ifStmt is the Else branch of a parent *ast.IfStmt (e.g. walk with inspector.WithStack and check the parent node) — report the diagnostic message only. Alternatively, rewrite the whole else if chain, but that's much more involved.
Description
The suggested fix inserts the assignment right before
ifStmt.Pos(). When theifis anelse ifbranch, the insertion lands betweenelseandif, producing code that doesn't compile.Reproduce
Applying the suggested fix yields:
which fails to parse:
expected if statement or block, found err.Affected cases
else if err := f(); err != nil— broken on current masterelse if err = f(); err != nil— also broken after feat: support if err = doSomething(); err != nil #8 extends autofix to reassignmentsPossible fix
Don't emit a
SuggestedFixwhen theifStmtis theElsebranch of a parent*ast.IfStmt(e.g. walk withinspector.WithStackand check the parent node) — report the diagnostic message only. Alternatively, rewrite the wholeelse ifchain, but that's much more involved.