1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00

New rule: duplicated-imports (#111)

* Adds rule superfluous-else (an extension of indent-error-flow)

* Fix superfluous-else rule struct namming.

* Adds superfuous-else rule to the rules table

* Adds confusing-naming rule

* adds multifile test

* clean-up

* fix config.go

* clean master

* new ADS rule: newerr

* ADS-print working version

* ads-print final version

* ads-lost-err working version

* adds duplicated-imports rule

* adds duplicated-imports rule
This commit is contained in:
SalvadorC 2019-03-20 19:54:03 +01:00 committed by Minko Gechev
parent 6a62ee9f02
commit fbefad8558
6 changed files with 67 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

@ -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

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