diff options
author | Marek Polacek <polacek@redhat.com> | 2022-11-01 17:05:52 -0400 |
---|---|---|
committer | Thomas Schwinge <thomas@codesourcery.com> | 2022-12-14 14:29:44 +0100 |
commit | 5c2e6c93d919b939ba7eca2100fa35cc444f4b8d (patch) | |
tree | beec559689144dfe4f023924ae1dd3a37b19a580 /gcc/rust/backend | |
parent | 3d6dbb6fc5d1d14bf36a879b6f4a00ae91090a63 (diff) | |
download | gcc-5c2e6c93d919b939ba7eca2100fa35cc444f4b8d.zip gcc-5c2e6c93d919b939ba7eca2100fa35cc444f4b8d.tar.gz gcc-5c2e6c93d919b939ba7eca2100fa35cc444f4b8d.tar.bz2 |
c++: Quash -Wdangling-reference for member operator* [PR107488]
-Wdangling-reference complains here:
std::vector<int> v = ...;
std::vector<int>::const_iterator it = v.begin();
while (it != v.end()) {
const int &r = *it++; // warning
}
because it sees a call to
__gnu_cxx::__normal_iterator<const int*, std::vector<int> >::operator*
which returns a reference and its argument is a TARGET_EXPR representing
the result of
__gnu_cxx::__normal_iterator<const int*, std::vector<int> >::operator++
But 'r' above refers to one of the int elements of the vector 'v', not
to a temporary object. Therefore the warning is a false positive.
I suppose code like the above is relatively common (the warning broke
cppunit-1.15.1 and a few other projects), so presumably it makes sense
to suppress the warning when it comes to member operator*. In this case
it's defined as
reference
operator*() const _GLIBCXX_NOEXCEPT
{ return *_M_current; }
and I'm guessing a lot of member operator* are like that, at least when
it comes to iterators. I've looked at _Fwd_list_iterator,
_Fwd_list_const_iterator, __shared_ptr_access, _Deque_iterator,
istream_iterator, etc, and they're all like that, so adding #pragmas
would be quite tedious. :/
PR c++/107488
gcc/cp/ChangeLog:
* call.cc (do_warn_dangling_reference): Quash -Wdangling-reference
for member operator*.
gcc/testsuite/ChangeLog:
* g++.dg/warn/Wdangling-reference5.C: New test.
(cherry picked from commit 32a06ce38a38bf37db468f0e6c83520fcc221534)
Diffstat (limited to 'gcc/rust/backend')
0 files changed, 0 insertions, 0 deletions