-
Notifications
You must be signed in to change notification settings - Fork 285
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
Showing
6 changed files
with
105 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,54 @@ | ||
package rule | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"github.com/mgechev/revive/lint" | ||
) | ||
|
||
// UseAnyRule lints given else constructs. | ||
type UseAnyRule struct{} | ||
|
||
// Apply applies the rule to given file. | ||
func (r *UseAnyRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { | ||
var failures []lint.Failure | ||
|
||
walker := lintUseAny{ | ||
onFailure: func(failure lint.Failure) { | ||
failures = append(failures, failure) | ||
}, | ||
} | ||
fileAst := file.AST | ||
ast.Walk(walker, fileAst) | ||
|
||
return failures | ||
} | ||
|
||
// Name returns the rule name. | ||
func (r *UseAnyRule) Name() string { | ||
return "use-any" | ||
} | ||
|
||
type lintUseAny struct { | ||
onFailure func(lint.Failure) | ||
} | ||
|
||
func (w lintUseAny) Visit(n ast.Node) ast.Visitor { | ||
it, ok := n.(*ast.InterfaceType) | ||
if !ok { | ||
return w | ||
} | ||
|
||
if len(it.Methods.List) != 0 { | ||
return w // it is not and empty interface | ||
} | ||
|
||
w.onFailure(lint.Failure{ | ||
Node: n, | ||
Confidence: 1, | ||
Category: "naming", | ||
Failure: "since GO 1.18 'interface{}' can be replaced by 'any'", | ||
}) | ||
|
||
return w | ||
} |
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,11 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
func TestUseAny(t *testing.T) { | ||
testRule(t, "use-any", &rule.UseAnyRule{}) | ||
} |
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,31 @@ | ||
package pkg | ||
|
||
var i interface{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/ | ||
|
||
type t interface{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/ | ||
type a = interface{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/ | ||
|
||
func any1(a interface{}) { // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/ | ||
m1 := map[interface{}]string{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/ | ||
m2 := map[int]interface{}{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/ | ||
a := []interface{}{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/ | ||
m3 := make(map[int]interface{}, 1) // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/ | ||
a2 := make([]interface{}, 2) // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/ | ||
} | ||
|
||
func any2(a int) interface{} {} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/ | ||
|
||
var ni interface{ Close() } | ||
|
||
type nt interface{ Close() } | ||
type na = interface{ Close() } | ||
|
||
func nany1(a interface{ Close() }) { | ||
nm1 := map[interface{ Close() }]string{} | ||
nm2 := map[int]interface{ Close() }{} | ||
na := []interface{ Close() }{} | ||
nm3 := make(map[int]interface{ Close() }, 1) | ||
na2 := make([]interface{ Close() }, 2) | ||
} | ||
|
||
func nany2(a int) interface{ Close() } {} |