mirror of
https://github.com/mgechev/revive.git
synced 2025-03-17 20:57:58 +02:00
feat: add redundant-build-tag
rule (#1135)
This commit is contained in:
parent
19834d40a4
commit
7f769f8c16
@ -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
|
||||
|
||||
|
@ -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)
|
||||
@ -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.
|
||||
|
@ -98,6 +98,7 @@ var allRules = append([]lint.Rule{
|
||||
&rule.CommentsDensityRule{},
|
||||
&rule.FileLengthLimitRule{},
|
||||
&rule.FilenameFormatRule{},
|
||||
&rule.RedundantBuildTagRule{},
|
||||
}, defaultRules...)
|
||||
|
||||
var allFormatters = []lint.Formatter{
|
||||
|
43
rule/redundant_build_tag.go
Normal file
43
rule/redundant_build_tag.go
Normal file
@ -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
test/redundant_build_tag_test.go
Normal file
20
test/redundant_build_tag_test.go
Normal file
@ -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
testdata/redundant_build_tag.go
vendored
Normal file
6
testdata/redundant_build_tag.go
vendored
Normal file
@ -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
testdata/redundant_build_tag_go116.go
vendored
Normal file
6
testdata/redundant_build_tag_go116.go
vendored
Normal file
@ -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
testdata/redundant_build_tag_no_failure.go
vendored
Normal file
3
testdata/redundant_build_tag_no_failure.go
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
//go:build tag
|
||||
|
||||
package pkg
|
Loading…
x
Reference in New Issue
Block a user