Skip to content

Commit

Permalink
feat: add redundant-old-build-tag rule
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Nov 19, 2024
1 parent 1425e2f commit dd996b2
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-old-build-tag`](./RULES_DESCRIPTIONS.md#redundant-old-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-old-build-tag](#redundant-old-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-old-build-tag
_Description_: This rule warns about redundant old 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.RedundantOldBuildTagRule{},
}, defaultRules...)

var allFormatters = []lint.Formatter{
Expand Down
43 changes: 43 additions & 0 deletions rule/redundant_old_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"
)

// RedundantOldBuildTagRule lints the presence of old build tags `// +build`.
type RedundantOldBuildTagRule 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 (*RedundantOldBuildTagRule) 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 old build tag "// +build" is redundant and can be removed`,
})
return failures
}
}
}

return failures
}

// Name returns the rule name.
func (*RedundantOldBuildTagRule) Name() string {
return "redundant-old-build-tag"
}
20 changes: 20 additions & 0 deletions test/redundant_old_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 TestRedundantOldBuildTagRule(t *testing.T) {
testRule(t, "redundant_old_build_tag", &rule.RedundantOldBuildTagRule{}, &lint.RuleConfig{})
}

func TestRedundantOldBuildTagRuleNoFailure(t *testing.T) {
testRule(t, "redundant_old_build_tag_no_failure", &rule.RedundantOldBuildTagRule{}, &lint.RuleConfig{})
}

func TestRedundantOldBuildTagRuleGo116(t *testing.T) {
testRule(t, "redundant_old_build_tag_go116", &rule.RedundantOldBuildTagRule{}, &lint.RuleConfig{})
}
6 changes: 6 additions & 0 deletions testdata/redundant_old_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 old build tag "// +build" is redundant and can be removed/

package pkg
6 changes: 6 additions & 0 deletions testdata/redundant_old_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_old_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 dd996b2

Please sign in to comment.