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

SA9009: flag defer foo() where foo returns a closure #1543

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arp242
Copy link
Contributor

@arp242 arp242 commented May 25, 2024

Add a new checker to flag cases where a deferred function returns exactly one function, and that function isn't called. For example:

func f() func() {
	// Do stuff.
	return func() {
		// Do stuff
	}
}

func main() {
	defer f() // Error: should be f()()
}

As mentioned in #466, there's a change for false positives. I ran the check on Go stdlib, all my own code, and about 200 popular Go packages, and haven't found any false positives (and one match: grpc/grpc-go#7270)

Fixes #466

@arp242
Copy link
Contributor Author

arp242 commented May 25, 2024

I ran the same script for #1545 with this check; found three more wrong uses of defer, but no false positives.

Add a new checker to flag cases where a deferred function returns
exactly one function, and that function isn't called. For example:

	func f() func() {
		// Do stuff.
		return func() {
			// Do stuff
		}
	}

	func main() {
		defer f() // Error: should be f()()
	}

As mentioned in dominikh#466, there's a change for false positives. I ran the
check on Go stdlib, all my own code, and about 200 popular Go packages,
and haven't found any false positives (and one match:
grpc/grpc-go#7270)

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

Successfully merging this pull request may close these issues.

staticcheck: flag defer foo() where foo returns a closure
1 participant