Skip to content

Commit

Permalink
- fix a false positive for endless method definition
Browse files Browse the repository at this point in the history
Fixes: #795.

This PR fixes false positives that the comparison methods recognizes as
a setter method.
  • Loading branch information
koic committed Apr 20, 2021
1 parent 21684f1 commit 7fa7dd9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/parser/ruby30.y
Expand Up @@ -3071,7 +3071,7 @@ require 'parser'
end
def endless_method_name(name_t)
if name_t[0].end_with?('=')
if !%w[== === >= <= !=].include?(name_t[0]) && name_t[0].end_with?('=')
diagnostic :error, :endless_setter, nil, name_t
end
end
2 changes: 1 addition & 1 deletion lib/parser/ruby31.y
Expand Up @@ -3071,7 +3071,7 @@ require 'parser'
end
def endless_method_name(name_t)
if name_t[0].end_with?('=')
if !%w[== === >= <= !=].include?(name_t[0]) && name_t[0].end_with?('=')
diagnostic :error, :endless_setter, nil, name_t
end
end
67 changes: 67 additions & 0 deletions test/test_parser.rb
Expand Up @@ -10107,6 +10107,73 @@ def test_endless_setter
SINCE_3_0)
end

def test_endless_method_comparison_method
assert_parses(
s(:def, :==,
s(:args,
s(:arg, :other)),
s(:send, nil, :do_something)),
%q{def ==(other) = do_something},
%q{~~~ keyword
| ~~ name
| ^ assignment
|! end
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression},
SINCE_3_0)

assert_parses(
s(:def, :===,
s(:args,
s(:arg, :other)),
s(:send, nil, :do_something)),
%q{def ===(other) = do_something},
%q{~~~ keyword
| ~~~ name
| ^ assignment
|! end
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression},
SINCE_3_0)

assert_parses(
s(:def, :>=,
s(:args,
s(:arg, :other)),
s(:send, nil, :do_something)),
%q{def >=(other) = do_something},
%q{~~~ keyword
| ~~ name
| ^ assignment
|! end
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression},
SINCE_3_0)

assert_parses(
s(:def, :<=,
s(:args,
s(:arg, :other)),
s(:send, nil, :do_something)),
%q{def <=(other) = do_something},
%q{~~~ keyword
| ~~ name
| ^ assignment
|! end
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression},
SINCE_3_0)

assert_parses(
s(:def, :!=,
s(:args,
s(:arg, :other)),
s(:send, nil, :do_something)),
%q{def !=(other) = do_something},
%q{~~~ keyword
| ~~ name
| ^ assignment
|! end
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression},
SINCE_3_0)
end

def test_endless_method_without_args
assert_parses(
s(:def, :foo,
Expand Down

0 comments on commit 7fa7dd9

Please sign in to comment.