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

    <tr>
        <th>Summary</th>
        <td>
            [clang-tidy] readability-const-return-type conflicts with cert-dcl21-cpp for postfix operators
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    [`cert-dcl21-cpp`](https://clang.llvm.org/extra/clang-tidy/checks/cert/dcl21-cpp.html) wants postfix increment/decrement operators to return a `const` value, because otherwise:

> any modifications made to the resulting object from calling operator++(int) would be modifying a temporary object

But [`readability-const-return-type`](https://clang.llvm.org/extra/clang-tidy/checks/readability/const-return-type.html) prohibits returning `const`value types, because

> Such use of _const_ is usually superfluous, and can prevent valuable compiler optimizations.

The `readability-const-return-type` check should be relaxed for postfix increment/decrement operators, because avoiding bugs should take precedence over a minor readability warning.

Example program:

```c++
struct Foo
{
    int value = 0;
};

struct Bar
{
    // warning: overloaded 'operator++' returns a non-constant object
    // instead of a constant object type [cert-dcl21-cpp]
    Bar operator++(int)
    {
        auto temp = *this;
        ++foo.value;
        return temp;
    }

 Foo foo;
};

struct Baz
{
    // warning: return type 'const Baz' is 'const'-qualified at the top
    // level, which may reduce code readability without improving const
    // correctness [readability-const-return-type]
    const Baz operator++(int)
    {
 auto temp = *this;
        ++foo.value;
        return temp;
 }

    Foo foo;
};

int main()
{
    Bar bar;
 bar++;
    Baz baz;
    baz++;
    return 0;
}
```

Godbolt demo: https://godbolt.org/z/E64zeKvzr
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vk2T4jYT_jXi0gVlZLDhwGF4Gd5Djsl9qy21sbKy5egDBn59SrJhDJtsTaqSLWrHllr98TxPt4zOqVNHtGPrPVsfZhh8Y-yOhLHtrDLymjaKTJD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