[Bug #19919] Warn class variable assignment and constant declaration in condition
This commit is contained in:
parent
dcee3cc6ce
commit
2794a8fef6
6
parse.y
6
parse.y
@ -1835,6 +1835,10 @@ get_nd_value(struct parser_params *p, NODE *node)
|
|||||||
return RNODE_DASGN(node)->nd_value;
|
return RNODE_DASGN(node)->nd_value;
|
||||||
case NODE_MASGN:
|
case NODE_MASGN:
|
||||||
return RNODE_MASGN(node)->nd_value;
|
return RNODE_MASGN(node)->nd_value;
|
||||||
|
case NODE_CVASGN:
|
||||||
|
return RNODE_CVASGN(node)->nd_value;
|
||||||
|
case NODE_CDECL:
|
||||||
|
return RNODE_CDECL(node)->nd_value;
|
||||||
default:
|
default:
|
||||||
compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
|
compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
|
||||||
return 0;
|
return 0;
|
||||||
@ -14044,6 +14048,8 @@ assign_in_cond(struct parser_params *p, NODE *node)
|
|||||||
case NODE_DASGN:
|
case NODE_DASGN:
|
||||||
case NODE_GASGN:
|
case NODE_GASGN:
|
||||||
case NODE_IASGN:
|
case NODE_IASGN:
|
||||||
|
case NODE_CVASGN:
|
||||||
|
case NODE_CDECL:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -895,12 +895,14 @@ x = __ENCODING__
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_assign_in_conditional
|
def test_assign_in_conditional
|
||||||
|
# multiple assignment
|
||||||
assert_warning(/`= literal' in conditional/) do
|
assert_warning(/`= literal' in conditional/) do
|
||||||
eval <<-END, nil, __FILE__, __LINE__+1
|
eval <<-END, nil, __FILE__, __LINE__+1
|
||||||
(x, y = 1, 2) ? 1 : 2
|
(x, y = 1, 2) ? 1 : 2
|
||||||
END
|
END
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# instance variable assignment
|
||||||
assert_warning(/`= literal' in conditional/) do
|
assert_warning(/`= literal' in conditional/) do
|
||||||
eval <<-END, nil, __FILE__, __LINE__+1
|
eval <<-END, nil, __FILE__, __LINE__+1
|
||||||
if @x = true
|
if @x = true
|
||||||
@ -910,6 +912,71 @@ x = __ENCODING__
|
|||||||
end
|
end
|
||||||
END
|
END
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# local variable assignment
|
||||||
|
assert_warning(/`= literal' in conditional/) do
|
||||||
|
eval <<-END, nil, __FILE__, __LINE__+1
|
||||||
|
def m
|
||||||
|
if x = true
|
||||||
|
1
|
||||||
|
else
|
||||||
|
2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
END
|
||||||
|
end
|
||||||
|
|
||||||
|
# global variable assignment
|
||||||
|
assert_separately([], <<-RUBY)
|
||||||
|
assert_warning(/`= literal' in conditional/) do
|
||||||
|
eval <<-END, nil, __FILE__, __LINE__+1
|
||||||
|
if $x = true
|
||||||
|
1
|
||||||
|
else
|
||||||
|
2
|
||||||
|
end
|
||||||
|
END
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
# dynamic variable assignment
|
||||||
|
assert_warning(/`= literal' in conditional/) do
|
||||||
|
eval <<-END, nil, __FILE__, __LINE__+1
|
||||||
|
y = 1
|
||||||
|
|
||||||
|
1.times do
|
||||||
|
if y = true
|
||||||
|
1
|
||||||
|
else
|
||||||
|
2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
END
|
||||||
|
end
|
||||||
|
|
||||||
|
# class variable assignment
|
||||||
|
assert_warning(/`= literal' in conditional/) do
|
||||||
|
eval <<-END, nil, __FILE__, __LINE__+1
|
||||||
|
c = Class.new
|
||||||
|
class << c
|
||||||
|
if @@a = 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
END
|
||||||
|
end
|
||||||
|
|
||||||
|
# constant declaration
|
||||||
|
assert_separately([], <<-RUBY)
|
||||||
|
assert_warning(/`= literal' in conditional/) do
|
||||||
|
eval <<-END, nil, __FILE__, __LINE__+1
|
||||||
|
if Const = true
|
||||||
|
1
|
||||||
|
else
|
||||||
|
2
|
||||||
|
end
|
||||||
|
END
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_literal_in_conditional
|
def test_literal_in_conditional
|
||||||
|
Loading…
x
Reference in New Issue
Block a user