<commit_before>defmodule Credo.Check.Readability.PreferImplicitTry do
  @moduledoc """
We don't need to explicity use `try` in function definitions. For example, this:

defmodule ModuleWithRescue do
  def failing_function(first) do
    try do
      to_string(first)
    rescue
      _ -> :rescued
    end
  end
end

Could be rewritten without `try` as below:

defmodule ModuleWithRescue do
  def failing_function(first) do
    to_string(first)
  rescue
    _ -> :rescued
  end
end
"""

  @explanation [check: @moduledoc]

  use Credo.Check, base_priority: :low

  def run(%SourceFile{} = source_file, params \\ []) do
    issue_meta = IssueMeta.for(source_file, params)

    Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
  end

  defp traverse({:def, _, [{_, _, _}, [do: {:try, [line: line_no], _}]]} = ast, issues, issue_meta) do
    {ast, issues ++ [issue_for(issue_meta, line_no)]}
  end

  defp traverse(ast, issues, _issue_meta) do
    {ast, issues}
  end

  defp issue_for(issue_meta, line_no) do
    format_issue issue_meta,
      message: "Prefer using an implicit `try` rather than explicit `try`",
      trigger: "try",
      line_no: line_no
  end
end<commit_msg>Improve docs and fix indent for PreferImplicitTry<commit_after>defmodule Credo.Check.Readability.PreferImplicitTry do
  @moduledoc """
  Prefer using an implicit `try` rather than explicit `try` if you try to rescue
  anything the function does.

  For example, this:

      def failing_function(first) do
        try do
          to_string(first)
        rescue
          _ -> :rescued
        end
      end

  Can be rewritten without `try` as below:

      def failing_function(first) do
        to_string(first)
      rescue
        _ -> :rescued
      end

  Like all `Readability` issues, this one is not a technical concern.
  The code will behave identical in both ways.
  """

  @explanation [check: @moduledoc]

  use Credo.Check, base_priority: :low

  def run(%SourceFile{} = source_file, params \\ []) do
    issue_meta = IssueMeta.for(source_file, params)

    Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
  end

  defp traverse({:def, _, [{_, _, _}, [do: {:try, [line: line_no], _}]]} = ast, issues, issue_meta) do
    {ast, issues ++ [issue_for(issue_meta, line_no)]}
  end

  defp traverse(ast, issues, _issue_meta) do
    {ast, issues}
  end

  defp issue_for(issue_meta, line_no) do
    format_issue issue_meta,
      message: "Prefer using an implicit `try` rather than explicit `try`.",
      trigger: "try",
      line_no: line_no
  end
end
{'fraction_non_alphanumeric': 0.10774017891870867, 'fraction_numerical': 0.0015558148580318942, 'mean_word_length': 3.279534109816972, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2362128', 'n_tokens_mistral': 894, 'n_tokens_neox': 859, 'n_words': 252}