Skip to content

False positive Redundant-clause warning on binary pattern #15455

@lukaszsamson

Description

@lukaszsamson

Existing issue

  • I have searched existing issues and could not find a duplicate.

Elixir and Erlang/OTP versions

Erlang/OTP 28 [erts-16.4.0.1] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]

Elixir 1.20.1 (compiled with Erlang/OTP 28)

Operating system

any

Current behavior

The following code emits a warning:

defmodule TRedundant do
              def f(x, <<x::binary>>), do: 1
              def f(a, b) when is_binary(a) and is_binary(b), do: 2
            end
warning: the following clause is redundant:

    def f(a, b) when is_binary(a) and is_binary(b)

it has type:

    binary(), binary()

previous clauses have already matched on the following types:

    binary(), binary()

└─ iex:3: TRedundant.f/2

Both clauses are reachable:

iex(2)> TRedundant.f("a", "a")
1
iex(3)> TRedundant.f("a", "b")
2

Expected behavior

No warning or a documented false positive. Can the type system encode that arguments are the same subtype of binary?

This code does not warn

defmodule TRedundant do
        def f(x, x) when is_binary(x), do: 1
        def f(a, b) when is_binary(a) and is_binary(b), do: 2
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions