https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108165
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Romain Geissler from comment #3) > In my real life case B was std::string and used a "string literal" at call > site, and I guess using the implicit conversion from const char[] to > std::string is something that might happen in many call sites in big code > bases. And in general that is not safe. const std::string& f(const std::string& s) { return s; } const std::string& s = f(""); // BUG Warning here would be entirely correct. A temporary string is created from the string literal, then a reference to that temporary is returned, and bound to another reference. This is a dangling reference, and a serious bug, and exactly what the new warning is designed to diagnose. > Is it expected that -Wdangling-reference doesn't take into account the > definition of f ? Yes. > The problem of dangling reference in general needs > function definitions to be effective, otherwise I fear there might be quite > some false positives. Yes, but not in a case like f(const std::string&) above. The warning is correct in most real cases. The situation for the code in comment 0 is different though, there is no A temporary. The temporary is the second argument of type B, and that isn't returned. This seems like a bug in the implementation of the warning's heuristics, not a problem with the design of the warning.