檢視原始碼 erl_lint (stdlib v6.2)

Erlang 程式碼的檢查器。

此模組用於檢查 Erlang 程式碼中不合法的語法和其他錯誤。它也會針對不建議的程式碼實作方式發出警告。

偵測到的錯誤包括:

  • 重新定義和未定義的函式
  • 未綁定和不安全的變數
  • 不合法的記錄使用

偵測到的警告包括:

  • 未使用的函式和導入
  • 未使用的變數
  • 導入到模式匹配的變數
  • if/case/receive 導出的變數
  • 在 fun 和列表解析中被遮蔽的變數

某些警告是可選的,可以透過指定下方描述的適當選項來開啟。

此模組中的函式會由 Erlang 編譯器自動調用。除非您已撰寫自己的 Erlang 編譯器,否則沒有理由單獨調用這些函式。

錯誤資訊

ErrorInfo 是所有 I/O 模組傳回的標準 ErrorInfo 結構。格式如下:

{ErrorLine, Module, ErrorDescriptor}

可以使用以下呼叫取得描述錯誤的字串:

Module:format_error(ErrorDescriptor)

參閱

epp, erl_parse

摘要

函式

接受一個 ErrorDescriptor 並傳回描述錯誤或警告的字串。當處理 ErrorInfo 結構時,通常會隱式呼叫此函式 (請參閱 錯誤資訊 章節)。

測試 Expr 是否為合法的 guard 測試。Expr 是代表運算式抽象形式的 Erlang 項。erl_parse:parse_exprs(Tokens) 可用於產生 Expr 的列表。

等同於 module/3

檢查模組中的所有形式是否有錯誤。它會傳回:

型別

-type error_description() :: term().
-type error_info() :: {erl_anno:location() | none, module(), error_description()}.
-type fa() :: {atom(), arity()}.
-type fun_used_vars() :: #{erl_parse:abstract_expr() => {[atom()], fun_used_vars()}}.

函式

此函式的連結

format_error(ErrorDescriptor)

檢視原始碼
-spec format_error(ErrorDescriptor) -> io_lib:chars() when ErrorDescriptor :: error_description().

接受一個 ErrorDescriptor 並傳回描述錯誤或警告的字串。當處理 ErrorInfo 結構時,通常會隱式呼叫此函式 (請參閱 錯誤資訊 章節)。

-spec is_guard_test(Expr) -> boolean() when Expr :: erl_parse:abstract_expr().

測試 Expr 是否為合法的 guard 測試。Expr 是代表運算式抽象形式的 Erlang 項。erl_parse:parse_exprs(Tokens) 可用於產生 Expr 的列表。

-spec module(AbsForms) -> {ok, Warnings} | {error, Errors, Warnings}
                when
                    AbsForms :: [erl_parse:abstract_form() | erl_parse:form_info()],
                    Warnings :: [{SourceFile, [ErrorInfo]}],
                    Errors :: [{SourceFile, [ErrorInfo]}],
                    SourceFile :: file:filename(),
                    ErrorInfo :: error_info().

等同於 module/3

此函式的連結

module(AbsForms, FileName)

檢視原始碼
-spec module(AbsForms, FileName) -> {ok, Warnings} | {error, Errors, Warnings}
                when
                    AbsForms :: [erl_parse:abstract_form() | erl_parse:form_info()],
                    FileName :: atom() | string(),
                    Warnings :: [{SourceFile, [ErrorInfo]}],
                    Errors :: [{SourceFile, [ErrorInfo]}],
                    SourceFile :: file:filename(),
                    ErrorInfo :: error_info().

等同於 module/3

此函式的連結

module(AbsForms, FileName, CompileOptions)

檢視原始碼
-spec module(AbsForms, FileName, CompileOptions) -> {ok, Warnings} | {error, Errors, Warnings}
                when
                    AbsForms :: [erl_parse:abstract_form() | erl_parse:form_info()],
                    FileName :: atom() | string(),
                    CompileOptions :: [compile:option()],
                    Warnings :: [{SourceFile, [ErrorInfo]}],
                    Errors :: [{SourceFile, [ErrorInfo]}],
                    SourceFile :: file:filename(),
                    ErrorInfo :: error_info().

檢查模組中的所有形式是否有錯誤。它會傳回:

  • {ok,Warnings} - 模組中沒有錯誤。

  • {error,Errors,Warnings} - 模組中有錯誤。

由於此模組僅對編譯器的維護者感興趣,並且為了避免在兩個地方出現相同的描述,因此控制警告的 Options 元素僅在 compile 模組中描述。

模組的 AbsForms 來自透過 Erlang 前置處理器 epp 讀取的文件,可能來自許多檔案。這表示任何對錯誤的參照都必須包含檔案名稱,請參閱 epp 模組或剖析器 (請參閱 erl_parse 模組)。傳回的錯誤和警告具有以下格式:

[{SourceFile,[ErrorInfo]}]

錯誤和警告會按照它們在形式中遇到的順序列出。因此,來自一個檔案的錯誤可以分割成錯誤清單中的不同項目。