请问一个警告问题

作者 Harry_xu 所属板块 问答
Unsafe variable found at: lib/nfl/bot.ex:1241 warning: the variable "orderId" is unsafe as it has been set inside a case/cond/receive/if/&&/||. Please explicitly return the variable value instead. For example: case int do 1 -> atom = :one 2 -> atom = :two end should be written as atom = case int do 1 -> :one 2 -> :two end 像这样的警告,我只是从数据库中查询一个数据信息而已,尝试着改了很多结构,还是不行,求大神看一下 def handle_info(:order, %{instanceId: {:order, orderId} = instanceId, requiregolds: gold, initialgolds: initcoins, user: %{coins: coins}} = state) do xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx..... cards = case batch do %{1 => 200, 2 => %{2 => 0}} -> if (gold+initcoins) > coins do {_, info} = List.first :ets.lookup :tradebots, instanceId %{user: user} = info user = Map.merge user, %{coins: coins} info = Map.merge info, %{user: user} info = if Map.has_key?(info, :get_golds) do cond do info.get_golds < coins -> Map.merge info, %{get_golds: coins} true -> info end else Map.merge info, %{get_golds: coins} end :ets.insert :tradebots, {instanceId, info} :ets.tab2file :tradebots, 'tradebots.ets' case Database.Orders.read!(orderId) do <--警告位置 %{details: %{user: user} = db_info} = details -> user = Map.merge user, %{coins: coins} db_info = Map.merge db_info, %{user: user} details = Map.merge details, %{details: db_info} Database.Orders.write! details _ -> nil end :sell_no_require else {_, info} = List.first :ets.lookup :tradebots, instanceId %{user: user} = info user = Map.merge user, %{coins: coins} info = Map.merge info, %{user: user} info = if Map.has_key?(info, :get_golds) do cond do info.get_golds < coins -> Map.merge info, %{get_golds: coins} true -> info end else Map.merge info, %{get_golds: coins} end :ets.insert :tradebots, {instanceId, info} :ets.tab2file :tradebots, 'tradebots.ets' odrder_info = Database.Orders.read!(orderId) <--警告位置 case odrder_info do %{details: %{user: user} = db_info} = details -> user = Map.merge user, %{coins: coins} db_info = Map.merge db_info, %{user: user} details = Map.merge details, %{details: db_info} Database.Orders.write! details _ -> nil end :sell_yes_require end end
3 回复
  • jerry-chao 发表
    这个问题方便给出一个可以本地验证的代码示例?本地验证一种情况是不会出现你的错误提示的 ``` def test({:order, orderId}, switch) do case switch do true -> orderId false -> 1 end end ```
  • Harry_xu 发表
    [@jerry-chao](/users/771) 有时候会报这个警告,有时候就不会,很郁闷,去简单的匹配数据库查询的数据,我想也不会出现这个问题的
  • jerry-chao 发表
    [@Harry_xu](/users/822) 刚刚熟悉elixir,现在还只能简单的在本地验证一下