MetaCredo.Check.Utils (MetaCredo v0.4.0)

View Source

Shared utilities for check implementations.

Provides function classification helpers to reduce false positives across security, observability, and other checks that match on function call names.

Summary

Functions

Returns true when value appears in the doc-strings set returned by doc_string_contents/1.

Collects string content from documentation attributes (@moduledoc, @doc, @typedoc).

Returns true if a variable name represents a module attribute (starts with @), which should be excluded from snake_case checks since module attribute names follow their own conventions.

Returns true if the string looks like a module name (PascalCase or contains dots like Enum.map), not a regular variable.

Safely extracts a node name from AST metadata as a string.

Returns true if the function name belongs to a well-known standard library module that should never be flagged as user-facing I/O, HTTP, auth, file operations, etc.

Returns true if a variable name is a well-known Elixir special form or compiler artifact that should be excluded from naming checks.

Types

doc_strings()

@type doc_strings() :: %{required(String.t()) => true}

Functions

doc_string?(doc_strings, value)

@spec doc_string?(doc_strings(), String.t()) :: boolean()

Returns true when value appears in the doc-strings set returned by doc_string_contents/1.

doc_string_contents(ast)

@spec doc_string_contents(Metastatic.AST.meta_ast()) :: doc_strings()

Collects string content from documentation attributes (@moduledoc, @doc, @typedoc).

Returns a MapSet of strings that appear as values of documentation module attributes. Checks can use this to skip doc strings during literal analysis, preventing false positives when documentation merely mentions patterns like Phoenix.HTML.raw/1 or URL examples.

module_attribute?(arg1)

@spec module_attribute?(String.t()) :: boolean()

Returns true if a variable name represents a module attribute (starts with @), which should be excluded from snake_case checks since module attribute names follow their own conventions.

module_name?(name)

@spec module_name?(String.t()) :: boolean()

Returns true if the string looks like a module name (PascalCase or contains dots like Enum.map), not a regular variable.

safe_name(meta, default \\ "")

@spec safe_name(
  keyword(),
  term()
) :: String.t()

Safely extracts a node name from AST metadata as a string.

Handles the case where :name is a list of AST tuples (e.g., quoted expressions in typespec files) instead of a plain string or atom. Returns inspect(value) for non-stringable values rather than crashing.

safe_stdlib_call?(func_name)

@spec safe_stdlib_call?(String.t()) :: boolean()

Returns true if the function name belongs to a well-known standard library module that should never be flagged as user-facing I/O, HTTP, auth, file operations, etc.

This prevents false positives like Keyword.get being flagged as an HTTP "get" call, or Map.fetch! being flagged as a database "fetch".

special_variable?(name)

@spec special_variable?(String.t()) :: boolean()

Returns true if a variable name is a well-known Elixir special form or compiler artifact that should be excluded from naming checks.