Skip to content

Commit

Permalink
feat: add redundant-build-tag rule
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Nov 20, 2024
1 parent 1425e2f commit 1a2732a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
| [`comments-density`](./RULES_DESCRIPTIONS.md#comments-density) | int (defaults to 0) | Enforces a minimum comment / code relation | no | no |
| [`file-length-limit`](./RULES_DESCRIPTIONS.md#file-length-limit) | map (optional)| Enforces a maximum number of lines per file | no | no |
| [`filename-format`](./RULES_DESCRIPTIONS.md#filename-format) | regular expression (optional) | Enforces the formatting of filenames | no | no |
| [`redundant-build-tag`](./RULES_DESCRIPTIONS.md#redundant-build-tag) | n/a | Warns about redundant `// +build` comment lines | no | no |

## Configurable rules

Expand Down
8 changes: 8 additions & 0 deletions RULES_DESCRIPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ List of all available rules.
- [receiver-naming](#receiver-naming)
- [redefines-builtin-id](#redefines-builtin-id)
- [redundant-import-alias](#redundant-import-alias)
- [redundant-build-tag](#redundant-build-tag)
- [string-format](#string-format)
- [string-of-int](#string-of-int)
- [struct-tag](#struct-tag)
Expand Down Expand Up @@ -799,6 +800,13 @@ _Description_: This rule warns on redundant import aliases. This happens when th
_Configuration_: N/A
## redundant-build-tag
_Description_: This rule warns about redundant build tag comments `// +build` when `//go:build` is present.
`gofmt` in Go 1.17+ automatically adds the `//go:build` constraint, making the `// +build` comment unnecessary.

_Configuration_: N/A

## string-format

_Description_: This rule allows you to configure a list of regular expressions that string literals in certain function calls are checked against.
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ var allRules = append([]lint.Rule{
&rule.CommentsDensityRule{},
&rule.FileLengthLimitRule{},
&rule.FilenameFormatRule{},
&rule.RedundantBuildTagRule{},
}, defaultRules...)

var allFormatters = []lint.Formatter{
Expand Down
43 changes: 43 additions & 0 deletions rule/redundant_build_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package rule

import (
"strings"

"github.com/mgechev/revive/lint"
)

// RedundantBuildTagRule lints the presence of redundant build tags.
type RedundantBuildTagRule struct{}

// Apply triggers if an old build tag `// +build` is found after a new one `//go:build`.
// `//go:build` comments are automatically added by gofmt when Go 1.17+ is used.
// See https://pkg.go.dev/cmd/go#hdr-Build_constraints
func (*RedundantBuildTagRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []lint.Failure

for _, group := range file.AST.Comments {
hasGoBuild := false
for _, comment := range group.List {
if strings.HasPrefix(comment.Text, "//go:build ") {
hasGoBuild = true
continue
}
if hasGoBuild && strings.HasPrefix(comment.Text, "// +build ") {
failures = append(failures, lint.Failure{
Category: "style",
Confidence: 1,
Node: comment,
Failure: `The build tag "// +build" is redundant since Go 1.17 and can be removed`,
})
return failures
}
}
}

return failures
}

// Name returns the rule name.
func (*RedundantBuildTagRule) Name() string {
return "redundant-build-tag"
}
20 changes: 20 additions & 0 deletions test/redundant_build_tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test

import (
"testing"

"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)

func TestRedundantBuildTagRule(t *testing.T) {
testRule(t, "redundant_build_tag", &rule.RedundantBuildTagRule{}, &lint.RuleConfig{})
}

func TestRedundantBuildTagRuleNoFailure(t *testing.T) {
testRule(t, "redundant_build_tag_no_failure", &rule.RedundantBuildTagRule{}, &lint.RuleConfig{})
}

func TestRedundantBuildTagRuleGo116(t *testing.T) {
testRule(t, "redundant_build_tag_go116", &rule.RedundantBuildTagRule{}, &lint.RuleConfig{})
}
6 changes: 6 additions & 0 deletions testdata/redundant_build_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build tag
// +build tag

// MATCH:2 /The build tag "// +build" is redundant since Go 1.17 and can be removed/

package pkg
6 changes: 6 additions & 0 deletions testdata/redundant_build_tag_go116.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// +build tag

// This means that the file is for Go less than 1.17 because
// gofmt automatically adds the new build tag `//go:build` when Go 1.17+ is used.

package pkg
3 changes: 3 additions & 0 deletions testdata/redundant_build_tag_no_failure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//go:build tag

package pkg

0 comments on commit 1a2732a

Please sign in to comment.