aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/call.cc12
-rw-r--r--gcc/testsuite/g++.dg/warn/Wdangling-reference5.C22
2 files changed, 33 insertions, 1 deletions
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index c7c7a12..2c0fa37 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -13467,7 +13467,17 @@ do_warn_dangling_reference (tree expr)
can be e.g.
const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
which doesn't dangle: std::min here returns an int. */
- || !TYPE_REF_OBJ_P (TREE_TYPE (TREE_TYPE (fndecl))))
+ || !TYPE_REF_OBJ_P (TREE_TYPE (TREE_TYPE (fndecl)))
+ /* Don't emit a false positive for:
+ std::vector<int> v = ...;
+ std::vector<int>::const_iterator it = v.begin();
+ const int &r = *it++;
+ because R refers to one of the int elements of V, not to
+ a temporary object. Member operator* may return a reference
+ but probably not to one of its arguments. */
+ || (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
+ && DECL_OVERLOADED_OPERATOR_P (fndecl)
+ && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF)))
return NULL_TREE;
/* Here we're looking to see if any of the arguments is a temporary
diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference5.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference5.C
new file mode 100644
index 0000000..59b5538
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference5.C
@@ -0,0 +1,22 @@
+// PR c++/107488
+// { dg-do compile }
+// { dg-options "-Wdangling-reference" }
+
+#include <vector>
+
+int
+do_sum (std::vector<int>& v)
+{
+ int sum = 0;
+
+ std::vector<int>::const_iterator it = v.begin();
+ while (it != v.end())
+ {
+ // R refers to one of the int elements of V, not to a temporary
+ // object, so no dangling reference here.
+ const int &r = *it++; // { dg-bogus "dangling reference" }
+ sum += r;
+ }
+
+ return sum;
+}