mirror of
https://github.com/mgechev/revive.git
synced 2024-11-24 08:32:22 +02:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
5fd2a31b91
16
README.md
16
README.md
@ -26,11 +26,15 @@ Here's how `revive` is different from `golint`:
|
||||
## Who uses Revive
|
||||
|
||||
- [`tidb`](https://github.com/pingcap/tidb) - TiDB is a distributed HTAP database compatible with the MySQL protocol
|
||||
- [`grafana`](https://github.com/grafana/grafana) - The tool for beautiful monitoring and metric analytics & dashboards for Graphite, InfluxDB & Prometheus & More
|
||||
- [`etcd`](https://github.com/etcd-io/etcd) - Distributed reliable key-value store for the most critical data of a distributed system
|
||||
- [`ferret`](https://github.com/MontFerret/ferret) - Declarative web scraping
|
||||
- [`gopass`](https://github.com/gopasspw/gopass) - The slightly more awesome standard unix password manager for teams
|
||||
- [`gitea`](https://github.com/go-gitea/gitea) - Git with a cup of tea, painless self-hosted git service
|
||||
- [`excelize`](https://github.com/360EntSecGroup-Skylar/excelize) - Go library for reading and writing Microsoft Excel™ (XLSX) files
|
||||
- [`aurora`](https://github.com/xuri/aurora) - aurora is a web-based Beanstalk queue server console written in Go
|
||||
- [`soar`](https://github.com/XiaoMi/soar) - SQL Optimizer And Rewriter
|
||||
- [`gorush`](https://github.com/appleboy/gorush) - A push notification server written in Go (Golang)
|
||||
|
||||
*Open a PR to add your project*.
|
||||
|
||||
@ -294,6 +298,8 @@ List of all available rules. The rules ported from `golint` are left unchanged a
|
||||
| [`call-to-gc`](./RULES_DESCRIPTIONS.md#call-to-gc) | n/a | Warns on explicit call to the garbage collector | no | no |
|
||||
| [`duplicated-imports`](./RULES_DESCRIPTIONS#duplicated-imports) | n/a | Looks for packages that are imported two or more times | no | no |
|
||||
| [`import-shadowing`](./RULES_DESCRIPTIONS.md#import-shadowing) | n/a | Spots identifiers that shadow an import | no | no |
|
||||
| [`bare-return`](./RULES_DESCRIPTIONS#bare-return) | n/a | Warns on bare returns | no | no |
|
||||
| [`unused-receiver`](./RULES_DESCRIPTIONS.md#unused-receiver) | n/a | Suggests to rename or remove unused method receivers | no | no |
|
||||
|
||||
## Configurable rules
|
||||
|
||||
@ -434,13 +440,13 @@ Currently, type checking is enabled by default. If you want to run the linter wi
|
||||
:---: |:---: |:---: |:---: |:---: |:---: |
|
||||
[mgechev](https://github.com/mgechev) |[chavacava](https://github.com/chavacava) |[xuri](https://github.com/xuri) |[gsamokovarov](https://github.com/gsamokovarov) |[morphy2k](https://github.com/morphy2k) |[tamird](https://github.com/tamird) |
|
||||
|
||||
[<img alt="AragurDEV" src="https://avatars0.githubusercontent.com/u/11004008?v=4&s=117" width="117">](https://github.com/AragurDEV) |[<img alt="yangdiangzb" src="https://avatars3.githubusercontent.com/u/16643665?v=4&s=117" width="117">](https://github.com/yangdiangzb) |[<img alt="jamesmaidment" src="https://avatars3.githubusercontent.com/u/2050324?v=4&s=117" width="117">](https://github.com/jamesmaidment) |[<img alt="mapreal19" src="https://avatars2.githubusercontent.com/u/3055997?v=4&s=117" width="117">](https://github.com/mapreal19) |[<img alt="paul-at-start" src="https://avatars2.githubusercontent.com/u/5486775?v=4&s=117" width="117">](https://github.com/paul-at-start) |[<img alt="psapezhko" src="https://avatars3.githubusercontent.com/u/10865586?v=4&s=117" width="117">](https://github.com/psapezhko) |
|
||||
[<img alt="AragurDEV" src="https://avatars0.githubusercontent.com/u/11004008?v=4&s=117" width="117">](https://github.com/AragurDEV) |[<img alt="yangdiangzb" src="https://avatars3.githubusercontent.com/u/16643665?v=4&s=117" width="117">](https://github.com/yangdiangzb) |[<img alt="jamesmaidment" src="https://avatars3.githubusercontent.com/u/2050324?v=4&s=117" width="117">](https://github.com/jamesmaidment) |[<img alt="mapreal19" src="https://avatars2.githubusercontent.com/u/3055997?v=4&s=117" width="117">](https://github.com/mapreal19) |[<img alt="markelog" src="https://avatars0.githubusercontent.com/u/945528?v=4&s=117" width="117">](https://github.com/markelog) |[<img alt="paul-at-start" src="https://avatars2.githubusercontent.com/u/5486775?v=4&s=117" width="117">](https://github.com/paul-at-start) |
|
||||
:---: |:---: |:---: |:---: |:---: |:---: |
|
||||
[AragurDEV](https://github.com/AragurDEV) |[yangdiangzb](https://github.com/yangdiangzb) |[jamesmaidment](https://github.com/jamesmaidment) |[mapreal19](https://github.com/mapreal19) |[paul-at-start](https://github.com/paul-at-start) |[psapezhko](https://github.com/psapezhko) |
|
||||
[AragurDEV](https://github.com/AragurDEV) |[yangdiangzb](https://github.com/yangdiangzb) |[jamesmaidment](https://github.com/jamesmaidment) |[mapreal19](https://github.com/mapreal19) |[markelog](https://github.com/markelog) |[paul-at-start](https://github.com/paul-at-start) |
|
||||
|
||||
[<img alt="ridvansumset" src="https://avatars2.githubusercontent.com/u/26631560?v=4&s=117" width="117">](https://github.com/ridvansumset) |[<img alt="Jarema" src="https://avatars0.githubusercontent.com/u/7369771?v=4&s=117" width="117">](https://github.com/Jarema) |[<img alt="vkrol" src="https://avatars3.githubusercontent.com/u/153412?v=4&s=117" width="117">](https://github.com/vkrol) |[<img alt="haya14busa" src="https://avatars0.githubusercontent.com/u/3797062?v=4&s=117" width="117">](https://github.com/haya14busa) |
|
||||
:---: |:---: |:---: |:---: |
|
||||
[ridvansumset](https://github.com/ridvansumset) |[Jarema](https://github.com/Jarema) |[vkrol](https://github.com/vkrol) |[haya14busa](https://github.com/haya14busa) |
|
||||
[<img alt="psapezhko" src="https://avatars3.githubusercontent.com/u/10865586?v=4&s=117" width="117">](https://github.com/psapezhko) |[<img alt="ridvansumset" src="https://avatars2.githubusercontent.com/u/26631560?v=4&s=117" width="117">](https://github.com/ridvansumset) |[<img alt="Jarema" src="https://avatars0.githubusercontent.com/u/7369771?v=4&s=117" width="117">](https://github.com/Jarema) |[<img alt="vkrol" src="https://avatars3.githubusercontent.com/u/153412?v=4&s=117" width="117">](https://github.com/vkrol) |[<img alt="haya14busa" src="https://avatars0.githubusercontent.com/u/3797062?v=4&s=117" width="117">](https://github.com/haya14busa) |
|
||||
:---: |:---: |:---: |:---: |:---: |
|
||||
[psapezhko](https://github.com/psapezhko) |[ridvansumset](https://github.com/ridvansumset) |[Jarema](https://github.com/Jarema) |[vkrol](https://github.com/vkrol) |[haya14busa](https://github.com/haya14busa) |
|
||||
|
||||
## License
|
||||
|
||||
|
@ -7,6 +7,7 @@ List of all available rules.
|
||||
- [add-constant](#add-constant)
|
||||
- [argument-limit](#argument-limit)
|
||||
- [atomic](#atomic)
|
||||
- [bare-return](#bare-return)
|
||||
- [blank-imports](#blank-imports)
|
||||
- [bool-literal-in-expr](#bool-literal-in-expr)
|
||||
- [call-to-gc](#call-to-gc)
|
||||
@ -53,6 +54,7 @@ List of all available rules.
|
||||
- [unnecessary-stmt](#unnecessary-stmt)
|
||||
- [unreachable-code](#unreachable-code)
|
||||
- [unused-parameter](#unused-parameter)
|
||||
- [unused-receiver](#unused-receiver)
|
||||
- [waitgroup-by-value](#waitgroup-by-value)
|
||||
|
||||
## add-constant
|
||||
@ -93,6 +95,12 @@ _Description_: Check for commonly mistaken usages of the `sync/atomic` package
|
||||
|
||||
_Configuration_: N/A
|
||||
|
||||
## bare-return
|
||||
|
||||
_Description_: Warns on bare (a.k.a. naked) returns
|
||||
|
||||
_Configuration_: N/A
|
||||
|
||||
## blank-imports
|
||||
|
||||
_Description_: Blank import should be only in a main or test package, or have a comment justifying it.
|
||||
@ -444,6 +452,12 @@ _Description_: This rule warns on unused parameters. Functions or methods with u
|
||||
|
||||
_Configuration_: N/A
|
||||
|
||||
## unused-receiver
|
||||
|
||||
_Description_: This rule warns on unused method receivers. Methods with unused receivers can be a symptom of an unfinished refactoring or a bug.
|
||||
|
||||
_Configuration_: N/A
|
||||
|
||||
## waitgroup-by-value
|
||||
|
||||
_Description_: Function parameters that are passed by value, are in fact a copy of the original argument. Passing a copy of a `sync.WaitGroup` is usually not what the developer wants to do.
|
||||
|
@ -75,6 +75,8 @@ var allRules = append([]lint.Rule{
|
||||
&rule.CallToGCRule{},
|
||||
&rule.DuplicatedImportsRule{},
|
||||
&rule.ImportShadowingRule{},
|
||||
&rule.BareReturnRule{},
|
||||
&rule.UnusedReceiverRule{},
|
||||
}, defaultRules...)
|
||||
|
||||
var allFormatters = []lint.Formatter{
|
||||
|
33
fixtures/bare-return.go
Normal file
33
fixtures/bare-return.go
Normal file
@ -0,0 +1,33 @@
|
||||
package fixtures
|
||||
|
||||
func bare1() (int, int, error) {
|
||||
go func() (a int) {
|
||||
return // MATCH /avoid using bare returns, please add return expressions/
|
||||
}(5)
|
||||
}
|
||||
|
||||
func bare2(a, b int) (int, error, int) {
|
||||
defer func() (a int) {
|
||||
return // MATCH /avoid using bare returns, please add return expressions/
|
||||
}(5)
|
||||
}
|
||||
|
||||
func bare3(a string, b int) (a int, b float32, c string, d string) {
|
||||
go func() (a int, b int) {
|
||||
return a, b
|
||||
}(5, 6)
|
||||
|
||||
defer func() (a int) {
|
||||
return a
|
||||
}(5)
|
||||
|
||||
return // MATCH /avoid using bare returns, please add return expressions/
|
||||
}
|
||||
|
||||
func bare4(a string, b int) string {
|
||||
return a
|
||||
}
|
||||
|
||||
func bare5(a string, b int) {
|
||||
return
|
||||
}
|
43
fixtures/unused-receiver.go
Normal file
43
fixtures/unused-receiver.go
Normal file
@ -0,0 +1,43 @@
|
||||
package fixtures
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
|
||||
func (f *Unix) Name() string { // MATCH /method receiver 'f' is not referenced in method's body, consider removing or renaming it as _/
|
||||
return "unix"
|
||||
}
|
||||
|
||||
func (f *Unix) Format(failures <-chan lint.Failure, _ lint.RulesConfig) (string, error) { // MATCH /method receiver 'f' is not referenced in method's body, consider removing or renaming it as _/
|
||||
for failure := range failures {
|
||||
fmt.Printf("%v: [%s] %s\n", failure.Position.Start, failure.RuleName, failure.Failure)
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (u *Unix) Foo() (string, error) { // MATCH /method receiver 'u' is not referenced in method's body, consider removing or renaming it as _/
|
||||
for failure := range failures {
|
||||
u := 1 // shadowing the receiver
|
||||
fmt.Printf("%v\n", u)
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (u *Unix) Foos() (string, error) {
|
||||
for failure := range failures {
|
||||
u := 1 // shadowing the receiver
|
||||
fmt.Printf("%v\n", u)
|
||||
}
|
||||
|
||||
return u, nil // use of the receiver
|
||||
}
|
||||
|
||||
func (u *Unix) Bar() (string, error) {
|
||||
for failure := range failures {
|
||||
u.path = nil // modifies the receiver
|
||||
fmt.Printf("%v\n", u)
|
||||
}
|
||||
return "", nil
|
||||
}
|
77
rule/bare-return.go
Normal file
77
rule/bare-return.go
Normal file
@ -0,0 +1,77 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
|
||||
// BareReturnRule lints given else constructs.
|
||||
type BareReturnRule struct{}
|
||||
|
||||
// Apply applies the rule to given file.
|
||||
func (r *BareReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
||||
var failures []lint.Failure
|
||||
|
||||
onFailure := func(failure lint.Failure) {
|
||||
failures = append(failures, failure)
|
||||
}
|
||||
|
||||
w := lintBareReturnRule{onFailure: onFailure}
|
||||
ast.Walk(w, file.AST)
|
||||
return failures
|
||||
}
|
||||
|
||||
// Name returns the rule name.
|
||||
func (r *BareReturnRule) Name() string {
|
||||
return "bare-return"
|
||||
}
|
||||
|
||||
type lintBareReturnRule struct {
|
||||
onFailure func(lint.Failure)
|
||||
}
|
||||
|
||||
func (w lintBareReturnRule) Visit(node ast.Node) ast.Visitor {
|
||||
switch n := node.(type) {
|
||||
case *ast.FuncDecl:
|
||||
w.checkFunc(n.Type.Results, n.Body)
|
||||
case *ast.FuncLit: // to cope with deferred functions and go-routines
|
||||
w.checkFunc(n.Type.Results, n.Body)
|
||||
}
|
||||
|
||||
return w
|
||||
}
|
||||
|
||||
// checkFunc will verify if the given function has named result and bare returns
|
||||
func (w lintBareReturnRule) checkFunc(results *ast.FieldList, body *ast.BlockStmt) {
|
||||
hasNamedResults := results != nil && len(results.List) > 0 && results.List[0].Names != nil
|
||||
if !hasNamedResults || body == nil {
|
||||
return // nothing to do
|
||||
}
|
||||
|
||||
brf := bareReturnFinder{w.onFailure}
|
||||
ast.Walk(brf, body)
|
||||
}
|
||||
|
||||
type bareReturnFinder struct {
|
||||
onFailure func(lint.Failure)
|
||||
}
|
||||
|
||||
func (w bareReturnFinder) Visit(node ast.Node) ast.Visitor {
|
||||
rs, ok := node.(*ast.ReturnStmt)
|
||||
if !ok {
|
||||
return w
|
||||
}
|
||||
|
||||
if len(rs.Results) > 0 {
|
||||
return w
|
||||
}
|
||||
|
||||
w.onFailure(lint.Failure{
|
||||
Confidence: 1,
|
||||
Node: rs,
|
||||
Failure: "avoid using bare returns, please add return expressions",
|
||||
})
|
||||
|
||||
return w
|
||||
}
|
77
rule/unused-receiver.go
Normal file
77
rule/unused-receiver.go
Normal file
@ -0,0 +1,77 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
|
||||
// UnusedReceiverRule lints unused params in functions.
|
||||
type UnusedReceiverRule struct{}
|
||||
|
||||
// Apply applies the rule to given file.
|
||||
func (_ *UnusedReceiverRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
||||
var failures []lint.Failure
|
||||
|
||||
onFailure := func(failure lint.Failure) {
|
||||
failures = append(failures, failure)
|
||||
}
|
||||
|
||||
w := lintUnusedReceiverRule{onFailure: onFailure}
|
||||
|
||||
ast.Walk(w, file.AST)
|
||||
|
||||
return failures
|
||||
}
|
||||
|
||||
// Name returns the rule name.
|
||||
func (_ *UnusedReceiverRule) Name() string {
|
||||
return "unused-receiver"
|
||||
}
|
||||
|
||||
type lintUnusedReceiverRule struct {
|
||||
onFailure func(lint.Failure)
|
||||
}
|
||||
|
||||
func (w lintUnusedReceiverRule) Visit(node ast.Node) ast.Visitor {
|
||||
switch n := node.(type) {
|
||||
case *ast.FuncDecl:
|
||||
if n.Recv == nil {
|
||||
return nil // skip this func decl, not a method
|
||||
}
|
||||
|
||||
rec := n.Recv.List[0] // safe to access only the first (unique) element of the list
|
||||
if len(rec.Names) < 1 {
|
||||
return nil // the receiver is anonymous: func (aType) Foo(...) ...
|
||||
}
|
||||
|
||||
recID := rec.Names[0]
|
||||
if recID.Name == "_" {
|
||||
return nil // the receiver is already named _
|
||||
}
|
||||
|
||||
// inspect the func body looking for references to the receiver id
|
||||
fselect := func(n ast.Node) bool {
|
||||
ident, isAnID := n.(*ast.Ident)
|
||||
|
||||
return isAnID && ident.Obj == recID.Obj
|
||||
}
|
||||
refs2recID := pick(n.Body, fselect, nil)
|
||||
|
||||
if len(refs2recID) > 0 {
|
||||
return nil // the receiver is referenced in the func body
|
||||
}
|
||||
|
||||
w.onFailure(lint.Failure{
|
||||
Confidence: 1,
|
||||
Node: recID,
|
||||
Category: "bad practice",
|
||||
Failure: fmt.Sprintf("method receiver '%s' is not referenced in method's body, consider removing or renaming it as _", recID.Name),
|
||||
})
|
||||
|
||||
return nil // full method body already inspected
|
||||
}
|
||||
|
||||
return w
|
||||
}
|
11
test/bare-return_test.go
Normal file
11
test/bare-return_test.go
Normal file
@ -0,0 +1,11 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mgechev/revive/rule"
|
||||
)
|
||||
|
||||
func TestBareReturn(t *testing.T) {
|
||||
testRule(t, "bare-return", &rule.BareReturnRule{})
|
||||
}
|
11
test/unused-receiver_test.go
Normal file
11
test/unused-receiver_test.go
Normal file
@ -0,0 +1,11 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mgechev/revive/rule"
|
||||
)
|
||||
|
||||
func TestUnusedReceiver(t *testing.T) {
|
||||
testRule(t, "unused-receiver", &rule.UnusedReceiverRule{})
|
||||
}
|
Loading…
Reference in New Issue
Block a user