Index: clang-tidy/llvm/TwineLocalCheck.cpp =================================================================== --- clang-tidy/llvm/TwineLocalCheck.cpp +++ clang-tidy/llvm/TwineLocalCheck.cpp @@ -35,8 +35,11 @@ // of the initializer. const Expr *C = VD->getInit()->IgnoreImplicit(); - while (isa(C)) + while (isa(C)) { + if (cast(C)->getNumArgs() == 0) + break; C = cast(C)->getArg(0)->IgnoreParenImpCasts(); + } SourceRange TypeRange = VD->getTypeSourceInfo()->getTypeLoc().getSourceRange(); Index: test/clang-tidy/llvm-twine-local.cpp =================================================================== --- test/clang-tidy/llvm-twine-local.cpp +++ test/clang-tidy/llvm-twine-local.cpp @@ -5,6 +5,7 @@ public: Twine(const char *); Twine(int); + Twine(); Twine &operator+(const Twine &); }; } @@ -29,4 +30,35 @@ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: twine variables are prone to use-after-free bugs // CHECK-MESSAGES: note: FIX-IT applied suggested code changes // CHECK-FIXES: const char * Prefix = false ? "__INT_FAST" : "__UINT_FAST"; + + const Twine t2 = Twine(); +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs +// CHECK-MESSAGES: note: FIX-IT applied suggested code changes +// CHECK-FIXES: std::string t2 = (Twine()).str(); + foo(Twine() + "b"); + + const Twine t3 = Twine(42); +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs +// CHECK-MESSAGES: note: FIX-IT applied suggested code changes +// CHECK-FIXES: std::string t3 = (Twine(42)).str(); + + const Twine t4 = Twine(42) + "b"; +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs +// CHECK-MESSAGES: note: FIX-IT applied suggested code changes +// CHECK-FIXES: std::string t4 = (Twine(42) + "b").str(); + + const Twine t5 = Twine() + "b"; +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs +// CHECK-MESSAGES: note: FIX-IT applied suggested code changes +// CHECK-FIXES: std::string t5 = (Twine() + "b").str(); + + const Twine t6 = true ? Twine() : Twine(42); +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs +// CHECK-MESSAGES: note: FIX-IT applied suggested code changes +// CHECK-FIXES: std::string t6 = (true ? Twine() : Twine(42)).str(); + + const Twine t7 = false ? Twine() : Twine("b"); +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs +// CHECK-MESSAGES: note: FIX-IT applied suggested code changes +// CHECK-FIXES: std::string t7 = (false ? Twine() : Twine("b")).str(); }