[clang] [clang] Generate note on declaration for nodiscard-related attributes (PR #112289)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 15 08:38:55 PDT 2024
================
@@ -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:
I can't imagine anything that would not be a `NamedDecl` that one could put the `nodiscard` on. Also though, WHY dose it have to be a `NamedDecl`? We only need the location for the diagnostic anyway, so having `getUnusedResultAttr` return just a `Decl` is probably sufficient.
https://github.com/llvm/llvm-project/pull/112289
More information about the cfe-commits
mailing list