Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Jul 22, 2024
1 parent 4526bff commit 94a722f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
2 changes: 1 addition & 1 deletion formatter/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"testing"

"github.com/gnoswap-labs/lint/internal"
tt "github.com/gnoswap-labs/lint/internal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tt "github.com/gnoswap-labs/lint/internal/types"
)

func TestFormatIssuesWithArrows(t *testing.T) {
Expand Down
3 changes: 0 additions & 3 deletions internal/dfa/dfa.go

This file was deleted.

34 changes: 17 additions & 17 deletions internal/lints/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,22 +422,22 @@ func main() {
}`,
expected: 2,
},
// {
// name: "Variable allocation in loop",
// // ref: https://stackoverflow.com/questions/77180437/understanding-short-variable-declaration-in-loop-resulting-unnecessary-memory-a
// code: `
// package main

// import "fmt"

// func main() {
// for i:=0; i<10; i++ {
// a:=i+1 // BAD!: allocates memory in every iteration
// fmt.Printf("i-val: %d, i-addr: %p, a-val: %d, a-addr: %p\n", i, &i, a, &a)
// }
// }`,
// expected: 1,
// },
// {
// name: "Variable allocation in loop",
// // ref: https://stackoverflow.com/questions/77180437/understanding-short-variable-declaration-in-loop-resulting-unnecessary-memory-a
// code: `
// package main

// import "fmt"

// func main() {
// for i:=0; i<10; i++ {
// a:=i+1 // BAD!: allocates memory in every iteration
// fmt.Printf("i-val: %d, i-addr: %p, a-val: %d, a-addr: %p\n", i, &i, a, &a)
// }
// }`,
// expected: 1,
// },
{
name: "No allocation in loop",
code: `
Expand Down Expand Up @@ -473,7 +473,7 @@ func main() {
defer os.RemoveAll(tmpDir)

tmpfile := filepath.Join(tmpDir, "test.go")
err = os.WriteFile(tmpfile, []byte(tt.code), 0644)
err = os.WriteFile(tmpfile, []byte(tt.code), 0o644)
require.NoError(t, err)

issues, err := DetectLoopAllocation(tmpfile)
Expand Down
29 changes: 14 additions & 15 deletions internal/lints/loop_allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ func DetectLoopAllocation(filename string) ([]tt.Issue, error) {
if isAllocationFunction(innerNode) {
issues = append(issues, tt.Issue{
Message: "Potential unnecessary allocation inside loop",
Start: fset.Position(innerNode.Pos()),
End: fset.Position(innerNode.End()),
Start: fset.Position(innerNode.Pos()),
End: fset.Position(innerNode.End()),
})
}
// case *ast.AssignStmt:
// if innerNode.Tok == token.DEFINE {
// issues = append(issues, tt.Issue{
// Message: "Variable declaration inside loop may cause unnecessary allocation",
// Start: fset.Position(innerNode.Pos()),
// End: fset.Position(innerNode.End()),
// })
// }
// case *ast.AssignStmt:
// if innerNode.Tok == token.DEFINE {
// issues = append(issues, tt.Issue{
// Message: "Variable declaration inside loop may cause unnecessary allocation",
// Start: fset.Position(innerNode.Pos()),
// End: fset.Position(innerNode.End()),
// })
// }
}
return true
})
Expand All @@ -47,10 +47,9 @@ func DetectLoopAllocation(filename string) ([]tt.Issue, error) {
return issues, nil
}


func isAllocationFunction(call *ast.CallExpr) bool {
if ident, ok := call.Fun.(*ast.Ident); ok {
return ident.Name == "make" || ident.Name == "new"
}
return false
if ident, ok := call.Fun.(*ast.Ident); ok {
return ident.Name == "make" || ident.Name == "new"
}
return false
}

0 comments on commit 94a722f

Please sign in to comment.