feat (FilterFilter and related): Check Map and Keyword versions too#1200
Open
s3cur3 wants to merge 1 commit intorrrene:masterfrom
Open
feat (FilterFilter and related): Check Map and Keyword versions too#1200s3cur3 wants to merge 1 commit intorrrene:masterfrom
s3cur3 wants to merge 1 commit intorrrene:masterfrom
Conversation
This expands the `Credo.Check.Refactor.FilterFilter` and related checks (`FilterReject`, `RejectFilter`, and `RejectReject`) to detect chains of the `Map` and `Keyword` versions of those functions in addition to `Enum`.
Thus, we can now detect all three of:
```elixir
ints
|> Enum.filter(&even?/1)
|> Enum.filter(fn val -> val > 0 end)
map
|> Map.filter(fn {key, _val} -> is_binary(key) end)
|> Map.filter(fn {_key, val} -> val > 0 end)
keyword
|> Keyword.filter(fn {key, _val} -> key in [:a, :b, :c] end)
|> Keyword.filter(fn {_key, val} -> val > 0 end)
```
s3cur3
commented
Jun 14, 2025
Comment on lines
+1
to
+15
| defmodule Credo.Check.Refactor.KeywordHelpers do | ||
| def traverse( | ||
| {{:., _, [{:__aliases__, meta, [:Keyword]}, second]}, _, | ||
| [{{:., _, [{:__aliases__, _, [:Keyword]}, first]}, _, _}, _]} = ast, | ||
| issues, | ||
| issue_meta, | ||
| message, | ||
| trigger, | ||
| first, | ||
| second, | ||
| module | ||
| ) do | ||
| new_issue = issue_for(issue_meta, meta[:line], message, trigger, module) | ||
| {ast, issues ++ List.wrap(new_issue)} | ||
| end |
Contributor
Author
There was a problem hiding this comment.
This module, as well as the MapHelpers below, is a straight copy and paste of EnumHelpers, with the module name changed.
My sense based on reading the rest of the codebase was that duplication was preferable to either
- metaprogramming to generate a KeywordHelpers and MapHelpers module, or
- making a single, more generic module that accept the target module (
:Enum,:Map, or:Keyword) as an argument
If I'm mistaken, though, I'm happy to do that refactor.
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.
This expands the
Credo.Check.Refactor.FilterFilterand related checks (FilterReject,RejectFilter, andRejectReject) to detect chains of theMapandKeywordversions of those functions in addition toEnum.Thus, we can now detect all three of: