Skip to content

Commit 02f0a40

Browse files
authored
feature: Improve error-strings rule to detect acronyms and proper nouns (#1287)
1 parent 343da9f commit 02f0a40

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

rule/error_strings.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,29 @@ func (lintErrorStrings) checkArg(expr *ast.CallExpr, arg int) (s *ast.BasicLit,
178178
func lintErrorString(s string) (isClean bool, conf float64) {
179179
const basicConfidence = 0.8
180180
const capConfidence = basicConfidence - 0.2
181-
first, firstN := utf8.DecodeRuneInString(s)
181+
182182
last, _ := utf8.DecodeLastRuneInString(s)
183183
if last == '.' || last == ':' || last == '!' || last == '\n' {
184184
return false, basicConfidence
185185
}
186-
if unicode.IsUpper(first) {
187-
// People use proper nouns and exported Go identifiers in error strings,
188-
// so decrease the confidence of warnings for capitalization.
189-
if len(s) <= firstN {
190-
return false, capConfidence
186+
187+
first, firstN := utf8.DecodeRuneInString(s)
188+
if !unicode.IsUpper(first) {
189+
return true, 0
190+
}
191+
192+
// People use proper nouns and exported Go identifiers in error strings,
193+
// so decrease the confidence of warnings for capitalization.
194+
for _, r := range s[firstN:] {
195+
if unicode.IsSpace(r) {
196+
break
191197
}
192-
// Flag strings starting with something that doesn't look like an initialism.
193-
if second, _ := utf8.DecodeRuneInString(s[firstN:]); !unicode.IsUpper(second) {
194-
return false, capConfidence
198+
199+
if unicode.IsUpper(r) || unicode.IsDigit(r) {
200+
return true, 0 // accept words with more than 2 capital letters or digits (e.g. GitHub, URLs, I2000)
195201
}
196202
}
197-
return true, 0
203+
204+
// Flag strings starting with something that doesn't look like an initialism.
205+
return false, capConfidence
198206
}

testdata/golint/error_strings.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ func g(x int) error {
1616
err = fmt.Errorf("Newlines are really fun\n") // MATCH /error strings should not be capitalized or end with punctuation or a newline/
1717
err = errors.New(`too much stuff.`) // MATCH /error strings should not be capitalized or end with punctuation or a newline/
1818
err = errors.New("This %d is too low", x) // MATCH /error strings should not be capitalized or end with punctuation or a newline/
19+
err = errors.New("GitHub should be ok", x)
20+
err = errors.New("OTP should be ok", x)
21+
err = errors.New("A JSON should be not ok", x) // MATCH /error strings should not be capitalized or end with punctuation or a newline/
22+
err = errors.New("H264 should be ok", x)
23+
err = errors.New("I/O should be ok", x)
24+
err = errors.New("U.S. should be ok", x)
25+
err = errors.New("Wi-Fi should be ok", x)
1926

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

0 commit comments

Comments
 (0)