From f1271c8d087273af381e2ae5e447c2314c4a1f74 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Mon, 7 Nov 2016 20:34:16 +0000 Subject: [PATCH] Disallow StringRef assignment from temporary std::strings. Similar to r283798, this prevents accidentally referring to temporary storage that goes out of scope by the end of the statement: someStringRef = getStringByValue(); someStringRef = (Twine("-") + otherString).str(); Note that once again the constructor still has this problem: StringRef someStringRef = getStringByValue(); because once again we occasionally rely on this in calls: takesStringRef(getStringByValue()); takesStringRef(Twine("-") + otherString); Still, it's a step. llvm-svn: 286139 --- llvm/include/llvm/ADT/StringRef.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h index cc5a5795eeb2..97359dd2630b 100644 --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -226,6 +226,15 @@ namespace llvm { return Data[Index]; } + /// Disallow accidental assignment from a temporary std::string. + /// + /// The declaration here is extra complicated so that `stringRef = {}` + /// and `stringRef = "abc"` continue to select the move assignment operator. + template + typename std::enable_if::value, + StringRef>::type & + operator=(T &&Str) = delete; + /// @} /// @name Type Conversions /// @{ -- 2.34.1