Issue |
147374
|
Summary |
[clang] Possibly incorrect -Winfinite-recursion emitted
|
Labels |
clang
|
Assignees |
|
Reporter |
baxter-huntington
|
The following minimal examples are used as a reference:
**Example A** (https://godbolt.org/z/5Mvbj7ePd):
```cpp
#include <string>
#include <iostream>
class Bar {
public:
explicit Bar(const std::string aValue) : mValue{aValue} {};
bool operator==(const std::string aValue) const {
std::cout << "bool operator==(const std::string aValue) const\n";
return aValue == mValue;
};
friend bool operator==(const std::string aLhs, Bar aRhs) { return aRhs == aLhs; }
private:
std::string mValue;
};
int main() {
auto other1 = Bar("Bar");
[[ maybe_unused ]] auto equal = other1 == "Bar";
}
```
**Example B** (https://godbolt.org/z/aM5f8odqa):
```cpp
#include <string>
#include <iostream>
template<typename T>
class Foo {
public:
explicit Foo(const T aValue) : mValue{aValue} {};
bool operator==(const T aValue) const {
std::cout << "bool operator==(const T aValue) const\n";
return aValue == mValue;
};
friend bool operator==(const T aLhs, Foo aRhs) { return aRhs == aLhs; }
private:
T mValue;
};
int main() {
auto thing1 = Foo<std::string>("Foo");
[[maybe_unused]] auto equal = thing1 == "Foo";
}
```
In Clang 21 (and the most recent trunk at the time of writing), **Example A** results in the following warning being emitted:
```
<source>:13:62: warning: all paths through this function will call itself [-Winfinite-recursion]
13 | friend bool operator==(const std::string aLhs, Bar aRhs) { return aRhs == aLhs; }
|
```
In older versions (e.g., Clang 20.1.0), no warning is emitted. `const`-qualifying `aRhs` resolves the warning in Clang 21. Is this a false positive? I'm not sure I understand what Clang deems to be infinitely recursive here.
Notably, when using a class template as in **Example B**, no warning is emitted.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs