Project

General

Profile

Bug #12359

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

Added by headius (Charles Nutter) over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
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

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

Associated revisions

Revision 75a68057
Added by nobu (Nobuyoshi Nakada) over 3 years ago

parse.y: no named capture conflict warnings

  • parse.y (reg_named_capture_assign_iter): remove named capture conflict warnings. it is just annoying rather than useful. [ruby-core:75416] [Bug #12359]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55396 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 55396
Added by nobu (Nobuyoshi Nakada) over 3 years ago

parse.y: no named capture conflict warnings

  • parse.y (reg_named_capture_assign_iter): remove named capture conflict warnings. it is just annoying rather than useful. [ruby-core:75416] [Bug #12359]

Revision 55396
Added by nobu (Nobuyoshi Nakada) over 3 years ago

parse.y: no named capture conflict warnings

  • parse.y (reg_named_capture_assign_iter): remove named capture conflict warnings. it is just annoying rather than useful. [ruby-core:75416] [Bug #12359]

Revision 55396
Added by nobu (Nobuyoshi Nakada) over 3 years ago

parse.y: no named capture conflict warnings

  • parse.y (reg_named_capture_assign_iter): remove named capture conflict warnings. it is just annoying rather than useful. [ruby-core:75416] [Bug #12359]

Revision 55396
Added by nobu (Nobuyoshi Nakada) over 3 years ago

parse.y: no named capture conflict warnings

  • parse.y (reg_named_capture_assign_iter): remove named capture conflict warnings. it is just annoying rather than useful. [ruby-core:75416] [Bug #12359]

History

Updated by headius (Charles Nutter) over 3 years ago

Related Ruby issue: https://bugs.ruby-lang.org/issues/9623

I think the warning needs to be softened to allow repeat use in literal regexp at the very least.

Updated by headius (Charles Nutter) over 3 years ago

I have worked around this for now in JRuby by modifying the warning to only fire if a named capture was previously declared as something other than a named capture.

The commit to JRuby is here: https://github.com/jruby/jruby/commit/3ebc198860905084231d2a1356ea46718b86f2b0

It resolves the verbose noise issue we had with our date/format implementation.

I propose that the warning should only fire when it is a mixed-use variable, i.e. it is potentially assigned both as a named capture and as a normal local variable.

I'd actually like to get rid of it altogether, but it's really just this mixed-use case that needs a warning.

#4

Updated by Eregon (Benoit Daloze) over 3 years ago

  • Related to Bug #9623: warning: "named capture conflicts a local variable" does not allow loops with named captures added
#7

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

  • Status changed from Open to Closed

Applied in changeset r55396.


parse.y: no named capture conflict warnings

  • parse.y (reg_named_capture_assign_iter): remove named capture conflict warnings. it is just annoying rather than useful. [ruby-core:75416] [Bug #12359]

Also available in: Atom PDF