mirror of
				https://github.com/mgechev/revive.git
				synced 2025-10-30 23:37:49 +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:
		| @@ -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 | ||||
|  | ||||
|   | ||||
| @@ -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. | ||||
|   | ||||
| @@ -73,6 +73,7 @@ var allRules = append([]lint.Rule{ | ||||
| 	&rule.EmptyLinesRule{}, | ||||
| 	&rule.LineLengthLimitRule{}, | ||||
| 	&rule.CallToGCRule{}, | ||||
| 	&rule.DuplicatedImportsRule{}, | ||||
| }, defaultRules...) | ||||
|  | ||||
| var allFormatters = []lint.Formatter{ | ||||
|   | ||||
							
								
								
									
										8
									
								
								fixtures/duplicated-imports.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								fixtures/duplicated-imports.go
									
									
									
									
									
										Normal 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/ | ||||
| ) | ||||
							
								
								
									
										39
									
								
								rule/duplicated-imports.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								rule/duplicated-imports.go
									
									
									
									
									
										Normal 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" | ||||
| } | ||||
							
								
								
									
										11
									
								
								test/duplicated-import_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								test/duplicated-import_test.go
									
									
									
									
									
										Normal 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{}) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user