c3

package module
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2025 License: MIT Imports: 9 Imported by: 0

README

c3

Cleanup Context Checker

c3 is a tool to analyze and warn against calling (*testing.common).Context within (*testing.common).Cleanup functions in Go tests.

(*testing.common).Context is canceled before (*testing.common).Cleanup functions are called, so calling (*testing.common).Context within (*testing.common).Cleanup functions can cause unexpected behavior.

Context returns a context that is canceled just before Cleanup-registered functions are called.^1

package a

import (
	"context"
	"testing"
)

func cleanup(t *testing.T) {
	t.Context()
}

func f(ctx context.Context) { }

func TestA(t *testing.T) {
	t.Cleanup(func() { t.Context() })          // want `avoid calling \(\*testing\.common\)\.Context inside Cleanup`
	t.Cleanup(func() { cleanup(t) })           // want `avoid calling \(\*testing\.common\)\.Context inside Cleanup`
	t.Cleanup(func() { f(t.Context()) })       // want `avoid calling \(\*testing\.common\)\.Context inside Cleanup`
	t.Cleanup(func() { context.Background() }) // ok
}

Usage

go vet
go install github.com/ikura-hamu/c3@latest
go vet -vettool=$(which c3) ./...
golangci-lint Module Plugin

https://golangci-lint.run/plugins/module-plugins/

  1. Configure golangci-lint customization.

.custom-gcl.yml

version: "v1.64.1" # golangci-lint version
destination: "."
name: "custom-gcl"
plugins:
  - module: "github.com/ikura-hamu/c3"
    path: "github.com/ikura-hamu/c3"
    version: "latest"
  1. Build custom golangci-lint bianry.
golangci-lint custom
  1. Set up golangci-lint configuration.

.golangci.yml

linters:
  enable:
    - c3

linters-settings:
  custom:
    c3:
      type: "module"
  1. Run custom golangci-lint.
./custom-gcl run

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "testing_cleanup_ctx",
	Doc:  doc,
	Run:  run,
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
		buildssa.Analyzer,
	},
}

Analyzer is an analyzer to detect calling (*testing.common).Context inside (*testing.common).Cleanup

Functions

func New

func New(settings any) (register.LinterPlugin, error)

Types

This section is empty.

Directories

Path Synopsis
cmd
c3 command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL