ForgeCredoChecks.MapNewFromInto (forge_credo_checks v0.3.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Replace Enum.into(%{}, fn ...) with Map.new/2.

Why

Map.new/2 is the idiomatic API for building a map from an enumerable with a transform function. Enum.into(%{}, fn ...) does the same work with awkward syntax that obscures the intent.

How to fix

# BEFORE
enum |> Enum.into(%{}, fn {k, v} -> {String.downcase(k), v} end)

# AFTER
Map.new(enum, fn {k, v} -> {String.downcase(k), v} end)

The transform function is identical; only the call site changes.

Both nested and piped forms are flagged:

Enum.into(enum, %{}, fn ... end)        # also flagged
enum |> Enum.into(%{}, fn ... end)      # also flagged

Note

Stock Credo's Refactor.MapInto only catches Enum.map |> Enum.into(%{}). This check covers the direct Enum.into(%{}, fn) form Stock Credo misses.

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.