RedundantStringCoercion: do not report to_s with named args

closes #160
This commit is contained in:
Vitalii Elenhaupt 2020-06-24 09:25:28 +03:00
parent 3448de30da
commit 7f501a1df5
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
2 changed files with 12 additions and 1 deletions

View file

@ -66,6 +66,13 @@ module Ameba::Rule::Lint
subject.catch(s).should be_valid subject.catch(s).should be_valid
end end
it "doesn't report if Object#to_s is called with named args" do
s = Source.new %q(
"0x#{250.to_s base: 16}"
)
subject.catch(s).should be_valid
end
it "reports rule, location and message" do it "reports rule, location and message" do
s = Source.new %q( s = Source.new %q(
"Hello, #{name1.to_s}" "Hello, #{name1.to_s}"

View file

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