mirror of
https://github.com/mgechev/revive.git
synced 2025-09-16 09:06:22 +02:00
feat: add rule for redundant import alias (#854)
This commit is contained in:
@@ -501,6 +501,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
|
|||||||
| [`use-any`](./RULES_DESCRIPTIONS.md#use-any) | n/a | Proposes to replace `interface{}` with its alias `any` | no | no |
|
| [`use-any`](./RULES_DESCRIPTIONS.md#use-any) | n/a | Proposes to replace `interface{}` with its alias `any` | no | no |
|
||||||
| [`datarace`](./RULES_DESCRIPTIONS.md#datarace) | n/a | Spots potential dataraces | no | no |
|
| [`datarace`](./RULES_DESCRIPTIONS.md#datarace) | n/a | Spots potential dataraces | no | no |
|
||||||
| [`comment-spacings`](./RULES_DESCRIPTIONS.md#comment-spacings) | []string | Warns on malformed comments | no | no |
|
| [`comment-spacings`](./RULES_DESCRIPTIONS.md#comment-spacings) | []string | Warns on malformed comments | no | no |
|
||||||
|
| [`redundant-import-alias`](./RULES_DESCRIPTIONS.md#redundant-import-alias) | n/a | Warns on import aliases matching the imported package name | no | no |
|
||||||
|
|
||||||
|
|
||||||
## Configurable rules
|
## Configurable rules
|
||||||
|
@@ -75,6 +75,7 @@ List of all available rules.
|
|||||||
- [use-any](#use-any)
|
- [use-any](#use-any)
|
||||||
- [useless-break](#useless-break)
|
- [useless-break](#useless-break)
|
||||||
- [waitgroup-by-value](#waitgroup-by-value)
|
- [waitgroup-by-value](#waitgroup-by-value)
|
||||||
|
- [redundant-import-alias](#redundant-import-alias)
|
||||||
|
|
||||||
## add-constant
|
## add-constant
|
||||||
|
|
||||||
@@ -760,3 +761,9 @@ _Description_: Function parameters that are passed by value, are in fact a copy
|
|||||||
This rule warns when a `sync.WaitGroup` expected as a by-value parameter in a function or method.
|
This rule warns when a `sync.WaitGroup` expected as a by-value parameter in a function or method.
|
||||||
|
|
||||||
_Configuration_: N/A
|
_Configuration_: N/A
|
||||||
|
|
||||||
|
## redundant-import-alias
|
||||||
|
|
||||||
|
_Description_: This rule warns on redundant import aliases. This happens when the alias used on the import statement matches the imported package name.
|
||||||
|
|
||||||
|
_Configuration_: N/A
|
@@ -87,6 +87,7 @@ var allRules = append([]lint.Rule{
|
|||||||
&rule.DataRaceRule{},
|
&rule.DataRaceRule{},
|
||||||
&rule.CommentSpacingsRule{},
|
&rule.CommentSpacingsRule{},
|
||||||
&rule.IfReturnRule{},
|
&rule.IfReturnRule{},
|
||||||
|
&rule.RedundantImportAlias{},
|
||||||
}, defaultRules...)
|
}, defaultRules...)
|
||||||
|
|
||||||
var allFormatters = []lint.Formatter{
|
var allFormatters = []lint.Formatter{
|
||||||
|
52
rule/redundant-import-alias.go
Normal file
52
rule/redundant-import-alias.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package rule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go/ast"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mgechev/revive/lint"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RedundantImportAlias lints given else constructs.
|
||||||
|
type RedundantImportAlias struct{}
|
||||||
|
|
||||||
|
// Apply applies the rule to given file.
|
||||||
|
func (*RedundantImportAlias) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
||||||
|
var failures []lint.Failure
|
||||||
|
|
||||||
|
for _, imp := range file.AST.Imports {
|
||||||
|
if imp.Name == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if getImportPackageName(imp) == imp.Name.Name {
|
||||||
|
failures = append(failures, lint.Failure{
|
||||||
|
Confidence: 1,
|
||||||
|
Failure: fmt.Sprintf("Import alias \"%s\" is redundant", imp.Name.Name),
|
||||||
|
Node: imp,
|
||||||
|
Category: "imports",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return failures
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name returns the rule name.
|
||||||
|
func (*RedundantImportAlias) Name() string {
|
||||||
|
return "redundant-import-alias"
|
||||||
|
}
|
||||||
|
|
||||||
|
func getImportPackageName(imp *ast.ImportSpec) string {
|
||||||
|
const pathSep = "/"
|
||||||
|
const strDelim = `"`
|
||||||
|
|
||||||
|
path := imp.Path.Value
|
||||||
|
i := strings.LastIndex(path, pathSep)
|
||||||
|
if i == -1 {
|
||||||
|
return strings.Trim(path, strDelim)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Trim(path[i+1:], strDelim)
|
||||||
|
}
|
11
test/redundant-import-alias_test.go
Normal file
11
test/redundant-import-alias_test.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mgechev/revive/rule"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestRedundantImportAlias rule.
|
||||||
|
func TestRedundantImportAlias(t *testing.T) {
|
||||||
|
testRule(t, "redundant-import-alias", &rule.RedundantImportAlias{})
|
||||||
|
}
|
12
testdata/redundant-import-alias.go
vendored
Normal file
12
testdata/redundant-import-alias.go
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package fixtures
|
||||||
|
|
||||||
|
import(
|
||||||
|
"crypto/md5"
|
||||||
|
"strings"
|
||||||
|
_ "crypto/md5"
|
||||||
|
str "strings"
|
||||||
|
strings "strings" // MATCH /Import alias "strings" is redundant/
|
||||||
|
crypto "crypto/md5"
|
||||||
|
md5 "crypto/md5" // MATCH /Import alias "md5" is redundant/
|
||||||
|
)
|
||||||
|
|
Reference in New Issue
Block a user