Skip to content

Add #[mutants::exclude_re("pattern")] attribute#607

Open
sandersaares wants to merge 5 commits intosourcefrog:mainfrom
sandersaares:exclude-re-attr
Open

Add #[mutants::exclude_re("pattern")] attribute#607
sandersaares wants to merge 5 commits intosourcefrog:mainfrom
sandersaares:exclude-re-attr

Conversation

@sandersaares
Copy link
Copy Markdown

Add a new attribute that excludes specific mutations by regex, without disabling all mutations on the function like #[mutants::skip] does.

The attribute can be placed on functions, impl blocks, trait blocks, modules, and files (as an inner attribute). Patterns from outer scopes are inherited. Also supported within cfg_attr.

Changes

  • mutants_attrs/src/lib.rs: New exclude_re proc-macro attribute (no-op, like skip)
  • src/visit.rs: Core implementation - exclude_re_stack on the discovery visitor, push/pop at scope boundaries, attrs_exclude_re_patterns parsing, filtering in collect_mutant
  • testdata/exclude_re_attr/: Test tree exercising all supported scopes (function, impl, trait, mod, scope inheritance, cfg_attr)
  • tests/main.rs: Integration test
  • book/src/attrs.md: Documentation
  • NEWS.md: Changelog entry

Design decisions

  • Invalid regex in an attribute returns a hard error, matching --exclude-re CLI behavior
  • Multiple #[mutants::exclude_re] attributes on the same item are OR'd
  • Patterns on outer scopes (impl/trait/mod/file) are inherited by all items within

Closes #551

Add a new attribute that excludes specific mutations by regex, without
disabling all mutations on the function like #[mutants::skip] does.

The attribute can be placed on functions, impl blocks, trait blocks,
modules, and files (as an inner attribute). Patterns from outer scopes
are inherited. Also supported within cfg_attr.

Closes sourcefrog#551
add_numbers used 'replace .* with ()' where () was a regex capture
group matching empty string, causing it to exclude ALL mutations
instead of just 'with ()'. Use r"with \(\)" instead.

subtract used 'replace .* with' which excluded everything. Use
'with 0' to demonstrate cfg_attr while keeping other mutations.
@sandersaares sandersaares marked this pull request as ready for review April 14, 2026 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: define regex excludes via attributes in code

1 participant