Do not report if Object#to_s is called without receiver

This commit is contained in:
Vitalii Elenhaupt 2020-03-26 23:51:54 +02:00
parent ff1669ebe8
commit 59bc021e57
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
2 changed files with 15 additions and 1 deletions

View file

@ -52,6 +52,20 @@ module Ameba::Rule::Lint
subject.catch(s).should_not be_valid
end
it "doesn't report if Object#to_s is called with arguments" do
s = Source.new %q(
/\w #{name.to_s(io)}/
)
subject.catch(s).should be_valid
end
it "doesn't report if Object#to_s is called without receiver" do
s = Source.new %q(
/\w #{to_s}/
)
subject.catch(s).should be_valid
end
it "reports rule, location and message" do
s = Source.new %q(
"Hello, #{name1.to_s}"

View file

@ -36,7 +36,7 @@ module Ameba::Rule::Lint
private def string_coercion_nodes(node)
node.expressions.select do |e|
e.is_a?(Crystal::Call) && e.name == "to_s" && e.args.size == 0
e.is_a?(Crystal::Call) && e.name == "to_s" && e.args.size.zero? && e.obj
end
end
end