mirror of
https://github.com/mgechev/revive.git
synced 2025-11-25 22:12:38 +02:00
feature: Improve error-strings rule to detect acronyms and proper nouns (#1287)
This commit is contained in:
@@ -178,21 +178,29 @@ func (lintErrorStrings) checkArg(expr *ast.CallExpr, arg int) (s *ast.BasicLit,
|
|||||||
func lintErrorString(s string) (isClean bool, conf float64) {
|
func lintErrorString(s string) (isClean bool, conf float64) {
|
||||||
const basicConfidence = 0.8
|
const basicConfidence = 0.8
|
||||||
const capConfidence = basicConfidence - 0.2
|
const capConfidence = basicConfidence - 0.2
|
||||||
first, firstN := utf8.DecodeRuneInString(s)
|
|
||||||
last, _ := utf8.DecodeLastRuneInString(s)
|
last, _ := utf8.DecodeLastRuneInString(s)
|
||||||
if last == '.' || last == ':' || last == '!' || last == '\n' {
|
if last == '.' || last == ':' || last == '!' || last == '\n' {
|
||||||
return false, basicConfidence
|
return false, basicConfidence
|
||||||
}
|
}
|
||||||
if unicode.IsUpper(first) {
|
|
||||||
// People use proper nouns and exported Go identifiers in error strings,
|
first, firstN := utf8.DecodeRuneInString(s)
|
||||||
// so decrease the confidence of warnings for capitalization.
|
if !unicode.IsUpper(first) {
|
||||||
if len(s) <= firstN {
|
return true, 0
|
||||||
return false, capConfidence
|
}
|
||||||
|
|
||||||
|
// 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) {
|
if unicode.IsUpper(r) || unicode.IsDigit(r) {
|
||||||
return false, capConfidence
|
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
testdata/golint/error_strings.go
vendored
7
testdata/golint/error_strings.go
vendored
@@ -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 = 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(`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("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
|
// Non-regression test for issue #610
|
||||||
d.stack.Push(from)
|
d.stack.Push(from)
|
||||||
|
|||||||
Reference in New Issue
Block a user