Issue 63464
Summary [clang-tidy] readability-const-return-type conflicts with cert-dcl21-cpp for postfix operators
Labels new issue
Assignees
Reporter ecorm
    [`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
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to