diff --git a/docs/modules/ROOT/pages/cops_lint.adoc b/docs/modules/ROOT/pages/cops_lint.adoc index ed4e5cd96d1..f40c2fc2627 100644 --- a/docs/modules/ROOT/pages/cops_lint.adoc +++ b/docs/modules/ROOT/pages/cops_lint.adoc @@ -2368,8 +2368,23 @@ p [''.frozen?, ''.encoding] #=> [true, #] | - |=== +This cops looks for out of range referencing for Regexp, as while capturing groups out of +out of range reference always returns nil. + +=== Examples + +[source,ruby] +---- +/(foo)bar/ =~ 'foobar' + +# bad - always returns nil + +puts $2 # => nil + # good - puts $1 # => foo + +puts $1 # => foo +---- == Lint/ParenthesesAsGroupedExpression diff --git a/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb b/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb index d9c153f2eaa..0ca5e562abb 100644 --- a/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +++ b/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb @@ -5,14 +5,17 @@ module Cop module Lint # This cops looks for out of range referencing for Regexp, as while capturing groups out of # out of range reference always returns nil. - + # # @example + # # /(foo)bar/ =~ 'foobar' - + # # # bad - always returns nil + # # puts $2 # => nil - + # # # good + # # puts $1 # => foo # class OutOfRangeRegexpRef < Base