[前][次][番号順一覧][スレッド一覧]

ruby-changes:25292

From: nobu <ko1@a...>
Date: Sun, 28 Oct 2012 00:54:08 +0900 (JST)
Subject: [ruby-changes:25292] nobu:r37344 (trunk): parse.y: warn static content assign in cond

nobu	2012-10-28 00:49:09 +0900 (Sun, 28 Oct 2012)

  New Revision: 37344

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37344

  Log:
    parse.y: warn static content assign in cond
    
    * parse.y (assign_in_cond): warn for static content object asignments
      in conditional statements.  [ruby-dev:43083] [Feature #4299]

  Modified files:
    trunk/ChangeLog
    trunk/parse.y
    trunk/test/ruby/test_rubyoptions.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37343)
+++ ChangeLog	(revision 37344)
@@ -1,3 +1,8 @@
+Sun Oct 28 00:49:06 2012  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (assign_in_cond): warn for static content object asignments
+	  in conditional statements.  [ruby-dev:43083] [Feature #4299]
+
 Sat Oct 27 23:33:41 2012  Benoit Daloze  <eregontp@g...>
 
 	* gc.c (gc_profile_result, gc_profile_report): use internal structures
Index: parse.y
===================================================================
--- parse.y	(revision 37343)
+++ parse.y	(revision 37344)
@@ -8936,6 +8936,30 @@
 }
 
 static int
+is_static_content(NODE *node)
+{
+    if (!node) return 1;
+    switch (nd_type(node)) {
+      case NODE_HASH:
+	if (!(node = node->nd_head)) break;
+      case NODE_ARRAY:
+	do {
+	    if (!is_static_content(node->nd_head)) return 0;
+	} while ((node = node->nd_next) != 0);
+      case NODE_LIT:
+      case NODE_STR:
+      case NODE_NIL:
+      case NODE_TRUE:
+      case NODE_FALSE:
+      case NODE_ZARRAY:
+	break;
+      default:
+	return 0;
+    }
+    return 1;
+}
+
+static int
 assign_in_cond(struct parser_params *parser, NODE *node)
 {
     switch (nd_type(node)) {
@@ -8955,23 +8979,9 @@
     }
 
     if (!node->nd_value) return 1;
-    switch (nd_type(node->nd_value)) {
-      case NODE_LIT:
-      case NODE_STR:
-      case NODE_NIL:
-      case NODE_TRUE:
-      case NODE_FALSE:
+    if (is_static_content(node->nd_value)) {
 	/* reports always */
 	parser_warn(node->nd_value, "found = in conditional, should be ==");
-	return 1;
-
-      case NODE_DSTR:
-      case NODE_XSTR:
-      case NODE_DXSTR:
-      case NODE_EVSTR:
-      case NODE_DREGX:
-      default:
-	break;
     }
     return 1;
 }
Index: test/ruby/test_rubyoptions.rb
===================================================================
--- test/ruby/test_rubyoptions.rb	(revision 37343)
+++ test/ruby/test_rubyoptions.rb	(revision 37344)
@@ -331,12 +331,33 @@
     t.puts "  end"
     t.puts "end"
     t.close
-    err = ["#{t.path}:1: warning: found = in conditional, should be ==",
-           "#{t.path}:4: warning: found = in conditional, should be =="]
-    err = /\A(#{Regexp.quote(t.path)}):1(: warning: found = in conditional, should be ==)\n\1:4\2\Z/
+    warning = ' warning: found = in conditional, should be =='
+    err = ["#{t.path}:1:#{warning}",
+           "#{t.path}:4:#{warning}",
+          ]
     bug2136 = '[ruby-dev:39363]'
     assert_in_out_err(["-w", t.path], "", [], err, bug2136)
     assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err, bug2136)
+
+    t.open
+    t.truncate(0)
+    t.puts "if a = ''; end"
+    t.puts "if a = []; end"
+    t.puts "if a = [1]; end"
+    t.puts "if a = [a]; end"
+    t.puts "if a = {}; end"
+    t.puts "if a = {1=>2}; end"
+    t.puts "if a = {3=>a}; end"
+    t.close
+    err = ["#{t.path}:1:#{warning}",
+           "#{t.path}:2:#{warning}",
+           "#{t.path}:3:#{warning}",
+           "#{t.path}:5:#{warning}",
+           "#{t.path}:6:#{warning}",
+          ]
+    feature4299 = '[ruby-dev:43083]'
+    assert_in_out_err(["-w", t.path], "", [], err, feature4299)
+    assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err, feature4299)
   ensure
     t.close(true) if t
   end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]