Skip to content

feature: Improve error-strings rule to detect acronyms and proper nouns #1287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2025
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
28 changes: 18 additions & 10 deletions rule/error_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,29 @@ func (lintErrorStrings) checkArg(expr *ast.CallExpr, arg int) (s *ast.BasicLit,
func lintErrorString(s string) (isClean bool, conf float64) {
const basicConfidence = 0.8
const capConfidence = basicConfidence - 0.2
first, firstN := utf8.DecodeRuneInString(s)

last, _ := utf8.DecodeLastRuneInString(s)
if last == '.' || last == ':' || last == '!' || last == '\n' {
return false, basicConfidence
}
if unicode.IsUpper(first) {
// People use proper nouns and exported Go identifiers in error strings,
// so decrease the confidence of warnings for capitalization.
if len(s) <= firstN {
return false, capConfidence

first, firstN := utf8.DecodeRuneInString(s)
if !unicode.IsUpper(first) {
return true, 0
}

// People use proper nouns and exported Go identifiers in error strings,
// so decrease the confidence of warnings for capitalization.
for _, r := range s[firstN:] {
if unicode.IsSpace(r) {
break
}
// Flag strings starting with something that doesn't look like an initialism.
if second, _ := utf8.DecodeRuneInString(s[firstN:]); !unicode.IsUpper(second) {
return false, capConfidence

if unicode.IsUpper(r) || unicode.IsDigit(r) {
return true, 0 // accept words with more than 2 capital letters or digits (e.g. GitHub, URLs, I2000)
}
}
return true, 0

// Flag strings starting with something that doesn't look like an initialism.
return false, capConfidence
}
7 changes: 7 additions & 0 deletions testdata/golint/error_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ func g(x int) error {
err = fmt.Errorf("Newlines are really fun\n") // MATCH /error strings should not be capitalized or end with punctuation or a newline/
err = errors.New(`too much stuff.`) // MATCH /error strings should not be capitalized or end with punctuation or a newline/
err = errors.New("This %d is too low", x) // MATCH /error strings should not be capitalized or end with punctuation or a newline/
err = errors.New("GitHub should be ok", x)
err = errors.New("OTP should be ok", x)
err = errors.New("A JSON should be not ok", x) // MATCH /error strings should not be capitalized or end with punctuation or a newline/
err = errors.New("H264 should be ok", x)
err = errors.New("I/O should be ok", x)
err = errors.New("U.S. should be ok", x)
err = errors.New("Wi-Fi should be ok", x)

// Non-regression test for issue #610
d.stack.Push(from)
Expand Down