Skip to content

Commit 7f769f8

Browse files
authored
feat: add redundant-build-tag rule (#1135)
1 parent 19834d4 commit 7f769f8

8 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
550550
| [`comments-density`](./RULES_DESCRIPTIONS.md#comments-density) | int (defaults to 0) | Enforces a minimum comment / code relation | no | no |
551551
| [`file-length-limit`](./RULES_DESCRIPTIONS.md#file-length-limit) | map (optional)| Enforces a maximum number of lines per file | no | no |
552552
| [`filename-format`](./RULES_DESCRIPTIONS.md#filename-format) | regular expression (optional) | Enforces the formatting of filenames | no | no |
553+
| [`redundant-build-tag`](./RULES_DESCRIPTIONS.md#redundant-build-tag) | n/a | Warns about redundant `// +build` comment lines | no | no |
553554

554555
## Configurable rules
555556

RULES_DESCRIPTIONS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ List of all available rules.
6565
- [receiver-naming](#receiver-naming)
6666
- [redefines-builtin-id](#redefines-builtin-id)
6767
- [redundant-import-alias](#redundant-import-alias)
68+
- [redundant-build-tag](#redundant-build-tag)
6869
- [string-format](#string-format)
6970
- [string-of-int](#string-of-int)
7071
- [struct-tag](#struct-tag)
@@ -799,6 +800,13 @@ _Description_: This rule warns on redundant import aliases. This happens when th
799800
800801
_Configuration_: N/A
801802
803+
## redundant-build-tag
804+
805+
_Description_: This rule warns about redundant build tag comments `// +build` when `//go:build` is present.
806+
`gofmt` in Go 1.17+ automatically adds the `//go:build` constraint, making the `// +build` comment unnecessary.
807+
808+
_Configuration_: N/A
809+
802810
## string-format
803811

804812
_Description_: This rule allows you to configure a list of regular expressions that string literals in certain function calls are checked against.

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ var allRules = append([]lint.Rule{
9898
&rule.CommentsDensityRule{},
9999
&rule.FileLengthLimitRule{},
100100
&rule.FilenameFormatRule{},
101+
&rule.RedundantBuildTagRule{},
101102
}, defaultRules...)
102103

103104
var allFormatters = []lint.Formatter{

rule/redundant_build_tag.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package rule
2+
3+
import (
4+
"strings"
5+
6+
"github.com/mgechev/revive/lint"
7+
)
8+
9+
// RedundantBuildTagRule lints the presence of redundant build tags.
10+
type RedundantBuildTagRule struct{}
11+
12+
// Apply triggers if an old build tag `// +build` is found after a new one `//go:build`.
13+
// `//go:build` comments are automatically added by gofmt when Go 1.17+ is used.
14+
// See https://pkg.go.dev/cmd/go#hdr-Build_constraints
15+
func (*RedundantBuildTagRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
16+
var failures []lint.Failure
17+
18+
for _, group := range file.AST.Comments {
19+
hasGoBuild := false
20+
for _, comment := range group.List {
21+
if strings.HasPrefix(comment.Text, "//go:build ") {
22+
hasGoBuild = true
23+
continue
24+
}
25+
if hasGoBuild && strings.HasPrefix(comment.Text, "// +build ") {
26+
failures = append(failures, lint.Failure{
27+
Category: "style",
28+
Confidence: 1,
29+
Node: comment,
30+
Failure: `The build tag "// +build" is redundant since Go 1.17 and can be removed`,
31+
})
32+
return failures
33+
}
34+
}
35+
}
36+
37+
return failures
38+
}
39+
40+
// Name returns the rule name.
41+
func (*RedundantBuildTagRule) Name() string {
42+
return "redundant-build-tag"
43+
}

test/redundant_build_tag_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mgechev/revive/lint"
7+
"github.com/mgechev/revive/rule"
8+
)
9+
10+
func TestRedundantBuildTagRule(t *testing.T) {
11+
testRule(t, "redundant_build_tag", &rule.RedundantBuildTagRule{}, &lint.RuleConfig{})
12+
}
13+
14+
func TestRedundantBuildTagRuleNoFailure(t *testing.T) {
15+
testRule(t, "redundant_build_tag_no_failure", &rule.RedundantBuildTagRule{}, &lint.RuleConfig{})
16+
}
17+
18+
func TestRedundantBuildTagRuleGo116(t *testing.T) {
19+
testRule(t, "redundant_build_tag_go116", &rule.RedundantBuildTagRule{}, &lint.RuleConfig{})
20+
}

testdata/redundant_build_tag.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build tag
2+
// +build tag
3+
4+
// MATCH:2 /The build tag "// +build" is redundant since Go 1.17 and can be removed/
5+
6+
package pkg

testdata/redundant_build_tag_go116.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// +build tag
2+
3+
// This means that the file is for Go less than 1.17 because
4+
// gofmt automatically adds the new build tag `//go:build` when Go 1.17+ is used.
5+
6+
package pkg
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//go:build tag
2+
3+
package pkg

0 commit comments

Comments
 (0)