Skip to content

Commit 59520a8

Browse files
committed
Reduce allocs in config
1 parent bab341a commit 59520a8

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

internal/config/rule.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,30 +148,38 @@ func (rule Rule) validate() (err error) {
148148
return nil
149149
}
150150

151-
func isDisabledForRule(rule parser.Rule, name string, check checks.RuleChecker, promTags []string) bool {
152-
matches := make([]string, 0, len(promTags)+2)
153-
matches = append(matches, name)
154-
matches = append(matches, check.String())
155-
for _, tag := range promTags {
156-
matches = append(matches, name+"(+"+tag+")")
151+
func matchesTag(comment, name string, tags []string) bool {
152+
// control comment doesn't start with name so it cannot match it.
153+
if !strings.HasPrefix(comment, name) {
154+
return false
157155
}
158-
for _, disable := range comments.Only[comments.Disable](rule.Comments, comments.DisableType) {
159-
for _, match := range matches {
160-
if match == disable.Match {
161-
slog.LogAttrs(context.Background(), slog.LevelDebug,
162-
"Check disabled by comment",
163-
slog.String("check", check.String()),
164-
slog.String("match", match),
165-
)
156+
for _, tag := range tags {
157+
if strings.Contains(comment, tag) {
158+
if comment == name+"(+"+tag+")" {
166159
return true
167160
}
168161
}
162+
163+
}
164+
return false
165+
}
166+
167+
func isDisabledForRule(rule parser.Rule, name string, check checks.RuleChecker, promTags []string) bool {
168+
for _, disable := range comments.Only[comments.Disable](rule.Comments, comments.DisableType) {
169+
if disable.Match == name || disable.Match == check.String() || matchesTag(disable.Match, name, promTags) {
170+
slog.LogAttrs(context.Background(), slog.LevelDebug,
171+
"Check disabled by comment",
172+
slog.String("check", check.String()),
173+
slog.String("match", disable.Match),
174+
)
175+
return true
176+
}
169177
}
170178
for _, snooze := range comments.Only[comments.Snooze](rule.Comments, comments.SnoozeType) {
171179
if !snooze.Until.After(time.Now()) {
172180
continue
173181
}
174-
if slices.Contains(matches, snooze.Match) {
182+
if snooze.Match == name || snooze.Match == check.String() || matchesTag(snooze.Match, name, promTags) {
175183
slog.LogAttrs(context.Background(), slog.LevelDebug,
176184
"Check snoozed by comment",
177185
slog.String("check", check.String()),

0 commit comments

Comments
 (0)