<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - clang++ gives false warnings about self-assign-overloaded expressions"
href="https://bugs.llvm.org/show_bug.cgi?id=43124">43124</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>clang++ gives false warnings about self-assign-overloaded expressions
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>8.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>-New Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>j.l.k@gmx.com
</td>
</tr>
<tr>
<th>CC</th>
<td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Clang 8.0.1 produces false warnings for expressions like `a -= a` and `a /= a`,
even when `a` is not a number and the overloaded operators don't do any
arithmetic operations, let alone when their return type is completely different
from `decltype(a)`.
Consider this code:
```
class Vector {
public:
Vector& operator+=(const Vector &v) { return *this; }
Vector& operator-=(const Vector &v) { return *this; }
Vector& operator*=(const Vector &v) { return *this; }
Vector& operator/=(const Vector &v) { return *this; }
};
int main()
{
Vector v;
v += v;
v -= v;
v *= v;
v /= v;
}
```
When compiled with clang++ 8.0.1, I get the following warnings:
```
$ clang++ -Wall example2.cpp -o example2
example2.cpp:13:7: warning: explicitly assigning value of variable of type
'Vector' to
itself [-Wself-assign-overloaded]
v -= v;
~ ^ ~
example2.cpp:15:7: warning: explicitly assigning value of variable of type
'Vector' to
itself [-Wself-assign-overloaded]
v /= v;
~ ^ ~
2 warnings generated.
```
See also <a href="https://stackoverflow.com/q/57645872">https://stackoverflow.com/q/57645872</a> and
<a href="https://github.com/pybind/pybind11/issues/1893">https://github.com/pybind/pybind11/issues/1893</a> for a real-world non-trivial and
non-arithmetic example of self-assignment expressions using overloaded
operators.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>