Go (golang) linter to check function return styles according to rules of your choice.
go install -v github.com/gavv/returnstyles/cmd/returnstyles@latest
returnstyles [options] <package>
For example:
returnstyles -allow-naked-returns=false .
option | default | description |
---|---|---|
-allow-unnamed |
true | allow functions with unnamed return variables |
-allow-named |
true | allow functions with named return variables |
-allow-partially-named |
false | allow functions with partially named return variables |
-allow-normal-returns |
true | allow normal (non-naked) returns in functions with named return variables |
-allow-naked-returns |
true | allow naked returns in functions with named return variables |
-allow-mixing-returns |
false | allow mixing normal and naked in functions with named return variables |
-include-generated |
false | include diagnostics for generated files |
Instead of specifying individual command-line options, you can provide a single configuration file in YAML format:
returnstyles -config path/to/config.yaml <package>
It should have the following format:
returnstyles:
allow-unnamed: true
allow-named: true
allow-partially-named: false
allow-normal-returns: true
allow-naked-returns: true
allow-mixing-returns: false
include-generated: false
import "github.com/gavv/returnstyles"
Global variable returnstyles.Analyzer
follows guidelines in the golang.org/x/tools/go/analysis package.
func foo() (int, int) { // lint: functions with unnamed return variables not allowed
return 11, 22
}
func foo() (a int, b int) { // lint: functions with named return variables not allowed
return 11, 22
}
func foo() (int, b int) { // lint: functions with partially named return variables not allowed
return 11, 22
}
func foo() (a int, b int) {
return 11, 22 // lint: normal (non-naked) returns not allowed
// in functions with named return variables
}
func foo() (a int, b int) {
a = 11
b = 22
return // lint: naked returns not allowed
}
func foo() (a int, b int) {
if cond() {
return 11, 22
} else {
a = 11
b = 22
return // lint: mixing normal and naked returns not allowed
}
}
func bar() (a int, b int) {
if cond() {
a = 11
b = 22
return
} else {
return 11, 22 // lint: mixing normal and naked returns not allowed
}
}
See here.