Skip to content

mixed-regexp-capture-types

Rubysuspiciousno fixdefault: warnruby/rubocop

Reports a regexp literal that has both named groups ((?<name>...)) and plain groups ((...)).

When a regexp has named groups, Ruby does not capture the plain groups, so $2 and match[2] are nil, which is not what the numbered group in the source suggests.

To fix, name every group you need, or use (?:...) for the ones you do not.

Reported
...
y = :ok
...
/(?<a>x)(y)/
/(?<a>x)(?:y)/
...
ruby:
lint:
rules:
mixed-regexp-capture-types: warn # off, warn (default), error

Enabled by the presets: ruby/rubocop.

Suppress one line with sudsy-disable-next-line mixed-regexp-capture-types in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
rubocop Lint/MixedRegexpCaptureTypes full