credo icon indicating copy to clipboard operation
credo copied to clipboard

StrictLayout not honoring guard rules

Open eksperimental opened this issue 8 months ago • 5 comments

Credo is not emitting any suggestion

.credo.exs

          {Credo.Check.Readability.StrictModuleLayout,
           order: [
             :moduledoc,
             :public_guard,
             :private_guard
           ]}

module

  defguardp is_foo(term) when term == :foo

  defguard is_bar(term) when term == :bar

I have used Credo v1.7 and master.

A sample repo that reproduces this can be found here: https://github.com/eksperimental-debug/credo_debug/tree/strict-layout-guards

eksperimental avatar Jun 26 '25 14:06 eksperimental

Thanks for reporting this 😀 It should be fixed on master.

You can try this by setting the Credo dep to

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

Please report back if your issue is solved! 👍

rrrene avatar Jul 06 '25 18:07 rrrene

Thank for the fix. Unfortunately I am getting the following message in a module with no defp defined.

[R] ↘ public guard must appear before private_guard

The line refers to the last public guard defined in the module.

eksperimental avatar Jul 07 '25 19:07 eksperimental

I was not able to reproduce this with this module:

# order: [:moduledoc, :public_guard, :private_guard]
defmodule Test do
  @moduledoc ""

  defguard is_bar(term) when term == :bar

  defguard is_baz(term) when not is_foo(term) and term == :baz
end

Could you provide a snippet that reproduces this?

rrrene avatar Jul 10 '25 04:07 rrrene

Hi Rene. Thank you for looking into this.

I think it is mistaking a hidden guard (public guard with @doc false) for a private guard.

Here's a snippet that reproduces it.

defmodule Test do
  @moduledoc ""

  @doc false
  defguard is_bar(term) when term == :bar

  defguard is_baz(term) when not is_bar(term) and term == :baz
end

eksperimental avatar Jul 10 '25 04:07 eksperimental

Yes, undocumented guards, functions and macros are treated as "private". This was a choice by the original author of the check.

We could add a parameter to disable this behaviour, of course.

rrrene avatar Jul 12 '25 12:07 rrrene