[lint] Suggest using alternative names for reserved word variants

82 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Apr 13, 2018, 3:30:32 PM4/13/18
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

Suggest using alternative names for reserved word variants

This PR displays an additional message for improper names when they are variants of a reserved word. A name is considered a variant of a reserved word if, when stripped of underscores and converted to a lowercase string, the name matches a reserved word.

Additionally, substitutions are suggested for `type` (`typ`) and `struct` (`structure`). More substitutions can easily be added.

Example:
```
don't use underscores in Go names; var type_ should be type (to avoid conflict with this reserved word, choose 'typ')
```

Change-Id: I5faf36f2f90f6fa32de27a0f6eee355a920501b0
GitHub-Last-Rev: ad1e3daa46bec5912e2356989fba294062d8a1b1
GitHub-Pull-Request: golang/lint#394
---
M lint.go
1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/lint.go b/lint.go
index 46bd45f..c7c4a68 100644
--- a/lint.go
+++ b/lint.go
@@ -540,6 +540,20 @@
"kWh": true,
}

+// reservedWordSubstitutions is a set of reserved words mapped to their corresponding substitution.
+// An empty string means there is no obvious substitution.
+var reservedWordSubstitutions = map[string]string{
+ // reserved words with substitutions
+ "type": "typ",
+ "struct": "structure",
+ // reserved words without substitutions
+ "break": "", "case": "", "chan": "", "const": "", "continue": "",
+ "default": "", "defer": "", "else": "", "fallthrough": "", "for": "",
+ "func": "", "go": "", "goto": "", "if": "", "import": "",
+ "interface": "", "map": "", "package": "", "range": "", "return": "",
+ "select": "", "switch": "", "var": "",
+}
+
// lintNames examines all names in the file.
// It complains if any use underscores or incorrect known initialisms.
func (f *file) lintNames() {
@@ -551,6 +565,18 @@
f.errorf(f.f, 1, link("http://golang.org/doc/effective_go.html#package-names"), category("mixed-caps"), "don't use MixedCaps in package name; %s should be %s", f.f.Name.Name, strings.ToLower(f.f.Name.Name))
}

+ checkReserved := func(id *ast.Ident) string {
+ simpleName := strings.Replace(id.Name, "_", "", -1)
+ simpleName = strings.ToLower(simpleName)
+ if substitution, exists := reservedWordSubstitutions[simpleName]; exists {
+ if substitution == "" {
+ return " (to avoid conflict with a reserved word, select an alternative)"
+ }
+ return " (to avoid conflict with this reserved word, choose '" + substitution + "')"
+ }
+ return ""
+ }
+
check := func(id *ast.Ident, thing string) {
if id.Name == "_" {
return
@@ -561,12 +587,12 @@

// 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, link(styleGuideBase+"#mixed-caps"), category("naming"), "don't use ALL_CAPS in Go names; use CamelCase")
+ f.errorf(id, 0.8, link(styleGuideBase+"#mixed-caps"), category("naming"), "don't use ALL_CAPS in Go names; use CamelCase%s", checkReserved(id))
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, link(styleGuideBase+"#mixed-caps"), category("naming"), "don't use leading k in Go names; %s %s should be %s", thing, id.Name, should)
+ f.errorf(id, 0.8, link(styleGuideBase+"#mixed-caps"), category("naming"), "don't use leading k in Go names; %s %s should be %s%s", thing, id.Name, should, checkReserved(id))
}

should := lintName(id.Name)
@@ -575,10 +601,11 @@
}

