Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions internal/codeguard/checks/quality/quality_defensive.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
strings.HasSuffix(name, "row") ||
strings.Contains(name, "rowpart") ||
strings.HasSuffix(name, "part") ||
regexp.MustCompile(`part\d+$`).MatchString(name) ||

Check warning on line 171 in internal/codeguard/checks/quality/quality_defensive.go

View workflow job for this annotation

GitHub Actions / codeguard

[performance.regex-compile-in-loop] regular expression compiled inside a loop; compile it once before the loop or as a package-level variable. Fix: Compile the pattern once before the loop or at package/module level and reuse it.
strings.HasSuffix(name, "context") ||
strings.HasSuffix(name, "ctx") ||
strings.Contains(name, "dto") ||
Expand Down Expand Up @@ -334,12 +334,20 @@
if guardStart == nil {
return false
}
windowStart := guardStart[1]
windowEnd := windowStart + 3000
if windowEnd > len(loweredBody) {
windowEnd = len(loweredBody)
blockStart := guardStart[1] - 1
depth := 1
for blockEnd := blockStart + 1; blockEnd < len(loweredBody); blockEnd++ {
switch loweredBody[blockEnd] {
case '{':
depth++
case '}':
depth--
if depth == 0 {
return regexp.MustCompile(`\b(?:return|throw|continue|break)\b`).MatchString(loweredBody[blockStart+1 : blockEnd])

Check warning on line 346 in internal/codeguard/checks/quality/quality_defensive.go

View workflow job for this annotation

GitHub Actions / codeguard

[performance.regex-compile-in-loop] regular expression compiled inside a loop; compile it once before the loop or as a package-level variable. Fix: Compile the pattern once before the loop or at package/module level and reuse it.
}
}
}
return regexp.MustCompile(`\b(?:return|throw|continue|break)\b`).MatchString(loweredBody[windowStart:windowEnd])
return false
}

func nullableUseLine(fn precisionFunction, name string) int {
Expand Down
44 changes: 44 additions & 0 deletions internal/codeguard/checks/quality/quality_defensive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package quality

import (
"regexp"
"testing"
)

func TestNullableParamHasBlockExitGuardRequiresExitInsideGuard(t *testing.T) {
tests := []struct {
name string
body string
want bool
}{
{
name: "return in guard",
body: `if (!user) { return missingUser(); } return user.email;`,
want: true,
},
{
name: "return after guard",
body: `if (!user) { logMissing(); } return user.email;`,
want: false,
},
{
name: "return in nested guard scope",
body: `if (!user) { if (missing()) { return fallback; } throw err; } use(user);`,
want: true,
},
{
name: "unclosed guard",
body: `if (!user) { logMissing(); return fallback;`,
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := nullableParamHasBlockExitGuard(tt.body, regexp.QuoteMeta("user"))
if got != tt.want {
t.Fatalf("nullableParamHasBlockExitGuard() = %v, want %v", got, tt.want)
}
})
}
}
Loading