Skip to content

Add linting rule for preferring defer over t.Cleanup in tests (except helpers) #3181

Closed
@mafredri

Description

@mafredri

The premise is that we want tests to use defer so that they read like idiomatic Go code and that there is no mixed usage of defer and t.Cleanup (since the order of execution is hard to reason about, and rarely the intent). The exception is test helper functions.

This was requested in #3113 but not required for merging the PR.

A few examples for ruleguard rules was floated in #3113 but both have unnecessary repercussions:

  1. Simple rule is too aggressive
  2. More specific rule does not catch non-toplevel uses (e.g. if the t.Cleanup is inside if {} it won't be caught

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleThis issue is like stale bread.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions