mirror of
https://github.com/mgechev/revive.git
synced 2025-01-26 03:52:12 +02:00
fix merge conflicts
This commit is contained in:
commit
e0a294505a
47
README.md
47
README.md
@ -30,29 +30,29 @@ Here's how `revive` is different from `golint`:
|
|||||||
<!-- TOC -->
|
<!-- TOC -->
|
||||||
|
|
||||||
- [revive](#revive)
|
- [revive](#revive)
|
||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
- [Text Editors](#text-editors)
|
- [Text Editors](#text-editors)
|
||||||
- [Installation](#installation)
|
- [Installation](#installation)
|
||||||
- [Command Line Flags](#command-line-flags)
|
- [Command Line Flags](#command-line-flags)
|
||||||
- [Sample Invocations](#sample-invocations)
|
- [Sample Invocations](#sample-invocations)
|
||||||
- [Comment Annotations](#comment-annotations)
|
- [Comment Annotations](#comment-annotations)
|
||||||
- [Configuration](#configuration)
|
- [Configuration](#configuration)
|
||||||
- [Default Configuration](#default-configuration)
|
- [Default Configuration](#default-configuration)
|
||||||
- [Recommended Configuration](#recommended-configuration)
|
- [Recommended Configuration](#recommended-configuration)
|
||||||
- [Available Rules](#available-rules)
|
- [Available Rules](#available-rules)
|
||||||
- [Available Formatters](#available-formatters)
|
- [Available Formatters](#available-formatters)
|
||||||
- [Friendly](#friendly)
|
- [Friendly](#friendly)
|
||||||
- [Stylish](#stylish)
|
- [Stylish](#stylish)
|
||||||
- [Default](#default)
|
- [Default](#default)
|
||||||
- [Extensibility](#extensibility)
|
- [Extensibility](#extensibility)
|
||||||
- [Custom Rule](#custom-rule)
|
- [Custom Rule](#custom-rule)
|
||||||
- [Example](#example)
|
- [Example](#example)
|
||||||
- [Custom Formatter](#custom-formatter)
|
- [Custom Formatter](#custom-formatter)
|
||||||
- [Speed Comparison](#speed-comparison)
|
- [Speed Comparison](#speed-comparison)
|
||||||
- [golint](#golint)
|
- [golint](#golint)
|
||||||
- [revive](#revive-1)
|
- [revive](#revive-1)
|
||||||
- [Contributors](#contributors)
|
- [Contributors](#contributors)
|
||||||
- [License](#license)
|
- [License](#license)
|
||||||
|
|
||||||
<!-- /TOC -->
|
<!-- /TOC -->
|
||||||
|
|
||||||
@ -211,6 +211,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
|
|||||||
| `empty-block` | n/a | Warns on empty code blocks | no | no |
|
| `empty-block` | n/a | Warns on empty code blocks | no | no |
|
||||||
| `superfluous-else` | n/a | Prevents redundant else statements (extends `indent-error-flow`) | no | no |
|
| `superfluous-else` | n/a | Prevents redundant else statements (extends `indent-error-flow`) | no | no |
|
||||||
| `confusing-naming` | n/a | Warns on methods with names that differ only by capitalization | no | no |
|
| `confusing-naming` | n/a | Warns on methods with names that differ only by capitalization | no | no |
|
||||||
|
| `get-return ` | n/a | Warns on getters that do not yield any result | no | no |
|
||||||
|
|
||||||
## Available Formatters
|
## Available Formatters
|
||||||
|
|
||||||
|
@ -49,6 +49,7 @@ var allRules = append([]lint.Rule{
|
|||||||
&rule.FileHeaderRule{},
|
&rule.FileHeaderRule{},
|
||||||
&rule.EmptyBlockRule{},
|
&rule.EmptyBlockRule{},
|
||||||
&rule.SuperfluousElseRule{},
|
&rule.SuperfluousElseRule{},
|
||||||
|
&rule.GetReturnRule{},
|
||||||
}, defaultRules...)
|
}, defaultRules...)
|
||||||
|
|
||||||
var allFormatters = []lint.Formatter{
|
var allFormatters = []lint.Formatter{
|
||||||
|
29
fixtures/get-return.go
Normal file
29
fixtures/get-return.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package fixtures
|
||||||
|
|
||||||
|
func getfoo() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBar(a, b int) { // MATCH /function 'getBar' seems to be a getter but it does not return any result/
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func Getbaz(a string, b int) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTaz(a string, b int) string {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *t) GetTaz(a string, b int) string {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *t) GetSaz(a string, b int) { // MATCH /function 'GetSaz' seems to be a getter but it does not return any result/
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetQux(a string, b int, c int, d string, e int64) { // MATCH /function 'GetQux' seems to be a getter but it does not return any result/
|
||||||
|
|
||||||
|
}
|
71
rule/get-return.go
Normal file
71
rule/get-return.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package rule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go/ast"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mgechev/revive/lint"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetReturnRule lints given else constructs.
|
||||||
|
type GetReturnRule struct{}
|
||||||
|
|
||||||
|
// Apply applies the rule to given file.
|
||||||
|
func (r *GetReturnRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
||||||
|
var failures []lint.Failure
|
||||||
|
|
||||||
|
onFailure := func(failure lint.Failure) {
|
||||||
|
failures = append(failures, failure)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := lintReturnRule{onFailure}
|
||||||
|
ast.Walk(w, file.AST)
|
||||||
|
return failures
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name returns the rule name.
|
||||||
|
func (r *GetReturnRule) Name() string {
|
||||||
|
return "get-return"
|
||||||
|
}
|
||||||
|
|
||||||
|
type lintReturnRule struct {
|
||||||
|
onFailure func(lint.Failure)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isGetter(name string) bool {
|
||||||
|
if strings.HasPrefix(strings.ToUpper(name), "GET") {
|
||||||
|
if len(name) > 3 {
|
||||||
|
c := name[3]
|
||||||
|
return !(c >= 'a' && c <= 'z')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasResults(rs *ast.FieldList) bool {
|
||||||
|
return rs != nil && len(rs.List) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w lintReturnRule) Visit(node ast.Node) ast.Visitor {
|
||||||
|
fd, ok := node.(*ast.FuncDecl)
|
||||||
|
if !ok {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isGetter(fd.Name.Name) {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
if !hasResults(fd.Type.Results) {
|
||||||
|
w.onFailure(lint.Failure{
|
||||||
|
Confidence: 0.8,
|
||||||
|
Node: fd,
|
||||||
|
Category: "logic",
|
||||||
|
URL: "#get-return",
|
||||||
|
Failure: fmt.Sprintf("function '%s' seems to be a getter but it does not return any result", fd.Name.Name),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return w
|
||||||
|
}
|
11
test/get-return_test.go
Normal file
11
test/get-return_test.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mgechev/revive/rule"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetReturn(t *testing.T) {
|
||||||
|
testRule(t, "get-return", &rule.GetReturnRule{})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user