diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index a4fb4d5a1f2ec..d8119543e9f8f 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1618,9 +1618,9 @@ QualType CallExpr::getCallReturnType(const ASTContext &Ctx) const { std::pair CallExpr::getUnusedResultAttr(const ASTContext &Ctx) const { // If the callee is marked nodiscard, return that attribute - const Decl *D = getCalleeDecl(); - if (const auto *A = D->getAttr()) - return {nullptr, A}; + if (const Decl *D = getCalleeDecl()) + if (const auto *A = D->getAttr()) + return {nullptr, A}; // If the return type is a struct, union, or enum that is marked nodiscard, // then return the return type attribute. diff --git a/clang/test/SemaCXX/warn-unused-result.cpp b/clang/test/SemaCXX/warn-unused-result.cpp index 682c500dc1d96..5105f347db8b5 100644 --- a/clang/test/SemaCXX/warn-unused-result.cpp +++ b/clang/test/SemaCXX/warn-unused-result.cpp @@ -355,3 +355,12 @@ void use2() { (void)G{"Hello"}; } } // namespace nodiscard_specialization + +namespace GH117975 { +// Test for a regression for ICE in CallExpr::getUnusedResultAttr +int f() { return 0; } +void id_print_name() { + (int) // expected-warning {{expression result unused}} + ((int(*)())f)(); +} +} // namespace GH117975