exercism / elixir-analyzer

GNU Affero General Public License v3.0
30 stars 32 forks source link

FileSniffer analyzer spurious comments? #353

Closed rongcuid closed 1 year ago

rongcuid commented 1 year ago

The following is my solution:

defmodule FileSniffer do
  def type_from_extension("exe"), do: "application/octet-stream"
  def type_from_extension("bmp"), do: "image/bmp"
  def type_from_extension("png"), do: "image/png"
  def type_from_extension("jpg"), do: "image/jpg"
  def type_from_extension("gif"), do: "image/gif"
  def type_from_extension(_), do: nil

  def type_from_binary(<<0x42::8,0x4D::8,_::binary>>), do: "image/bmp"
  def type_from_binary(<<0x47::8,0x49::8,0x46::8,_::binary>>), do: "image/gif"
  def type_from_binary(<<0x89::8,0x50::8,0x4E::8,0x47::8,0x0D::8,0x0A::8,0x1A::8,0x0A::8,_::binary>>), do: "image/png"
  def type_from_binary(<<0xFF::8,0xD8::8,0xFF::8,_::binary>>), do: "image/jpg"
  def type_from_binary(<<0x7F::8,0x45::8,0x4C::8,0x46::8,_::binary>>), do: "application/octet-stream"
  def type_from_binary(_), do: nil

  def verify(file_binary, extension) do
    ty = type_from_extension(extension)
    if ty == type_from_binary(file_binary) do
      {:ok, ty}
    else
      {:error, "Warning, file format and file extension do not match."}
    end
  end
end

And I get the following comment:

Even though there could be other valid ways to solve this exercise, for the sake of mastering bitstrings please match the first few bytes of the argument of type_from_binary/1 with the special form <<>>.

I certainly used only the special form, except for the last clause.

angelikatyborska commented 1 year ago

Thanks for reporting! I believe the analyzer is fooled by the usage of bit size (0x42::8 instead of 0x42), which is definitely valid code, so it's an analyzer bug 😞. I'll open a PR.