Project

General

Profile

Actions

Bug #12359

closed

Named captures "conflict" warning is unnecessary and limits uses of named captures

Added by headius (Charles Nutter) almost 8 years ago. Updated almost 8 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin14]
[ruby-core:75416]

Description

There's currently a warning whenever a named capture in a regular expression has the same name as an already-declared local variable:

$ ruby23 -v -e 'x = 1; /(?<x>foo)/ =~ "foo"'
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin14]
-e:1: warning: named capture conflicts a local variable - x

It also warns if you have two expressions using the same named capture:

$ ruby23 -v -e '/(?<x>foo)/ =~ "foo"; /(?<x>foo)/ =~ "foo"'
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin14]
-e:1: warning: named capture conflicts a local variable - x

I do not believe either of these warnings are useful, since the only option for fixing them is to use a new variable name for every named capture.

I have a few more reasons, as well:

  • Using the same named capture on both the "then" and "else" branches of an "if" statement will produce a warning. Changing one of the names requires additional work to join the to branches back together (since now there's no single variable resulting from them).
  • It is not possible to initialize a variable with the same name as a named capture.
  • The pattern that results from this warning is renaming a capture something like "name2" and then having "name = name2" after the match. Ugly.

Risks of removing the warning:

  • When a named capture writes to an already-declared variable, that may surprise a user.

I think this risk is very low, especially since you'd have to turn on verbose mode to even see the warning.


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #9623: warning: "named capture conflicts a local variable" does not allow loops with named capturesClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0