Skip to content

Commit

Permalink
+ builder.rb: warn it in a block with no ordinary params. (#980)
Browse files Browse the repository at this point in the history
This commit tracks upstream commit ruby/ruby@ae76c8a.
  • Loading branch information
iliabylich committed Dec 29, 2023
1 parent fd23193 commit 7c9498f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,13 @@ def accessible(node)
end

unless @parser.static_env.declared?(name)
if @parser.version == 33 &&
name == :it &&
@parser.context.in_block &&
!@parser.max_numparam_stack.has_ordinary_params?
diagnostic :warning, :ambiguous_it_call, nil, node.loc.expression
end

return n(:send, [ nil, name ],
var_send_map(node))
end
Expand Down
1 change: 1 addition & 0 deletions lib/parser/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module Parser
# Parser warnings
:useless_else => 'else without rescue is useless',
:duplicate_hash_key => 'key is duplicated and overwritten',
:ambiguous_it_call => '`it` calls without arguments refers to the first block param',

# Parser errors that are not Ruby errors
:invalid_encoding => 'literal contains escape sequences incompatible with UTF-8',
Expand Down
31 changes: 31 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11409,4 +11409,35 @@ def test_ungettable_gvar
' ^^^^^^ location',
ALL_VERSIONS)
end

def test_it_warning_in_33
refute_diagnoses(
'if false; it; end',
ALL_VERSIONS)
refute_diagnoses(
'def foo; it; end',
ALL_VERSIONS)
assert_diagnoses(
[:warning, :ambiguous_it_call, {}],
'0.times { it }',
' ^^ location',
['3.3'])
refute_diagnoses(
'0.times { || it }',
ALL_VERSIONS)
refute_diagnoses(
'0.times { |_n| it }',
ALL_VERSIONS)
assert_diagnoses(
[:warning, :ambiguous_it_call, {}],
'0.times { it; it = 1; it }',
' ^^ location',
['3.3'])
refute_diagnoses(
'0.times { it = 1; it }',
ALL_VERSIONS)
refute_diagnoses(
'it = 1; 0.times { it }',
ALL_VERSIONS)
end
end

0 comments on commit 7c9498f

Please sign in to comment.