Skip to content

Commit 0586c04

Browse files
authored
refactor(errormessage): rename helper functions to reflect exact matching semantics (#35728)
1 parent 2fc69dd commit 0586c04

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

pkg/linters/errormessage/errormessage.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func run(pass *analysis.Pass) (any, error) {
6666

6767
if msg, ok := extractLiteralErrorMessage(call); ok && returnsError(pass, call) {
6868
checkNegativeLanguage(pass, call, msg)
69-
checkGenericWrap(pass, call, msg)
69+
checkFailedToErrorfWrap(pass, call, msg)
7070
checkValidationFmtErrorf(pass, call, pos.Filename)
7171
}
7272

@@ -150,10 +150,10 @@ func checkValidationFmtErrorf(pass *analysis.Pass, call *ast.CallExpr, filename
150150

151151
func checkNegativeLanguage(pass *analysis.Pass, call *ast.CallExpr, msg string) {
152152
lower := strings.ToLower(msg)
153-
if !containsAnyKeyword(lower, "invalid", "cannot", "must", "failed") {
153+
if !containsAnyWholeWord(lower, "invalid", "cannot", "must", "failed") {
154154
return
155155
}
156-
if containsAnyKeyword(lower, "expected", "requires", "should", "example", "valid") {
156+
if containsAnyWholeWord(lower, "expected", "requires", "should", "example", "valid") {
157157
return
158158
}
159159
pass.ReportRangef(call, "error message uses negative language without constructive guidance; include expected/requires/should/example details")
@@ -181,7 +181,7 @@ func checkNewValidationSuggestion(pass *analysis.Pass, call *ast.CallExpr) {
181181
}
182182
}
183183

184-
func checkGenericWrap(pass *analysis.Pass, call *ast.CallExpr, msg string) {
184+
func checkFailedToErrorfWrap(pass *analysis.Pass, call *ast.CallExpr, msg string) {
185185
if !isFmtErrorf(call) {
186186
return
187187
}
@@ -210,16 +210,16 @@ func looksLikeYAMLExample(s string) bool {
210210
return strings.Contains(trimmed, ":") && strings.Contains(trimmed, " ")
211211
}
212212

213-
func containsAnyKeyword(s string, keywords ...string) bool {
213+
func containsAnyWholeWord(s string, keywords ...string) bool {
214214
for _, keyword := range keywords {
215-
if containsKeyword(s, keyword) {
215+
if containsWholeWord(s, keyword) {
216216
return true
217217
}
218218
}
219219
return false
220220
}
221221

222-
func containsKeyword(s, keyword string) bool {
222+
func containsWholeWord(s, keyword string) bool {
223223
offset := 0
224224
for {
225225
i := strings.Index(s[offset:], keyword)

0 commit comments

Comments
 (0)