dialyzer-configuration

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dialyzer Configuration

Dialyzer 配置

Dialyzer is a static analysis tool for Erlang and Elixir that identifies software discrepancies such as type errors, unreachable code, and unnecessary tests.
Dialyzer是一款针对Erlang和Elixir的静态分析工具,可识别软件中的不一致问题,例如类型错误、不可达代码和冗余测试。

Configuration Files

配置文件

dialyzer.ignore-warnings

dialyzer.ignore-warnings

undefined
undefined

Ignore specific warnings

Ignore specific warnings

lib/my_module.ex:42:pattern_match_cov
undefined
lib/my_module.ex:42:pattern_match_cov
undefined

.dialyzer_ignore.exs

.dialyzer_ignore.exs

elixir
[
  {"lib/generated_code.ex", :no_return},
  {~r/lib\/legacy\/.*/, :unknown_function}
]
elixir
[
  {"lib/generated_code.ex", :no_return},
  {~r/lib\/legacy\/.*/, :unknown_function}
]

mix.exs Configuration

mix.exs 配置

elixir
def project do
  [
    app: :my_app,
    dialyzer: [
      plt_add_apps: [:mix, :ex_unit],
      plt_core_path: "priv/plts",
      plt_file: {:no_warn, "priv/plts/dialyzer.plt"},
      flags: [:error_handling, :underspecs, :unmatched_returns],
      ignore_warnings: ".dialyzer_ignore.exs",
      list_unused_filters: true
    ]
  ]
end
elixir
def project do
  [
    app: :my_app,
    dialyzer: [
      plt_add_apps: [:mix, :ex_unit],
      plt_core_path: "priv/plts",
      plt_file: {:no_warn, "priv/plts/dialyzer.plt"},
      flags: [:error_handling, :underspecs, :unmatched_returns],
      ignore_warnings: ".dialyzer_ignore.exs",
      list_unused_filters: true
    ]
  ]
end

Common Configuration Options

常用配置选项

PLT Management

PLT 管理

  • plt_add_apps
    : Additional applications to include in PLT
  • plt_core_path
    : Directory for core PLT files
  • plt_file
    : Custom PLT file location
  • plt_add_deps
    : Include dependencies (
    :app_tree
    ,
    :apps_direct
    ,
    :transitive
    )
  • plt_add_apps
    : 要添加到PLT中的额外应用
  • plt_core_path
    : 核心PLT文件的存储目录
  • plt_file
    : 自定义PLT文件的位置
  • plt_add_deps
    : 包含依赖项(
    :app_tree
    ,
    :apps_direct
    ,
    :transitive

Analysis Flags

分析标志

  • :error_handling
    - Check error handling
  • :underspecs
    - Warn on under-specified functions
  • :unmatched_returns
    - Warn on unmatched return values
  • :unknown
    - Warn on unknown functions/types
  • :overspecs
    - Warn on over-specified functions
  • :error_handling
    - 检查错误处理逻辑
  • :underspecs
    - 针对规格不足的函数发出警告
  • :unmatched_returns
    - 针对不匹配的返回值发出警告
  • :unknown
    - 针对未知函数/类型发出警告
  • :overspecs
    - 针对过度规格化的函数发出警告

Filter Options

过滤选项

  • ignore_warnings
    : File with warning patterns to ignore
  • list_unused_filters
    : Show unused ignore patterns
  • ignore_warnings
    : 包含要忽略的警告模式的文件
  • list_unused_filters
    : 显示未使用的忽略模式

Best Practices

最佳实践

  1. Incremental PLT Building: Use project-specific PLTs to speed up analysis
  2. Gradual Adoption: Start with subset of checks, expand over time
  3. CI Integration: Run Dialyzer in continuous integration
  4. Type Specs: Add comprehensive @spec annotations
  5. Warning Management: Document intentional ignores
  1. 增量PLT构建:使用项目专属PLT以加快分析速度
  2. 逐步采用:从部分检查开始,逐步扩展覆盖范围
  3. CI集成:在持续集成流程中运行Dialyzer
  4. 类型规范:添加全面的@spec注解
  5. 警告管理:记录有意忽略的警告内容

Common Patterns

常见模式

Conditional Analysis

条件分析

elixir
if Mix.env() in [:dev, :test] do
  {:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false}
end
elixir
if Mix.env() in [:dev, :test] do
  {:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false}
end

Custom Check Script

自定义检查脚本

bash
#!/bin/bash
mix dialyzer --format github
bash
#!/bin/bash
mix dialyzer --format github

GitHub Actions Integration

GitHub Actions 集成

yaml
- name: Run Dialyzer
  run: mix dialyzer --format github
yaml
- name: Run Dialyzer
  run: mix dialyzer --format github