From 59bc021e57128386e6a71de7a0396dee081165b6 Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Thu, 26 Mar 2020 23:51:54 +0200 Subject: [PATCH] Do not report if Object#to_s is called without receiver --- .../rule/lint/redundant_string_cercion_spec.cr | 14 ++++++++++++++ src/ameba/rule/lint/redundant_string_coercion.cr | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spec/ameba/rule/lint/redundant_string_cercion_spec.cr b/spec/ameba/rule/lint/redundant_string_cercion_spec.cr index 4389300a..d2c7a824 100644 --- a/spec/ameba/rule/lint/redundant_string_cercion_spec.cr +++ b/spec/ameba/rule/lint/redundant_string_cercion_spec.cr @@ -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}" diff --git a/src/ameba/rule/lint/redundant_string_coercion.cr b/src/ameba/rule/lint/redundant_string_coercion.cr index 7b92ff23..b8fae386 100644 --- a/src/ameba/rule/lint/redundant_string_coercion.cr +++ b/src/ameba/rule/lint/redundant_string_coercion.cr @@ -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