[clang-tidy] Fix an unused-raii check crash on objective-c++.
authorHaojian Wu <hokein.wu@gmail.com>
Tue, 7 Jul 2020 11:35:22 +0000 (13:35 +0200)
committerHaojian Wu <hokein.wu@gmail.com>
Tue, 7 Jul 2020 11:36:20 +0000 (13:36 +0200)
Differential Revision: https://reviews.llvm.org/D83293

clang-tools-extra/clang-tidy/bugprone/UnusedRaiiCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-raii-crash.mm [new file with mode: 0644]

index 34a489e..70ce413 100644 (file)
@@ -27,9 +27,10 @@ void UnusedRaiiCheck::registerMatchers(MatchFinder *Finder) {
   // Look for temporaries that are constructed in-place and immediately
   // destroyed. Look for temporaries created by a functional cast but not for
   // those returned from a call.
-  auto BindTemp =
-      cxxBindTemporaryExpr(unless(has(ignoringParenImpCasts(callExpr()))))
-          .bind("temp");
+  auto BindTemp = cxxBindTemporaryExpr(
+                      unless(has(ignoringParenImpCasts(callExpr()))),
+                      unless(has(ignoringParenImpCasts(objcMessageExpr()))))
+                      .bind("temp");
   Finder->addMatcher(
       traverse(ast_type_traits::TK_AsIs,
                exprWithCleanups(
@@ -79,6 +80,7 @@ void UnusedRaiiCheck::check(const MatchFinder::MatchResult &Result) {
   auto Matches =
       match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context);
   const auto *TL = selectFirst<TypeLoc>("t", Matches);
+  assert(TL);
   D << FixItHint::CreateInsertion(
       Lexer::getLocForEndOfToken(TL->getEndLoc(), 0, *Result.SourceManager,
                                  getLangOpts()),
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-raii-crash.mm b/clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-raii-crash.mm
new file mode 100644 (file)
index 0000000..432fd53
--- /dev/null
@@ -0,0 +1,15 @@
+// RUN: clang-tidy %s -checks=-*,bugprone-unused-raii -- | count 0
+
+struct CxxClass {
+  ~CxxClass() {}
+};
+
+@interface ObjcClass {
+}
+- (CxxClass)set:(int)p;
+@end
+
+void test(ObjcClass *s) {
+  [s set:1]; // ok, no crash, no diagnostic emitted.
+  return;
+}