Skip to content

Commit 671c55d

Browse files
authored
adds rule use-any (#660)
1 parent 318db94 commit 671c55d

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
481481
| [`useless-break`](./RULES_DESCRIPTIONS.md#useless-break) | n/a | Warns on useless `break` statements in case clauses | no | no |
482482
| [`banned-characters`](./RULES_DESCRIPTIONS.md#banned-characters) | n/a | Checks banned characters in identifiers | no | no |
483483
| [`optimize-operands-order`](./RULES_DESCRIPTIONS.md#optimize-operands-order) | n/a | Checks inefficient conditional expressions | no | no |
484+
| [`use-any`](./RULES_DESCRIPTIONS.md#use-any) | n/a | Proposes to replace `interface{}` with its alias `any` | no | no |
484485

485486
## Configurable rules
486487

RULES_DESCRIPTIONS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ List of all available rules.
7070
- [unreachable-code](#unreachable-code)
7171
- [unused-parameter](#unused-parameter)
7272
- [unused-receiver](#unused-receiver)
73+
- [use-any](#use-any)
7374
- [useless-break](#useless-break)
7475
- [waitgroup-by-value](#waitgroup-by-value)
7576

@@ -670,6 +671,12 @@ _Description_: This rule warns on unused method receivers. Methods with unused r
670671

671672
_Configuration_: N/A
672673

674+
## use-any
675+
676+
_Description_: Since GO 1.18, `interface{}` has an alias: `any`. This rule proposes to replace instances of `interface{}` with `any`.
677+
678+
_Configuration_: N/A
679+
673680
## useless-break
674681

675682
_Description_: This rule warns on useless `break` statements in case clauses of switch and select statements. GO, unlike other programming languages like C, only executes statements of the selected case while ignoring the subsequent case clauses.

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ var allRules = append([]lint.Rule{
8484
&rule.TimeEqualRule{},
8585
&rule.BannedCharsRule{},
8686
&rule.OptimizeOperandsOrderRule{},
87+
&rule.UseAnyRule{},
8788
}, defaultRules...)
8889

8990
var allFormatters = []lint.Formatter{

rule/use-any.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package rule
2+
3+
import (
4+
"go/ast"
5+
6+
"github.com/mgechev/revive/lint"
7+
)
8+
9+
// UseAnyRule lints given else constructs.
10+
type UseAnyRule struct{}
11+
12+
// Apply applies the rule to given file.
13+
func (r *UseAnyRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
14+
var failures []lint.Failure
15+
16+
walker := lintUseAny{
17+
onFailure: func(failure lint.Failure) {
18+
failures = append(failures, failure)
19+
},
20+
}
21+
fileAst := file.AST
22+
ast.Walk(walker, fileAst)
23+
24+
return failures
25+
}
26+
27+
// Name returns the rule name.
28+
func (r *UseAnyRule) Name() string {
29+
return "use-any"
30+
}
31+
32+
type lintUseAny struct {
33+
onFailure func(lint.Failure)
34+
}
35+
36+
func (w lintUseAny) Visit(n ast.Node) ast.Visitor {
37+
it, ok := n.(*ast.InterfaceType)
38+
if !ok {
39+
return w
40+
}
41+
42+
if len(it.Methods.List) != 0 {
43+
return w // it is not and empty interface
44+
}
45+
46+
w.onFailure(lint.Failure{
47+
Node: n,
48+
Confidence: 1,
49+
Category: "naming",
50+
Failure: "since GO 1.18 'interface{}' can be replaced by 'any'",
51+
})
52+
53+
return w
54+
}

test/use-any_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mgechev/revive/rule"
7+
)
8+
9+
func TestUseAny(t *testing.T) {
10+
testRule(t, "use-any", &rule.UseAnyRule{})
11+
}

testdata/use-any.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package pkg
2+
3+
var i interface{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/
4+
5+
type t interface{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/
6+
type a = interface{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/
7+
8+
func any1(a interface{}) { // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/
9+
m1 := map[interface{}]string{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/
10+
m2 := map[int]interface{}{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/
11+
a := []interface{}{} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/
12+
m3 := make(map[int]interface{}, 1) // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/
13+
a2 := make([]interface{}, 2) // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/
14+
}
15+
16+
func any2(a int) interface{} {} // MATCH /since GO 1.18 'interface{}' can be replaced by 'any'/
17+
18+
var ni interface{ Close() }
19+
20+
type nt interface{ Close() }
21+
type na = interface{ Close() }
22+
23+
func nany1(a interface{ Close() }) {
24+
nm1 := map[interface{ Close() }]string{}
25+
nm2 := map[int]interface{ Close() }{}
26+
na := []interface{ Close() }{}
27+
nm3 := make(map[int]interface{ Close() }, 1)
28+
na2 := make([]interface{ Close() }, 2)
29+
}
30+
31+
func nany2(a int) interface{ Close() } {}

0 commit comments

Comments
 (0)