Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

staticcheck: detect shadowing of named returns #1524

Open
florianl opened this issue Apr 22, 2024 · 2 comments
Open

staticcheck: detect shadowing of named returns #1524

florianl opened this issue Apr 22, 2024 · 2 comments

Comments

@florianl
Copy link
Sponsor

staticcheck should detect the shadowing of named returns.

In the following example, the function foo() uses the named return err error. In this case, the named return is shadowed by err := bar().

func bar() error {
 return nil
}

func foo() (err error) {
  err := bar()
  return
}
@florianl florianl added the needs-triage Newly filed issue that needs triage label Apr 22, 2024
@dominikh dominikh added new-check and removed needs-triage Newly filed issue that needs triage labels Apr 22, 2024
@dominikh
Copy link
Owner

Note that the provided example doesn't compile, because that's not shadowing but trying to redeclare. And if it had worked, it would've been an unused variable and also hadn't compiled.

Do you have a closer-to-reality example you'd like to see flagged by Staticcheck?

@florianl
Copy link
Sponsor Author

This is the example from the Go specification (last example in the Return statements section):

func f(n int) (res int, err error) {
	if _, err := f(n-1); err != nil {
		return  // invalid return statement: err is shadowed
	}
	return
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants