-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
redundant-old-build-tag
rule
- Loading branch information
1 parent
1425e2f
commit dd996b2
Showing
8 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//go:build tag | ||
|
||
package pkg |