mirror of
				https://github.com/mgechev/revive.git
				synced 2025-10-30 23:37:49 +02:00 
			
		
		
		
	unexported_return: exclude main package and test and tests files (#1399)
The content of these files cannot be imported by other packages, so they are excluded from the unexported_return rule.
This commit is contained in:
		
							
								
								
									
										17
									
								
								lint/file.go
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								lint/file.go
									
									
									
									
									
								
							| @@ -24,6 +24,23 @@ type File struct { | ||||
| // IsTest returns if the file contains tests. | ||||
| func (f *File) IsTest() bool { return strings.HasSuffix(f.Name, "_test.go") } | ||||
|  | ||||
| // IsImportable returns if the symbols defined in this file can be imported in other packages. | ||||
| // | ||||
| // Symbols from the package `main` or test files are not exported, so they cannot be imported. | ||||
| func (f *File) IsImportable() bool { | ||||
| 	if f.IsTest() { | ||||
| 		// Test files cannot be imported. | ||||
| 		return false | ||||
| 	} | ||||
|  | ||||
| 	if f.Pkg.IsMain() { | ||||
| 		// The package `main` cannot be imported. | ||||
| 		return false | ||||
| 	} | ||||
|  | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| // Content returns the file's content. | ||||
| func (f *File) Content() []byte { | ||||
| 	return f.content | ||||
|   | ||||
| @@ -14,6 +14,12 @@ type UnexportedReturnRule struct{} | ||||
|  | ||||
| // Apply applies the rule to given file. | ||||
| func (*UnexportedReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { | ||||
| 	if !file.IsImportable() { | ||||
| 		// Symbols defined in such files cannot be used in other packages. | ||||
| 		// Therefore, we don't need to check for unexported return types. | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	var failures []lint.Failure | ||||
|  | ||||
| 	file.Pkg.TypeCheck() | ||||
|   | ||||
							
								
								
									
										16
									
								
								test/unexported_return_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								test/unexported_return_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| package test | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/mgechev/revive/rule" | ||||
| ) | ||||
|  | ||||
| func TestUnexportedReturn(t *testing.T) { | ||||
| 	testRule(t, "unexported_return_package_foo", &rule.UnexportedReturnRule{}) | ||||
| 	testRule(t, "unexported_return_package_foo_test", &rule.UnexportedReturnRule{}) | ||||
| 	testRule(t, "unexported_return_package_footest_test", &rule.UnexportedReturnRule{}) | ||||
| 	testRule(t, "unexported_return_package_main", &rule.UnexportedReturnRule{}) | ||||
| 	testRule(t, "unexported_return_package_main_test", &rule.UnexportedReturnRule{}) | ||||
| 	testRule(t, "unexported_return_package_maintest_test", &rule.UnexportedReturnRule{}) | ||||
| } | ||||
							
								
								
									
										12
									
								
								testdata/unexported_return_package_foo.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								testdata/unexported_return_package_foo.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| package foo | ||||
|  | ||||
| // this is a file in a package | ||||
| // | ||||
| // such files SHOULD be linted by unexported_return rule | ||||
| // because symbols defined in test files cannot be used in other packages | ||||
|  | ||||
| type foo struct{} | ||||
|  | ||||
| func NewFoo() foo { // MATCH /exported func NewFoo returns unexported type foo.foo, which can be annoying to use/ | ||||
| 	return foo{} | ||||
| } | ||||
							
								
								
									
										12
									
								
								testdata/unexported_return_package_foo_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								testdata/unexported_return_package_foo_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| package foo | ||||
|  | ||||
| // this is a test file in a package | ||||
| // | ||||
| // such files SHOULD NOT be linted by unexported_return rule | ||||
| // because symbols defined in test files cannot be used in other packages | ||||
|  | ||||
| type foo struct{} | ||||
|  | ||||
| func NewFoo() foo { | ||||
| 	return foo{} | ||||
| } | ||||
							
								
								
									
										12
									
								
								testdata/unexported_return_package_footest_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								testdata/unexported_return_package_footest_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| package foo_test | ||||
|  | ||||
| // this is a test file in a _test package | ||||
| // | ||||
| // such files SHOULD NOT be linted by unexported_return rule | ||||
| // because symbols defined in test files cannot be used in other packages | ||||
|  | ||||
| type foo struct{} | ||||
|  | ||||
| func NewFoo() foo { | ||||
| 	return foo{} | ||||
| } | ||||
							
								
								
									
										12
									
								
								testdata/unexported_return_package_main.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								testdata/unexported_return_package_main.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| package main | ||||
|  | ||||
| // this is a file in the main package | ||||
| // | ||||
| // such files SHOULD NOT be linted by unexported_return rule | ||||
| // because symbols defined in main package cannot be imported in other packages | ||||
|  | ||||
| type foo struct{} | ||||
|  | ||||
| func NewFoo() foo { | ||||
| 	return foo{} | ||||
| } | ||||
							
								
								
									
										12
									
								
								testdata/unexported_return_package_main_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								testdata/unexported_return_package_main_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| package main | ||||
|  | ||||
| // this is a test file in the main package | ||||
| // | ||||
| // such files SHOULD NOT be linted by unexported_return rule | ||||
| // because symbols defined in main package cannot be imported in other packages | ||||
|  | ||||
| type foo struct{} | ||||
|  | ||||
| func NewFoo() foo { | ||||
| 	return foo{} | ||||
| } | ||||
							
								
								
									
										12
									
								
								testdata/unexported_return_package_maintest_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								testdata/unexported_return_package_maintest_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| package main_test | ||||
|  | ||||
| // this is a test file in the main_test package | ||||
| // | ||||
| // such files SHOULD NOT be linted by unexported_return rule | ||||
| // because symbols defined in main package cannot be imported in other packages | ||||
|  | ||||
| type foo struct{} | ||||
|  | ||||
| func NewFoo() foo { | ||||
| 	return foo{} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user