Nomutant directive¶
The //nomutant: comment directive lets you suppress mutations on individual
statements, blocks, or whole files directly from the source code. It is a
finer-grained alternative to the exclude-files configuration option, and is
useful when only a few mutations in a file are noisy or known to be unkillable
(for example, defensive checks for unreachable code).
A suppressed mutation is reported with the SKIPPED status, the same status
used by diff-mode for unchanged code, so you can still audit which directives
took effect by reading the report.
Syntax¶
The directive always has a colon and no space after the //:
//nomutant:allsuppresses every mutator.//nomutant:<type>[,<type>...]suppresses only the listed mutator types.
a := b + c //nomutant:all
a := b + c //nomutant:arithmetic-base,invert-bitwise
The type names are the same as the configuration keys for each mutator type —
see Configuration for the full list (e.g.
arithmetic-base, conditionals-boundary, invert-bwassign).
Either form may be followed by a reason, separated by whitespace:
return a + b //nomutant:arithmetic-base a is always 0 here, see #42
The colon form is what keeps the directive intact under gofmt. Since Go
1.19, gofmt rewrites a doc comment such as //nomutant to // nomutant,
but leaves //word:word directives alone. A bare //nomutant or a spaced
// nomutant suppresses nothing; Gremlins logs a warning with its position
and asks you to write //nomutant:all instead.
Scopes¶
End-of-line¶
A directive after code on the same line applies to that line and to the statement or declaration that ends on it, including the earlier lines of a statement that spans several lines.
total := sum(
a+b, // suppressed
c*d, // suppressed
) //nomutant:all
next := e - f // not suppressed
A directive after a closing brace applies to the statement that brace ends, not to the statement after it:
if a > b { // suppressed
a = a - 1 // suppressed
} //nomutant:all
return a + b // not suppressed
Own line¶
A directive on its own line, immediately above a function declaration or a statement, suppresses every mutation inside that declaration or statement.
//nomutant:all
func myFunc() {
a := b + c
return a * d
}
func myFunc() {
//nomutant:arithmetic-base
a := b + c // suppressed
d := e * f // not suppressed
}
The directive must sit directly above the code it applies to. One followed
by a blank line, by another comment, or by a closing brace applies to
nothing; Gremlins logs a warning with its position. A directive may follow
a doc comment, which is where gofmt moves it:
// myFunc does something.
//
//nomutant:all
func myFunc() {}
File¶
A directive placed before the package clause suppresses mutations in the
whole file. It is equivalent to adding the file to the
unleash.exclude-files configuration list, but lives next to the code.
//nomutant:all
package apackage
Several file-level directives add up:
//nomutant:arithmetic-base
//nomutant:conditionals-boundary
package apackage
Nesting¶
Directives compose additively: a mutation is suppressed if any enclosing scope (file, own-line, or end-of-line) suppresses its type. An inner directive adds to outer ones rather than replacing them.
//nomutant:invert-bitwise
func F() {
//nomutant:arithmetic-base
a := 1 + 2 // BOTH arithmetic-base AND invert-bitwise are suppressed
b := 3 * 4 // only invert-bitwise is suppressed (outer scope still applies)
}
This means an outer //nomutant:all cannot be narrowed by an inner typed
directive — the outer one already covers everything. To opt back in to a
specific mutator inside a broadly-suppressed region, restructure the code
rather than relying on directive composition.
Malformed directives¶
A directive whose type list is empty (//nomutant:) or names only unknown
mutator types (//nomutant:bogus-type) is treated as a no-op and a warning
is logged. This makes it safe to add or rename directives without breaking
your build.
Interaction with other settings¶
- A directive-suppressed mutant is reported with status
SKIPPED. The mutant is still emitted to the report so you can confirm the directive took effect; it is not silently dropped. - File-level
exclude-filesrules and//nomutant:directives are independent. Either one is sufficient to suppress a mutation. - Disabling a mutator type via configuration takes effect before the directive is evaluated; if a mutator type is disabled globally, the directive has nothing to suppress.