Skip to content

Commit

Permalink
fix: separate if else block properly
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Aug 8, 2024
1 parent dfbdd9f commit da775a6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion formatter/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 11 additions & 4 deletions internal/lints/early_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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)
Expand All @@ -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() {
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lints/early_return_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
4 changes: 3 additions & 1 deletion testdata/complexity/medium.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit da775a6

Please sign in to comment.