Skip to content

Conversation

@aman-ak-r
Copy link
Contributor

@aman-ak-r aman-ak-r commented Dec 1, 2025

This PR fixes the Credo 1.7.14 warnings introduced by the ExpensiveEmptyEnumCheck check.

Changes

Replaced expensive length/1 checks with Enum.empty?/1 in 4 test files:

  • test/elixir/test/replication_test.exs - Line 1674
  • test/elixir/lib/couch/dbtest.ex - Line 199
  • test/elixir/test/partition_size_limit_test.exs - Line 231
  • test/elixir/test/basics_test.exs - Line 67

Rationale

The Enum.empty?/1 function is more performant than length/1 for checking if a list is empty because:

  • Enum.empty?/1 only needs to check if there's at least one element
  • length/1 must traverse the entire list to count all elements

These changes are semantically equivalent:

  • length(list) > 0not Enum.empty?(list)
  • length(list) >= 1not Enum.empty?(list)

Testing

All changes are in test files and maintain the same logical behavior. The modifications follow Elixir best practices for performance optimization.

Fixes #5799Replace expensive length/1 checks with Enum.empty?/1 in test files to resolve Credo warnings introduced in version 1.7.14.

Changes:

  • test/elixir/test/replication_test.exs: Replace length(list) > 0
  • test/elixir/lib/couch/dbtest.ex: Replace length(list) > 0
  • test/elixir/test/partition_size_limit_test.exs: Replace length(list) > 0
  • test/elixir/test/basics_test.exs: Replace length(list) >= 1

These changes are semantically equivalent but more performant as Enum.empty?/1 only checks for the first element instead of traversing the entire list.

Fixes #5799

Overview

Testing recommendations

Related Issues or Pull Requests

Checklist

  • Code is written and works correctly
  • Changes are covered by tests
  • Any new configurable parameters are documented in rel/overlay/etc/default.ini
  • Documentation changes were made in the src/docs folder
  • Documentation changes were backported (separated PR) to affected branches

Replace expensive length/1 checks with Enum.empty?/1 in test files
to resolve Credo warnings introduced in version 1.7.14.

Changes:
- test/elixir/test/replication_test.exs: Replace length(list) > 0
- test/elixir/lib/couch/dbtest.ex: Replace length(list) > 0
- test/elixir/test/partition_size_limit_test.exs: Replace length(list) > 0
- test/elixir/test/basics_test.exs: Replace length(list) >= 1

These changes are semantically equivalent but more performant as
Enum.empty?/1 only checks for the first element instead of
traversing the entire list.

Fixes apache#5799
@aman-ak-r
Copy link
Contributor Author

hey @nickva can u please review this .

Copy link
Contributor

@nickva nickva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Thanks @aman-ak-r

@big-r81
Copy link
Contributor

big-r81 commented Dec 1, 2025

main:

$ make elixir-source-checks
* creating /Users/big-r/.mix/elixir/1-18-otp-28/rebar3
* creating /Users/big-r/.kiex/mix/archives/elixir-1.18.4/hex-2.3.1-otp-27
Resolving Hex dependencies...
Resolution completed in 0.315s
New:
  bunt 1.0.0
  credo 1.7.14
  excoveralls 0.18.5
  file_system 1.1.1
  httpotion 3.2.0 RETIRED!
    (deprecated) Not really maintained, please check out Tesla
  jason 1.4.4
  junit_formatter 3.4.0
  ssl_verify_fun 1.1.7
* Getting junit_formatter (Hex package)
* Getting httpotion (Hex package)
* Getting excoveralls (Hex package)
* Getting credo (Hex package)
* Getting bunt (Hex package)
* Getting file_system (Hex package)
* Getting jason (Hex package)
* Getting ssl_verify_fun (Hex package)

...

Checking 112 source files (this might take a while) ...

info: some checks were skipped because they're not compatible with
your version of Elixir (1.18.4).

