# redundant-loop-count

**Summary**: Redundant count before loop

**Category**: Bugs

**Avoid**

```
package policyallow if {    # redundant count and > comparison    count(input.user.roles) > 0    some role in input.user.roles    # .. do more with role ..}
```

**Prefer**

```
package policyallow if {    some role in input.user.roles    # .. do more with role ..}
```

## Rationale

A loop that iterates over an empty collection evaluates to nothing, and counting the collection before the loop to ensure it's not empty is therefore redundant.

## Exceptions

Note that this check is currently only performed on `some` loops, and not "ref-style" loops:

```
package policyallow if {    # this won't be flagged    count(input.user.roles) > 0    role := input.user.roles[_]    # .. do more with role ..}
```

Another good reason to [prefer some .. in for iteration](https://www.openpolicyagent.org/projects/regal/rules/style/prefer-some-in-iteration)!

### `every` iteration

Counting to ensure a non-empty collection is used before `every` loops may **not** be redundant, as `every` evaluates to `true` when an empty collection is passed.

```
package policyallow if {    # every would otherwise be `true` on empty input.user.roles    # so this may be valid, depending on the outcome you expect    count(input.user.roles) > 0    every role in input.user.roles {        # .. do more with each role ..    }}
```

If you want to have empty collections fail on `every` conditions, do make sure to use `count`!

## Configuration Options

This linter rule provides the following configuration options:

```
rules:  bugs:    redundant-loop-count:      # one of "error", "warning", "ignore"      level: error
```

## Related Resources

*   GitHub: [Source Code](https://github.com/open-policy-agent/regal/blob/main/bundle/regal/rules/bugs/redundant-loop-count/redundant_loop_count.rego)