1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-15 01:04:40 +02:00

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

This commit is contained in:
chavacava
2025-04-08 07:17:58 +02:00
committed by GitHub
parent 343da9fbf2
commit 02f0a40e4e
2 changed files with 25 additions and 10 deletions

View File

@ -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
}

View File

@ -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)