Since the context is closed before t.Cleanup is called this is almost always a mistake, and can usually be detected statically. It's an easy mistake to make, and on one occasion I wasted about an hour trying to find the root cause.

The minimum would be to detect a direct call to t.Context() in t.Cleanup. This could be improved by detecting usage of context.Context variable inside t.Cleanup which was ever assigned t.Context in the past. This should probably cover almost all such uses.

Comment From: seankhliao

I don't think we'd want to block future changes to T.Context that would make accessing it in T.Cleanup useful, such as exposing additional information about the test or cancellation cause.

Comment From: YairHalberstadt

Wouldn't it be possible to revert this check if that ever happens?