8000 go/lint: only report kBlah error for constants and top level vars · golang/lint@8f45f77 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on May 9, 2021. It is now read-only.

Commit 8f45f77

Browse files
committed
go/lint: only report kBlah error for constants and top level vars
The error for variables that start with k followed by a variable name, such as "kBlah", has a lot of false positives. It's intended to avoid cases where users try to follow the Google C++ style guide or hungarian notation for constants. So it should only be reported for constants and top-level variables because those are the cases where users make this mistake. Change-Id: I7e862dbc1013707b6b9ebaa72ee07fb623dd0ce8 Reviewed-on: https://go-review.googlesource.com/c/154339 Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org> Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 93c0bb5 commit 8f45f77

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lint.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,18 @@ var knownNameExceptions = map[string]bool{
540540
"kWh": true,
541541
}
542542

543+
func isInTopLevel(f *ast.File, ident *ast.Ident) bool {
544+
path, _ := astutil.PathEnclosingInterval(f, ident.Pos(), ident.End())
545+
for _, f := range path {
546+
switch f.(type) {
547+
case *ast.File, *ast.GenDecl, *ast.ValueSpec, *ast.Ident:
548+
continue
549+
}
550+
return false
551+
}
552+
return true
553+
}
554+
543555
// lintNames examines all names in the file.
544556
// It complains if any use underscores or incorrect known initialisms.
545557
func (f *file) lintNames() {
@@ -572,9 +584,11 @@ func (f *file) lintNames() {
572584
return
573585
}
574586
}
575-
if len(id.Name) > 2 && id.Name[0] == 'k' && id.Name[1] >= 'A' && id.Name[1] <= 'Z' {
576-
should := string(id.Name[1]+'a'-'A') + id.Name[2:]
577-
f.errorf(id, 0.8, link(styleGuideBase+"#mixed-caps"), category("naming"), "don't use leading k in Go names; %s %s should be %s", thing, id.Name, should)
587+
if thing == "const" || (thing == "var" && isInTopLevel(f.f, id)) {
588+
if len(id.Name) > 2 && id.Name[0] == 'k' && id.Name[1] >= 'A' && id.Name[1] <= 'Z' {
589+
should := string(id.Name[1]+'a'-'A') + id.Name[2:]
590+
f.errorf(id, 0.8, link(styleGuideBase+"#mixed-caps"), category("naming"), "don't use leading k in Go names; %s %s should be %s", thing, id.Name, should)
591+
}
578592
}
579593

580594
should := lintName(id.Name)

testdata/names.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ const (
6464
V1_10_5 = 5 // okay; fewer than two uppercase letters
6565
)
6666

67+
var kVarsAreSometimesUsedAsConstants = 0 // MATCH /k.*varsAreSometimesUsedAsConstants/
68+
var (
69+
kVarsAreSometimesUsedAsConstants2 = 0 // MATCH /k.*varsAreSometimesUsedAsConstants2/
70+
)
71+
72+
var kThisIsNotOkay = struct { // MATCH /k.*thisIsNotOkay/
73+
kThisIsOkay bool
74+
}{}
75+
76+
func kThisIsOkay() { // this is okay because this is a function name
77+
var kThisIsAlsoOkay = 1 // this is okay because this is a non-top-level variable
78+
_ = kThisIsAlsoOkay
79+
const kThisIsNotOkay = 2 // MATCH /k.*thisIsNotOkay/
80+
}
81+
82+
var anotherFunctionScope = func() {
83+
var kThisIsOkay = 1 // this is okay because this is a non-top-level variable
84+
_ = kThisIsOkay
85+
const kThisIsNotOkay = 2 // MATCH /k.*thisIsNotOkay/}
86+
}
87+
6788
func f(bad_name int) {} // MATCH /underscore.*func parameter.*bad_name/
6889
func g() (no_way int) { return 0 } // MATCH /underscore.*func result.*no_way/
6990
func (t *t_wow) f(more_under string) {} // MATCH /underscore.*method parameter.*more_under/

0 commit comments

Comments
 (0)
0