================
@@ -204,23 +205,29 @@ static bool DiagnoseUnusedComparison(Sema &S, const Expr 
*E) {
   return true;
 }
 
-static bool DiagnoseNoDiscard(Sema &S, const WarnUnusedResultAttr *A,
-                              SourceLocation Loc, SourceRange R1,
-                              SourceRange R2, bool IsCtor) {
+static bool DiagnoseNoDiscard(Sema &S, const NamedDecl *OffendingDecl,
+                              const WarnUnusedResultAttr *A, SourceLocation 
Loc,
+                              SourceRange R1, SourceRange R2, bool IsCtor) {
   if (!A)
     return false;
   StringRef Msg = A->getMessage();
 
+  bool result;
   if (Msg.empty()) {
     if (IsCtor)
-      return S.Diag(Loc, diag::warn_unused_constructor) << A << R1 << R2;
-    return S.Diag(Loc, diag::warn_unused_result) << A << R1 << R2;
-  }
+      result = S.Diag(Loc, diag::warn_unused_constructor) << A << R1 << R2;
+    else
+      result = S.Diag(Loc, diag::warn_unused_result) << A << R1 << R2;
+  } else if (IsCtor)
+    result = S.Diag(Loc, diag::warn_unused_constructor_msg)
+             << A << Msg << R1 << R2;
+  else
+    result = S.Diag(Loc, diag::warn_unused_result_msg) << A << Msg << R1 << R2;
 
-  if (IsCtor)
-    return S.Diag(Loc, diag::warn_unused_constructor_msg) << A << Msg << R1
-                                                          << R2;
-  return S.Diag(Loc, diag::warn_unused_result_msg) << A << Msg << R1 << R2;
+  if (OffendingDecl)
----------------
erichkeane wrote:

Ah, woops!  I missed that you were doing a print of the name in the note.  I'd 
suggest doing a `select` and a dyn-cast kind of thing, such that the diagnostic 
doesn't print the name if one doesn't exist:

`S.Diag(...) << /*The select:*/isa<NamedDecl>(OffendingDecl) << /*the name: 
*/dyn_cast<NamedDecl>(OffendingDecl)`

It has to be a dyn-cast even though we know it won't be null, because we don't 
have a way of short-circuiting via 'select'.

https://github.com/llvm/llvm-project/pull/112289
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to