You can deactivate these checks by adding them to the `:checks`/`:disabled` list in your config:

  checks: %{
    disabled: [
      {Credo.Check.Refactor.MapInto, []},	# requires Elixir < 1.8.0
      {Credo.Check.Warning.LazyLogging, []},	# requires Elixir < 1.7.0

      # ...
    ]
  }


  Warnings - please take a look                                                                                                                                 
┃ 
┃ [W] ↗ Using `length/1` is expensive, prefer `Enum.empty?/1`.
┃       test/elixir/test/replication_test.exs:1674:8 #(ReplicationTest.wait_for_repl)
┃ [W] ↗ Using `length/1` is expensive, prefer `Enum.empty?/1`.
┃       test/elixir/lib/couch/dbtest.ex:199:27 #(Couch.DBTest.wait_for_design_auth)
┃ [W] ↗ Using `length/1` is expensive, prefer `Enum.empty?/1`.
┃       test/elixir/test/partition_size_limit_test.exs:231:12 #(PartitionSizeLimitTest)
┃ [W] ↗ Using `length/1` is expensive, prefer `Enum.empty?/1`.
┃       test/elixir/test/basics_test.exs:67:12 #(BasicsTest)

Please report incorrect results: https://github.com/rrrene/credo/issues

Analysis took 7.9 seconds (0.4s to load, 7.4s running 53 checks on 112 files)
504 mods/funs, found 4 warnings.

Showing priority issues: ↑ ↗ →  (use `mix credo explain` to explain issues, `mix credo --help` for options).

PR:

$ make elixir-source-checks
* creating /Users/big-r/.mix/elixir/1-18-otp-28/rebar3
* creating /Users/big-r/.kiex/mix/archives/elixir-1.18.4/hex-2.3.1-otp-27
Resolving Hex dependencies...
Resolution completed in 0.045s
Unchanged:
  bunt 1.0.0
  credo 1.7.14
  excoveralls 0.18.5
  file_system 1.1.1
  httpotion 3.2.0 RETIRED!
    (deprecated) Not really maintained, please check out Tesla
  jason 1.4.4
  junit_formatter 3.4.0
  ssl_verify_fun 1.1.7
===> Analyzing applications...
===> Compiling b64url
===> Analyzing applications...
===> Compiling jwtf
===> Analyzing applications...
===> Compiling jiffy
===> Analyzing applications...
===> Compiling mochiweb
===> Analyzing applications...
===> Compiling ibrowse
===> Analyzing applications...
===> Compiling meck
==> httpotion
Compiling 1 file (.ex)
     warning: :ssl_verify_hostname.verify_fun/3 is undefined (module :ssl_verify_hostname is not available or is yet to be defined)
     │
 476 │   use HTTPotion.Base
     │   ~~~~~~~~~~~~~~~~~~
     │
     └─ lib/httpotion.ex:476: HTTPotion.process_arguments/3

Generated httpotion app
Checking 112 source files (this might take a while) ...

info: some checks were skipped because they're not compatible with
your version of Elixir (1.18.4).

You can deactivate these checks by adding them to the `:checks`/`:disabled` list in your config:

  checks: %{
    disabled: [
      {Credo.Check.Refactor.MapInto, []},	# requires Elixir < 1.8.0
      {Credo.Check.Warning.LazyLogging, []},	# requires Elixir < 1.7.0

      # ...
    ]
  }


Please report incorrect results: https://github.com/rrrene/credo/issues

Analysis took 10.1 seconds (4.3s to load, 5.8s running 53 checks on 112 files)
504 mods/funs, found no issues.

Showing priority issues: ↑ ↗ →  (use `mix credo explain` to explain issues, `mix credo --help` for options).

Thanks for the quick fix!

@nickva nickva merged commit b0de5fa into apache:main Dec 1, 2025
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Elixir credo 1.7.14 warnings

3 participants