Fix Elixir 1.20 type-checker warnings#2
Merged
Conversation
Two warnings surfaced in consuming applications under Elixir 1.20's new
type checker:
- The generated struct `channel/2` paired a `%module{id: id}` clause with
a `nil` fallback. Because each impl is `for:` a single struct, the
checker sees the struct clause as covering every value and flags the
fallback as redundant. Replaced with a single clause that checks for the
`:id` field via `Map.has_key?/2` — a plain call is opaque to the checker,
so the `nil` branch stays reachable. Behaviour is unchanged (structs with
an `:id`, including `nil`, derive a channel; others return `nil`). An
`is_map_key/2` guard does NOT work here: the checker narrows guards too.
- `Amplified.PubSub.Protocol.channel/2` used a deprecated default argument
(`namespace \\ nil`) in the protocol definition. Split into explicit
`channel/1` and `channel/2` declarations; the `Tuple` impl gains a
`channel/1` head to satisfy the new arity.
No behaviour change; all tests pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Elixir 1.20's new type checker surfaces two warnings in consuming applications. Both are fixed here with no behaviour change.
1. Redundant clause in generated struct
channel/2The
use Amplified.PubSubmacro generated:Each impl is
for:a single struct, so the checker sees%module{id: id}as covering every value of that struct and flags thenilfallback as redundant — one warning per consuming module (100+ in parasol).Fixed by collapsing to a single clause that checks for
:idviaMap.has_key?/2. A plain function call is opaque to the checker, so thenilbranch stays reachable. Behaviour is identical: structs with an:id(includingnil, which derives"thing:") get a channel; structs without one returnnil.2. Deprecated default argument in the protocol
Amplified.PubSub.Protocoldeclareddef channel(subject, namespace \\ nil). Default args indefprotocolare deprecated in 1.20 ("protocols can only define functions without implementation"). Split into explicitchannel/1andchannel/2; theTupleimpl gains achannel/1head.Testing
mix compile --force --all-warningsis clean.