https://bugs.llvm.org/show_bug.cgi?id=43124

            Bug ID: 43124
           Summary: clang++ gives false warnings about
                    self-assign-overloaded expressions
           Product: clang
           Version: 8.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangb...@nondot.org
          Reporter: j....@gmx.com
                CC: htmldevelo...@gmail.com, llvm-bugs@lists.llvm.org,
                    neeil...@live.com, richard-l...@metafoo.co.uk

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 https://stackoverflow.com/q/57645872 and
https://github.com/pybind/pybind11/issues/1893 for a real-world non-trivial and
non-arithmetic example of self-assignment expressions using overloaded
operators.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to