Omit Redundant Control Flow
ID |
go.omit_redundant_control_flow |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Best Practice |
Language |
Go |
Tags |
best-practice, code-style |
Description
Reports a bare return that is the last statement of a function declaring no return values.
Rationale
Control falls off the end of a function body when it reaches the closing brace, so a trailing
bare return in a void function is pure noise. Removing it shortens the function and matches the
style most Go code follows; an early return used as a guard is, of course, left untouched.
// Bad
func greet(name string) {
fmt.Println(name)
return
}
// Good
func greet(name string) {
fmt.Println(name)
}
Remediation
Delete the trailing bare return. Keep early return statements that exit the function before
its end.