Ecto变更集和透析器错误
问题描述:
我有一个雨伞应用程序。我看到了Dialyzer的价值,我正试着开始使用它。我已经相当远了,但我有一个问题,我无法解决Ecto。Ecto变更集和透析器错误
这是一个处理认证的小型应用程序。我可以用最简单的例子来修剪它。
使用Elixir 1.4.2和Dialyxir 0.4.0。在问题
代码
defmodule Auth.Account do
use Ecto.Schema
import Ecto.Changeset
schema "auth_accounts" do
field :email, :string
field :password_hash, :string
field :password, :string, virtual: true
timestamps()
end
def build(params \\ %{}) do
changeset(%__MODULE__{}, params)
end
def changeset(account, params \\ %{}) do
account
|> cast(params, ~w(email password))
end
end
相关的错误输出
lib/auth/account.ex:13: Function build/0 has no local return
lib/auth/account.ex:13: Function build/1 has no local return
lib/auth/account.ex:14: The call 'Elixir.Auth.Account':changeset(#{'__meta__':=#{'__struct__':='Elixir.Ecto.Schema.Metadata', 'context':='nil', 'source':={'nil',<<_:104>>}, 'state':='built'}, '__struct__':='Elixir.Auth.Account', 'email':='nil', 'id':='nil', 'inserted_at':='nil', 'password':='nil', 'password_hash':='nil', 'updated_at':='nil'},[email protected]::any())
will never return since it differs in the 1st argument from the success typing arguments: ({map(),map()} | #{'__struct__':=atom(), 'action'=>'delete' | 'insert' | 'nil' | 'replace' | 'update', 'changes'=>#{atom()=>_}, 'constraints'=>[#{'constraint':=binary(), 'field':=atom(), 'match':='exact' | 'suffix', 'message':={_,_}, 'type':='unique'}], 'data'=>'nil' | #{'__struct__':=atom()}, 'empty_values'=>_, 'errors'=>[{atom(),{_,_}}], 'filters'=>#{atom()=>_}, 'params'=>'nil' | #{binary()=>_}, 'prepare'=>[fun((map()) -> map())], 'repo'=>atom(), 'required'=>[atom()], 'types'=>'nil' | #{atom()=>atom() | {'array',_} | {'embed',map()} | {'in',_} | {'map',_}}, 'valid?'=>boolean(), 'validations'=>[{atom(),_}]},'invalid' | #{'__struct__'=>none(), atom() | binary()=>_})
它出现的问题是围绕着build
功能的使用%__MODULE__{}
。看到这个相关Stack Overflow Topic。
但是,我只是无法弄清楚有效的替代语法。
答
Dogbert促使我深入挖掘,无法重现它。
我在ecto〜> 2.0。 mix.lock文件让我在2.0.5。在mix deps.unlock --all
和mix deps.clean --all
和mix deps.get
之后,我被提升到ecto 2.1.3。
图书馆升级后,透析器不再抱怨这一点。所以我的修复是升级到更新的ecto版本。
这是否修复了警告? 'def build(params \\%{})do'? – Dogbert
不幸的不是。我更新了上面的代码,以免混淆其他代码。但我只是重新测试来验证。问题依然存在。确保PLT重建。 –
Scratch,如果我复制粘贴你的确切代码在一个新的混合包中,我无法在本地重现。你用什么版本的Ecto?如果您将该模块复制到全新的混合包中,您是否能够重现此项目? (FWIW我不认为这个错误与你连接的其他问题有关。) – Dogbert