credo icon indicating copy to clipboard operation
credo copied to clipboard

Credo.Check.Refactor.MatchInCondition fails when using pipe operator

Open navinpeiris opened this issue 3 months ago • 5 comments

In credo version 1.7.14, Credo.Check.Refactor.MatchInCondition fails with the error message "Avoid matches in if conditions." when using pipe operator

Example:

# this fails with: "Avoid matches in `if` conditions."
if value = map |> Map.get(key) do
  value
else
  default
end

# this is fine
if value = Map.get(map, key) do
  value
else
  default
end

navinpeiris avatar Dec 01 '25 23:12 navinpeiris

Hi, this is sort of the intended behaviour (only allowing super simple assignments).

But I can see that the latest patch breaks existing patterns and we should at least provide an opt-out config param, so this does not trigger an issue.

Thx for reporting! 🙏

rrrene avatar Dec 02 '25 20:12 rrrene

Thanks for reporting this 😀 It should be fixed on master with a new config parameter called allow_operators.

You can try this by setting the Credo dep to

{:credo, github: "rrrene/credo"}

and configuring the check like this:

{Credo.Check.Refactor.MatchInCondition, allow_operators: true}

Please report back if your issue is solved! 👍

rrrene avatar Dec 04 '25 19:12 rrrene

Thanks @rrrene! Can confirm it works perfectly with the new setting 🫶

navinpeiris avatar Dec 07 '25 03:12 navinpeiris

Hello! I don't know if I should report it in this issue or open a new one, but I've got a similar case that I think should be valid:

if token = cookies[@remember_me_cookie] do

(this one is generated by Phoenix scaffolding 😄)

Let me know what you think!

Gladear avatar Dec 09 '25 07:12 Gladear

@Gladear an oversight on my part. Sorry! 😬

Should work on master!

rrrene avatar Dec 09 '25 08:12 rrrene