]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: -Wdangling-reference not suppressed in template [PR109774]
authorMarek Polacek <polacek@redhat.com>
Tue, 16 May 2023 18:12:06 +0000 (14:12 -0400)
committerMarek Polacek <polacek@redhat.com>
Tue, 16 May 2023 18:12:57 +0000 (14:12 -0400)
In check_return_expr, we suppress the -Wdangling-reference warning when
we're sure it would be a false positive.  It wasn't working in a
template, though, because the suppress_warning call was never reached.

PR c++/109774

gcc/cp/ChangeLog:

* typeck.cc (check_return_expr): In a template, return only after
suppressing -Wdangling-reference.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wdangling-reference13.C: New test.

gcc/cp/typeck.cc
gcc/testsuite/g++.dg/warn/Wdangling-reference13.C [new file with mode: 0644]

index 53ac925a09241914ff4ed94dbc4fbb00f2dd6c55..c225c4e242325a4334d8b54dbd9484145e69d4c1 100644 (file)
@@ -11236,9 +11236,6 @@ check_return_expr (tree retval, bool *no_warning)
                         build_zero_cst (TREE_TYPE (retval)));
     }
 
-  if (processing_template_decl)
-    return saved_retval;
-
   /* A naive attempt to reduce the number of -Wdangling-reference false
      positives: if we know that this function can return a variable with
      static storage duration rather than one of its parameters, suppress
@@ -11250,6 +11247,9 @@ check_return_expr (tree retval, bool *no_warning)
       && TREE_STATIC (bare_retval))
     suppress_warning (current_function_decl, OPT_Wdangling_reference);
 
+  if (processing_template_decl)
+    return saved_retval;
+
   /* Actually copy the value returned into the appropriate location.  */
   if (retval && retval != result)
     {
diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference13.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference13.C
new file mode 100644 (file)
index 0000000..bc09fba
--- /dev/null
@@ -0,0 +1,23 @@
+// PR c++/109774
+// { dg-do compile }
+// { dg-options "-Wdangling-reference" }
+
+int y;
+
+template<typename T>
+int& get(const char& )
+{
+    return y;
+}
+
+int& get2(const char&)
+{
+    return y;
+}
+
+int stuff(void)
+{
+    const int &h = get<void>(0); // { dg-bogus "dangling reference" }
+    const int &k = get2(0); // { dg-bogus "dangling reference" }
+    return h+k;
+}