Flag ALL_CAPS and kLeadingKay names specifically so they get good messages.
diff --git a/lint.go b/lint.go
index ed4fe0f..21e1678 100644
--- a/lint.go
+++ b/lint.go
@@ -14,6 +14,7 @@
"go/parser"
"go/printer"
"go/token"
+ "regexp"
"strings"
"unicode"
)
@@ -197,6 +198,8 @@
})
}
+var allCapsRE = regexp.MustCompile(`^[A-Z0-9_]+$`)
+
// lintNames examines all names in the file.
// It complains if any use underscores or incorrect known initialisms.
func (f *file) lintNames() {
@@ -209,6 +212,17 @@
if id.Name == "_" {
return
}
+
+ // Handle two common styles from other languages that don't belong in Go.
+ if len(id.Name) >= 5 && allCapsRE.MatchString(id.Name) && strings.Contains(id.Name, "_") {
+ f.errorf(id, 0.8, "don't use ALL_CAPS in Go names; use CamelCase")
+ return
+ }
+ if len(id.Name) > 2 && id.Name[0] == 'k' && id.Name[1] >= 'A' && id.Name[1] <= 'Z' {
+ should := string(id.Name[1]+'a'-'A') + id.Name[2:]
+ f.errorf(id, 0.8, "don't use leading k in Go names; %s %s should be %s", thing, id.Name, should)
+ }
+
should := lintName(id.Name)
if id.Name == should {
return
diff --git a/testdata/names.go b/testdata/names.go
index b2bd163..f38a694 100644
--- a/testdata/names.go
+++ b/testdata/names.go
@@ -17,3 +17,12 @@
x := foo_proto.Blah{} // should be okay
}
+
+// Common styles in other languages that don't belong in Go.
+const (
+ CPP_CONST = 1 // MATCH /ALL_CAPS.*CamelCase/
+ kLeadingKay = 2 // MATCH /k.*leadingKay/
+
+ HTML = 3 // okay; no underscore
+ X509B = 4 // ditto
+)