From c4e7047563d9374d1b8fe8f93225dca1c5365cad Mon Sep 17 00:00:00 2001 From: Margret Riegert Date: Sat, 25 Jan 2025 06:48:41 -0500 Subject: [PATCH 1/2] Casts can have a side-effect and thus their object can be used --- src/ameba/ast/visitors/implicit_return_visitor.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ameba/ast/visitors/implicit_return_visitor.cr b/src/ameba/ast/visitors/implicit_return_visitor.cr index a3deb745e..f21b84caf 100644 --- a/src/ameba/ast/visitors/implicit_return_visitor.cr +++ b/src/ameba/ast/visitors/implicit_return_visitor.cr @@ -25,7 +25,7 @@ module Ameba::AST if exp.is_a?(Crystal::ControlExpression) incr_stack { exp.accept(self) } break - elsif idx == last_idx && old_stack > 0 + elsif idx == last_idx && old_stack.positive? incr_stack { exp.accept(self) } else exp.accept(self) @@ -162,7 +162,7 @@ module Ameba::AST def visit(node : Crystal::Cast | Crystal::NilableCast) : Bool @rule.test(@source, node, @stack.positive?) - node.obj.accept(self) + incr_stack { node.obj.accept(self) } false end From f50ff91f1d0736ea3b1a4a1ce8e32c362d6e0d42 Mon Sep 17 00:00:00 2001 From: Margret Riegert Date: Tue, 4 Feb 2025 14:42:52 -0500 Subject: [PATCH 2/2] Add unused literal spec for cast objs --- spec/ameba/rule/lint/unused_literal_spec.cr | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/ameba/rule/lint/unused_literal_spec.cr b/spec/ameba/rule/lint/unused_literal_spec.cr index 5fc58aa39..65c4a5594 100644 --- a/spec/ameba/rule/lint/unused_literal_spec.cr +++ b/spec/ameba/rule/lint/unused_literal_spec.cr @@ -72,6 +72,13 @@ module Ameba::Rule::Lint CRYSTAL end + it "passes if a literal value is the object of a cast" do + expect_no_issues subject, <<-CRYSTAL + 1.as(Int64) + "2".as?(String) + CRYSTAL + end + it "fails if literals are top-level" do expect_issue subject, <<-CRYSTAL 1234