1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00
This commit is contained in:
chavacava 2019-04-15 10:26:18 +02:00
commit 3f40759f65
8 changed files with 80 additions and 0 deletions

View File

@ -292,6 +292,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
| [`empty-lines`](./RULES_DESCRIPTIONS.md#empty-lines) | n/a | Warns when there are heading or trailing newlines in a block | no | no |
| [`line-length-limit`](./RULES_DESCRIPTIONS.md#line-length-limit) | int | Specifies the maximum number of characters in a line | no | no |
| [`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 |
## Configurable rules

View File

@ -18,6 +18,7 @@ List of all available rules.
- [cyclomatic](#cyclomatic)
- [deep-exit](#deep-exit)
- [dot-imports](#dot-imports)
- [duplicated-imports](#duplicated-imports)
- [empty-block](#empty-block)
- [empty-lines](#empty-lines)
- [error-naming](#error-naming)
@ -168,6 +169,12 @@ More information [here](https://github.com/golang/go/wiki/CodeReviewComments#imp
_Configuration_: N/A
## duplicated-imports
_Description_: It is possible to unintentionally import the same package twice. This rule looks for packages that are imported two or more times.
_Configuration_: N/A
## empty-block
_Description_: Empty blocks make code less readable and could be a symptom of a bug or unfinished refactoring.

View File

@ -73,6 +73,7 @@ var allRules = append([]lint.Rule{
&rule.EmptyLinesRule{},
&rule.LineLengthLimitRule{},
&rule.CallToGCRule{},
&rule.DuplicatedImportsRule{},
}, defaultRules...)
var allFormatters = []lint.Formatter{

View File

@ -0,0 +1,8 @@
package fixtures
import(
"crypto/md5"
"strings"
_ "crypto/md5" // MATCH /Package "crypto/md5" already imported/
str "strings" // MATCH /Package "strings" already imported/
)

View File

@ -41,3 +41,13 @@ func l() (int, error, int) { // MATCH /error should be the last type when return
func m() (x int, err error, y int) { // MATCH /error should be the last type when returning multiple items/
return 0, nil, 0
}
// Check for multiple error returns but with errors at the end.
func n() (int, error, error) { // ok
return 0, nil, nil
}
// Check for multiple error returns mixed in order, but keeping one error at last position.
func o() (int, error, int, error) { // ok
return 0, nil, 0, nil
}

View File

@ -0,0 +1,39 @@
package rule
import (
"fmt"
"github.com/mgechev/revive/lint"
)
// DuplicatedImportsRule lints given else constructs.
type DuplicatedImportsRule struct{}
// Apply applies the rule to given file.
func (r *DuplicatedImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
impPaths := map[string]struct{}{}
for _, imp := range file.AST.Imports {
path := imp.Path.Value
_, ok := impPaths[path]
if ok {
failures = append(failures, lint.Failure{
Confidence: 1,
Failure: fmt.Sprintf("Package %s already imported", path),
Node: imp,
Category: "imports",
})
continue
}
impPaths[path] = struct{}{}
}
return failures
}
// Name returns the rule name.
func (r *DuplicatedImportsRule) Name() string {
return "duplicated-imports"
}

View File

@ -47,6 +47,9 @@ func (w lintErrorReturn) Visit(n ast.Node) ast.Visitor {
if len(ret) <= 1 {
return w
}
if isIdent(ret[len(ret)-1].Type, "error") {
return nil
}
// An error return parameter should be the last parameter.
// Flag any error parameters found before the last.
for _, r := range ret[:len(ret)-1] {

View File

@ -0,0 +1,11 @@
package test
import (
"testing"
"github.com/mgechev/revive/rule"
)
func TestDuplicatedImports(t *testing.T) {
testRule(t, "duplicated-imports", &rule.DuplicatedImportsRule{})
}