diff --git a/formatter/builder.go b/formatter/builder.go index 1145b02..eb5a755 100644 --- a/formatter/builder.go +++ b/formatter/builder.go @@ -11,7 +11,7 @@ import ( // rule set const ( - EarlyReturn = "early-return-opportunity" + EarlyReturn = "early-return" UnnecessaryTypeConv = "unnecessary-type-conversion" SimplifySliceExpr = "simplify-slice-range" CycloComplexity = "high-cyclomatic-complexity" diff --git a/internal/lints/early_return.go b/internal/lints/early_return.go index 143a0a1..cffe0f1 100644 --- a/internal/lints/early_return.go +++ b/internal/lints/early_return.go @@ -43,7 +43,7 @@ func DetectEarlyReturnOpportunities(filename string, node *ast.File, fset *token } issue := tt.Issue{ - Rule: "early-return-opportunity", + Rule: "early-return", Filename: filename, Start: fset.Position(ifStmt.Pos()), End: fset.Position(ifStmt.End()), @@ -129,7 +129,7 @@ func RemoveUnnecessaryElse(snippet string) (string, error) { func removeUnnecessaryElseAndEarlyReturnRecursive(node ast.Node) { ast.Inspect(node, func(n ast.Node) bool { if ifStmt, ok := n.(*ast.IfStmt); ok { - processIfStmtForEarlyReturn(ifStmt, node) + processIfStmt(ifStmt, node) removeUnnecessaryElseAndEarlyReturnRecursive(ifStmt.Body) if ifStmt.Else != nil { removeUnnecessaryElseAndEarlyReturnRecursive(ifStmt.Else) @@ -140,7 +140,7 @@ func removeUnnecessaryElseAndEarlyReturnRecursive(node ast.Node) { }) } -func processIfStmtForEarlyReturn(ifStmt *ast.IfStmt, node ast.Node) { +func processIfStmt(ifStmt *ast.IfStmt, node ast.Node) { if ifStmt.Else != nil { ifBranch := branch.BlockBranch(ifStmt.Body) if ifBranch.BranchKind.Deviates() { @@ -155,7 +155,7 @@ func processIfStmtForEarlyReturn(ifStmt *ast.IfStmt, node ast.Node) { ifStmt.Else = nil } } else if elseIfStmt, ok := ifStmt.Else.(*ast.IfStmt); ok { - processIfStmtForEarlyReturn(elseIfStmt, ifStmt) + processIfStmt(elseIfStmt, ifStmt) } } } @@ -195,7 +195,14 @@ func findParentBlockStmt(root ast.Node, child ast.Node) *ast.BlockStmt { func insertStatementsAfter(block *ast.BlockStmt, target ast.Stmt, stmts []ast.Stmt) { for i, stmt := range block.List { if stmt == target { + // insert new statements after the target statement block.List = append(block.List[:i+1], append(stmts, block.List[i+1:]...)...) + + for j := i + 1; j < len(block.List); j++ { + if newIfStmt, ok := block.List[j].(*ast.IfStmt); ok { + processIfStmt(newIfStmt, block) + } + } break } } diff --git a/internal/lints/early_return_test.go b/internal/lints/early_return_test.go index 2fb81a4..88b00d4 100644 --- a/internal/lints/early_return_test.go +++ b/internal/lints/early_return_test.go @@ -163,7 +163,7 @@ func example(x int) { if len(issues) > 0 { for _, issue := range issues { - assert.Equal(t, "early-return-opportunity", issue.Rule) + assert.Equal(t, "early-return", issue.Rule) assert.Contains(t, issue.Message, "can be simplified using early returns") } } diff --git a/testdata/complexity/medium.gno b/testdata/complexity/medium.gno index fe326f7..cefdc17 100644 --- a/testdata/complexity/medium.gno +++ b/testdata/complexity/medium.gno @@ -4,8 +4,10 @@ func mediumComplexity(x, y int) int { if x > y { if x > 10 { return x * 2 - } else { + } else if x > 5 { return x + y + } else { + return x - y } } else if y > 10 { return y * 2