<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/66413>66413</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang-tidy] readability-function-cognitive-complexity penalizes assert
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-tidy,
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          softmoth
      </td>
    </tr>
</table>

<pre>
    Adding an `assert()` increases the cognitive complexity score by 3. I believe `assert` should not change the score at all. If anything, assertions reduce cognitive complexity.

## Example

Here is a sample function that triggers the warning (*Function 'triplets_with_sum' has cognitive complexity of 27 (threshold 25) [readability-function-cognitive-complexity]*):

```c
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    uint16_t a, b, c;
} triplet_t;

typedef struct
{
    triplet_t *triplets;
    size_t bufsz;
    uint16_t count;
} triplets_t;

triplets_t *
triplets_with_sum(uint16_t sum);
void
free_triplets(triplets_t *triplets);

void
free_triplets(triplets_t *triplets)
{
    if (triplets->count)
        free(triplets->triplets);
 free(triplets);
}

static triplets_t *
triplets_new(void)
{
    return calloc(1, sizeof(triplets_t));
}

static bool
triplets_append(triplets_t *ts, triplet_t t)
{
    if (++ts->count > ts->bufsz) {
 ts->bufsz *= 2;
        if (ts->bufsz == 0)
            ts->bufsz = 1;
        triplet_t *tmp = realloc(ts->triplets, ts->bufsz * sizeof(triplet_t));
        if (!tmp) {
            return false;
 }
        ts->triplets = tmp;
    }

 ts->triplets[ts->count - 1] = t;
    return true;
}

triplets_t *
triplets_with_sum(const uint16_t sum)
{
    triplets_t *result = triplets_new();
    if (!result)
        return result;

    for (uint16_t a = 1; a * 3 <= sum - 2; ++a) {
        for (uint16_t b = a + 1; a + b * 2 <= sum - 1; ++b) {
            assert(a > 0);
 assert(b > a);
            assert(a + b + b < sum); // Ensure c > b

            uint16_t c = sum - a - b;

            assert(c > b && c < sum);
            assert(a + b + c == sum);

            if (a * a + b * b == c * c) {
                if (!triplets_append(result, (triplet_t){ a, b, c }))
                    return result; // Give'm what we've got, I guess?
            }
        }
    }

    return result;
}
```

## Logs

```
jdoe jelly:~ [4567%1:148]% clang-tidy samp.c
539 warnings generated.
/home/jdoe/samp.c:57:1: warning: function 'triplets_with_sum' has cognitive complexity of 27 (threshold 25) [readability-function-cognitive-complexity]
triplets_with_sum(const uint16_t sum)
^
/home/jdoe/samp.c:60:5: note: +1, including nesting penalty of 0, nesting level increased to 1
    if (!result)
 ^
/home/jdoe/samp.c:63:5: note: +1, including nesting penalty of 0, nesting level increased to 1
    for (uint16_t a = 1; a * 3 <= sum - 2; ++a) {
    ^
/home/jdoe/samp.c:64:9: note: +2, including nesting penalty of 1, nesting level increased to 2
        for (uint16_t b = a + 1; a + b * 2 <= sum - 1; ++b) {
        ^
/home/jdoe/samp.c:65:13: note: +3, including nesting penalty of 2, nesting level increased to 3
 assert(a > 0);
            ^
/usr/include/assert.h:107:6: note: expanded from macro 'assert'
     ? __ASSERT_VOID_CAST (0) \
     ^
/home/jdoe/samp.c:66:13: note: +3, including nesting penalty of 2, nesting level increased to 3
 assert(b > a);
            ^
/usr/include/assert.h:107:6: note: expanded from macro 'assert'
     ? __ASSERT_VOID_CAST (0) \
     ^
/home/jdoe/samp.c:67:13: note: +3, including nesting penalty of 2, nesting level increased to 3
 assert(a + b + b < sum); // Ensure c > b
 ^
/usr/include/assert.h:107:6: note: expanded from macro 'assert'
 ? __ASSERT_VOID_CAST (0)                                           \
 ^
/home/jdoe/samp.c:71:13: note: +3, including nesting penalty of 2, nesting level increased to 3
            assert(c > b && c < sum);
 ^
/usr/include/assert.h:107:6: note: expanded from macro 'assert'
     ? __ASSERT_VOID_CAST (0) \
     ^
/home/jdoe/samp.c:71:26: note: +1
 assert(c > b && c < sum);
 ^
/home/jdoe/samp.c:72:13: note: +3, including nesting penalty of 2, nesting level increased to 3
            assert(a + b + c == sum);
 ^
/usr/include/assert.h:107:6: note: expanded from macro 'assert'
     ? __ASSERT_VOID_CAST (0) \
     ^
/home/jdoe/samp.c:74:13: note: +3, including nesting penalty of 2, nesting level increased to 3
            if (a * a + b * b == c * c) {
            ^
/home/jdoe/samp.c:75:17: note: +4, including nesting penalty of 3, nesting level increased to 4
                if (!triplets_append(result, (triplet_t){ a, b, c }))
 ^
Suppressed 538 warnings (538 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
```

## System information

Output of clangd --version:
clangd version 16.0.6
Features: linux
Platform: x86_64-pc-linux-gnu

Output of clang-tidy --version:
LLVM (http://llvm.org/):
  LLVM version 16.0.6
  Optimized build.

Operating system:
Arch Linux
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWU-P4joS_zTmUgIlNglw4ABNszvSrGa1_fZdkZMU4JGJke10N33Yz76y84ck0A0zu9NPE7UgcZV_rvpVuVJuuDFilyPOSbQk0WrAC7tXem7U1h6U3Q8SlZ3miywT-Q54DiQOuDGoLaFTQmckDkDkqUZu0IDdI6Rqlwsrnt3d4SjxVdgTmFRphOQEbARfIEEp8BlbWHEAZq8KmUGuLKR7nu_Qo5UTuQUu5Qi-bIHnJ7sX-Y7QBygnC5Ub0JgV6fXFRyRYkWBRfVJGKIPHV-7EbcnfUSMIAxyMl8G2yFMHDnbPLVgtdjvUpY8vXOeOD8_BYl0rEjqxWhwlWrN5EXa_McWB0AnsublOi9oCnTgUu9do9kpmQCNCZ0CipUae8URIYU_D2pRhgzI8o5BoRejCxYItOq7GQfmXNq6LPJVFhkDYQ8ndaE_Y4zWxsVmilPxILvIPpwv1kVSKpC32n_Z0xAy3YKwuUltJJsvyBgCgELkN440F7qKfuI-UsGWtuYKK_Y09j96H3EwEQhd1DBsQp2HEG24sJMXWvHUEjVGpKnJ7zRxzaU8jcOv1xs6ZM22w_eOsQXlWIivvthpx0xhMp13ks2DW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