golint: Permit starting error strings with initialisms.
diff --git a/lint.go b/lint.go
index 691c826..8da6ea5 100644
--- a/lint.go
+++ b/lint.go
@@ -739,6 +739,20 @@
}
}
+func lintCapAndPunct(s string) (isCap, isPunct bool) {
+ first, firstN := utf8.DecodeRuneInString(s)
+ last, _ := utf8.DecodeLastRuneInString(s)
+ isPunct = last == '.' || last == ':' || last == '!'
+ isCap = unicode.IsUpper(first)
+ if isCap && len(s) > firstN {
+ // Don't flag strings starting with something that looks like an initialism.
+ if second, _ := utf8.DecodeRuneInString(s[firstN:]); unicode.IsUpper(second) {
+ isCap = false
+ }
+ }
+ return
+}
+
// lintErrorStrings examines error strings. It complains if they are capitalized or end in punctuation.
func (f *file) lintErrorStrings() {
f.walk(func(node ast.Node) bool {
@@ -760,10 +774,7 @@
if s == "" {
return true
}
- first, _ := utf8.DecodeRuneInString(s)
- last, _ := utf8.DecodeLastRuneInString(s)
- isCap := unicode.IsUpper(first)
- isPunct := last == '.' || last == ':' || last == '!'
+ isCap, isPunct := lintCapAndPunct(s)
var msg string
switch {
case isCap && isPunct:
diff --git a/testdata/errors.go b/testdata/errors.go
index e6aabdd..2882738 100644
--- a/testdata/errors.go
+++ b/testdata/errors.go
@@ -28,6 +28,8 @@
func g(x int) error {
if x < 1 {
return fmt.Errorf("This %d is too low", x) // MATCH /error strings.*not be capitalized/
+ } else if x == 0 {
+ return fmt.Errorf("XML time") // ok
}
return errors.New(`too much stuff.`) // MATCH /error strings.*not end with punctuation/
}