-
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.
- Loading branch information
1 parent
1425e2f
commit 1a2732a
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" | ||
) | ||
|
||
// 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" | ||
} |
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 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{}) | ||
} |
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 build tag "// +build" is redundant since Go 1.17 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 |