if len(id.Name) > 2 && strings.Contains(id.Name[1:], "_") {
- f.errorf(id, 0.9, link("http://golang.org/doc/effective_go.html#mixed-caps"), category("naming"), "don't use underscores in Go names; %s %s should be %s", thing, id.Name, should)
+ f.errorf(id, 0.9, link("http://golang.org/doc/effective_go.html#mixed-caps"), category("naming"), "don't use underscores in Go names; %s %s should be %s%s", thing, id.Name, should, checkReserved(id))
return
}
- f.errorf(id, 0.8, link(styleGuideBase+"#initialisms"), category("naming"), "%s %s should be %s", thing, id.Name, should)
+ f.errorf(id, 0.8, link(styleGuideBase+"#initialisms"), category("naming"), "%s %s should be %s", thing, id.Name, should, checkReserved(id))
+ return
}
checkList := func(fl *ast.FieldList, thing string) {
if fl == nil {

To view, visit change 106976. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: lint
Gerrit-Branch: master
Gerrit-Change-Id: I5faf36f2f90f6fa32de27a0f6eee355a920501b0
Gerrit-Change-Number: 106976
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Eric Dubé <eric.al...@gmail.com>
Gerrit-MessageType: newchange

Gobot Gobot (Gerrit)

unread,
Apr 13, 2018, 3:30:35 PM4/13/18
to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
Within the next week or so, a maintainer will review your change and provide
feedback. See https://golang.org/doc/contribute.html#review for more info and
tips to get your patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11, it means that this CL will be reviewed as part of the next development
cycle. See https://golang.org/s/release for more details.

View Change

    To view, visit change 106976. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: lint
    Gerrit-Branch: master
    Gerrit-Change-Id: I5faf36f2f90f6fa32de27a0f6eee355a920501b0
    Gerrit-Change-Number: 106976
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Eric Dubé <eric.al...@gmail.com>
    Gerrit-CC: Gobot Gobot <go...@golang.org>
    Gerrit-Comment-Date: Fri, 13 Apr 2018 19:30:34 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Katie Hockman (Gerrit)

    unread,
    Feb 11, 2019, 2:12:20 PM2/11/19
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Gobot Gobot, golang-co...@googlegroups.com

    Please update the commit message to meet the guidelines in the wiki: https://github.com/golang/go/wiki/CommitMessage#commit-messages

    This is a significant enough change that a Github issue with some discussion from the package owners is needed. If one has not already been created, please create one and associate it with the PR.

    View Change

      To view, visit change 106976. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: lint
      Gerrit-Branch: master
      Gerrit-Change-Id: I5faf36f2f90f6fa32de27a0f6eee355a920501b0
      Gerrit-Change-Number: 106976
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Eric Dubé <eric.al...@gmail.com>
      Gerrit-CC: Gobot Gobot <go...@golang.org>
      Gerrit-CC: Katie Hockman <ka...@golang.org>
      Gerrit-Comment-Date: Mon, 11 Feb 2019 19:06:10 +0000

      Daniel Martí (Gerrit)

      unread,
      May 8, 2021, 4:25:58 PM5/8/21
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Katie Hockman, Go Bot, golang-co...@googlegroups.com

      Daniel Martí abandoned this change.

      View Change

      Abandoned Thank you for submitting this patch! As proposed[1], we are freezing and deprecating golint. There's no drop-in replacement to golint per se, but you should find that Staticcheck[2] works well in encouraging good Go code, much like golint did in the past, since it also includes style checks. There's always gofmt and "go vet" too, of course. If you would like to contribute further, I'd encourage you to engage Staticcheck's issue tracker[3] or look at vet's open issues[4], as they are both actively maintained. If you have an idea that doesn't fit into either of those tools, you could look at other Go linters[5], or write your own - these days it's fairly straightforward with go/analysis[6]. To help avoid confusion, I'm closing all CLs before we freeze the repository. If you have any feedback, you can leave a comment on the proposal thread where it was decided to deprecate golint - though note that the proposal has been accepted for nearly a year. Thanks! [1] https://github.com/golang/go/issues/38968 [2] https://staticcheck.io/ [3] https://github.com/dominikh/go-tools/issues [4] https://github.com/golang/go/issues?q=is%3Aissue+is%3Aopen+cmd%2Fvet+in%3Atitle [5] https://github.com/golangci/awesome-go-linters [6] https://pkg.go.dev/golang.org/x/tools/go/analysis

      To view, visit change 106976. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: lint
      Gerrit-Branch: master
      Gerrit-Change-Id: I5faf36f2f90f6fa32de27a0f6eee355a920501b0
      Gerrit-Change-Number: 106976
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Eric Dubé <eric.al...@gmail.com>
      Gerrit-CC: Go Bot <go...@golang.org>
      Gerrit-CC: Katie Hockman <ka...@golang.org>
      Gerrit-MessageType: abandon
      Reply all
      Reply to author
      Forward
      0 new messages