Skip to content

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
@philjb
Copy link

philjb commented Jan 31, 2025

@arp242 - thanks for putting this pr together!

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
2 participants