unparam

package
v0.74.9 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package unparam provides helpers for generating nolint comments for the unparam linter (mvdan.cc/unparam), which flags a function parameter (or result) that never varies across its call sites.

unparam's default remediation - delete the parameter and inline the constant value at its one effective use - is usually right, and is a real simplification rather than linter appeasement. But unparam cannot see that a signature is fixed by something other than its own call sites: an interface method set, a function-type variable (http.HandlerFunc, sort.Interface, a callback field), or an exported API whose signature is a compatibility contract. In those cases the parameter is genuinely unused today, but removing it isn't possible (or isn't safe) without breaking the thing the signature exists to satisfy - nolint is the correct call.

Usage

comment := unparam.Nolint(unparam.CommonReasons.InterfaceSignature)
// Returns: "//nolint:unparam // Signature fixed by an interface method
// set this type implements"

When to fix instead of nolint

Reach for the real fix first - delete the parameter and hardcode the constant inside the function body - whenever the signature isn't externally constrained. This is especially common in test helpers, where a parameter added for generality often ends up receiving the same value at every call site as the test suite grows. See CommonReasons for the constrained-signature cases nolint is actually for.

Index

Constants

This section is empty.

Variables

View Source
var CommonReasons = struct {
	// InterfaceSignature is for a method whose parameter list is fixed by
	// an interface it implements (e.g. sort.Interface, io.Writer,
	// database/sql/driver.Valuer) even though this particular
	// implementation doesn't use every parameter.
	InterfaceSignature string

	// CallbackSignature is for a function passed as a value where the
	// signature is fixed by the receiving API (http.HandlerFunc, a
	// third-party callback/hook type, a function-type struct field) rather
	// than chosen by this function itself.
	CallbackSignature string

	// ExportedAPICompat is for an exported function or method whose
	// signature is a compatibility contract: removing a currently-unused
	// parameter would be a breaking change for callers outside this
	// module, even though no internal call site varies it today.
	ExportedAPICompat string
}{
	InterfaceSignature: "Signature fixed by an interface method set this type implements",
	CallbackSignature:  "Signature fixed by the callback/function type this value is passed as",
	ExportedAPICompat:  "Parameter kept for exported API compatibility; removing it would be a breaking change for external callers",
}

CommonReasons provides pre-written reason strings for common scenarios where a parameter or result unparam flags as unused is actually required by something outside the function's own call sites.

Functions

func Nolint

func Nolint(reason string) string

Nolint formats a nolint:unparam comment with the given reason.

Example:

comment := unparam.Nolint("r required by http.Handler")
// Returns: "//nolint:unparam // r required by http.Handler"

Types

This section is empty.

Jump to

Keyboard shortcuts

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