Skip to content

Commit

Permalink
Keep unused literal nodes
Browse files Browse the repository at this point in the history
For static analysis, it’s better to keep unused literal nodes.
If simply change `block_append` to fall through, both "unused literal ignored"
and "possibly useless use of a literal in void context" warnings
are shown for the same line. But it’s verbose then remove
"unused literal ignored" warning.

This kind of optimization is already implemented on compile.c.
`compile_block` calls `iseq_compile_each0` with `popped = 1` when NODE_BLOCK
has next.
  • Loading branch information
yui-knk committed Oct 30, 2023
1 parent ab4781b commit 5a8f255
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 14 deletions.
8 changes: 0 additions & 8 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -12291,14 +12291,6 @@ block_append(struct parser_params *p, NODE *head, NODE *tail)

if (h == 0) return tail;
switch (nd_type(h)) {
case NODE_LIT:
case NODE_STR:
case NODE_SELF:
case NODE_TRUE:
case NODE_FALSE:
case NODE_NIL:
parser_warning(p, h, "unused literal ignored");
return tail;
default:
h = end = NEW_BLOCK(head, &head->nd_loc);
RNODE_BLOCK(end)->nd_end = end;
Expand Down
9 changes: 4 additions & 5 deletions test/ruby/test_parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -876,18 +876,17 @@ class << o; self; end.instance_eval do
def test_void_expr_stmts_value
x = 1
useless_use = /useless use/
unused = /unused/
assert_nil assert_warning(useless_use) {eval("x; nil")}
assert_nil assert_warning(useless_use) {eval("1+1; nil")}
assert_nil assert_warning('') {eval("1.+(1); nil")}
assert_nil assert_warning(useless_use) {eval("TestParse; nil")}
assert_nil assert_warning(useless_use) {eval("::TestParse; nil")}
assert_nil assert_warning(useless_use) {eval("x..x; nil")}
assert_nil assert_warning(useless_use) {eval("x...x; nil")}
assert_nil assert_warning(unused) {eval("self; nil")}
assert_nil assert_warning(unused) {eval("nil; nil")}
assert_nil assert_warning(unused) {eval("true; nil")}
assert_nil assert_warning(unused) {eval("false; nil")}
assert_nil assert_warning(useless_use) {eval("self; nil")}
assert_nil assert_warning(useless_use) {eval("nil; nil")}
assert_nil assert_warning(useless_use) {eval("true; nil")}
assert_nil assert_warning(useless_use) {eval("false; nil")}
assert_nil assert_warning(useless_use) {eval("defined?(1); nil")}
assert_equal 1, x

Expand Down
2 changes: 1 addition & 1 deletion test/ruby/test_pattern_matching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ def test_hash_pattern
end
end

bug18890 = assert_warning(/(?:.*:[47]: warning: unused literal ignored\n){2}/) do
bug18890 = assert_warning(/(?:.*:[47]: warning: possibly useless use of a literal in void context\n){2}/) do
eval("#{<<~';;;'}")
proc do |i|
case i
Expand Down

0 comments on commit 5a8f255

Please sign in to comment.