llvm / llvm/llvm-project

[clang-tidy] false-negative in bugprone-return-const-ref-from-parameter when overload for rvalues exists with different constness

Open
#160,650 1 comment 0 reactions 0 assignees View on GitHub
clang-tidy false-negative
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This is a followup bug of #90274.

The following code snippet should generate `bugprone-return-const-ref-from-parameter` error but clang-tidy doesn't:

```cpp
#include

struct Foo {
const std::string &f(const std::string &a) const {
return a;
}
void f(std::string&&) = delete;
};
```

The following bug-prone usage compiles:

```cpp
int main() {
const Foo foo;
auto& a = foo.f(std::string("12"));
// use a...
return 0;
}
```

The proper way to avoid this clang-tidy warning is to overload with the same constness:

```cpp
#include

struct Foo {
const std::string &f(const std::string &a) const {
return a;
}
void f(std::string&&) const = delete;
};
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.