From 2dd4fb3d59db6d040e2225ce13363461978b8180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20F=C3=B6hring?= Date: Fri, 9 Feb 2024 11:15:38 +0000 Subject: [PATCH] Fix Spec for unquote in def name Refs #1114 --- lib/credo/check/readability/specs.ex | 6 ++++++ test/credo/check/readability/specs_test.exs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/credo/check/readability/specs.ex b/lib/credo/check/readability/specs.ex index 043f32e98..9843e9cd1 100644 --- a/lib/credo/check/readability/specs.ex +++ b/lib/credo/check/readability/specs.ex @@ -110,6 +110,12 @@ defmodule Credo.Check.Readability.Specs do end defp issue_for(issue_meta, line_no, trigger) do + trigger = if is_tuple(trigger) do + Macro.to_string(trigger) + else + trigger + end + format_issue( issue_meta, message: "Functions should have a @spec type specification.", diff --git a/test/credo/check/readability/specs_test.exs b/test/credo/check/readability/specs_test.exs index f36a526f3..3d21d330f 100644 --- a/test/credo/check/readability/specs_test.exs +++ b/test/credo/check/readability/specs_test.exs @@ -125,6 +125,7 @@ defmodule Credo.Check.Readability.SpecsTest do |> refute_issues() end + # # cases raising issues # @@ -195,4 +196,21 @@ defmodule Credo.Check.Readability.SpecsTest do assert issue.trigger == "foo" end) end + + test "it should report/not crash for unquote/1 calls in the function name" do + ~S""" + defmodule SpecIssue do + function_name = :do_something + + def unquote(function_name)() do + :ok + end + end + """ + |> to_source_file() + |> run_check(@described_check) + |> assert_issue(fn issue -> + assert issue.trigger == "unquote(function_name)" + end) + end end