[llvm-branch-commits] [clang] release/20.x: [Clang] Do not emit nodiscard warnings for the base expr of static member access (#131450) (PR #131474)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Mar 18 01:53:48 PDT 2025
https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/131474
>From e46c31e5a5d2aae2fcfc8d835681fcb58ea4c505 Mon Sep 17 00:00:00 2001
From: cor3ntin <corentinjabot at gmail.com>
Date: Sat, 15 Mar 2025 22:27:08 +0100
Subject: [PATCH 1/3] [Clang] Do not emit nodiscard warnings for the base expr
of static member access (#131450)
For an expression `nodiscard_function().static_member(), the nodiscard
warnings added by #120223, are not useful or actionable, and are
disruptive to some library implementations; we just remove them.
Fixes #131410
(cherry picked from commit 9a1e39062b2ab445f1f4424ecdc5ffb46e8cb9e0)
---
clang/include/clang/Sema/Sema.h | 5 -----
clang/lib/Sema/SemaExprMember.cpp | 1 -
clang/lib/Sema/SemaStmt.cpp | 4 ----
.../CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp | 10 ++++++----
clang/test/SemaCXX/ms-property.cpp | 2 +-
5 files changed, 7 insertions(+), 15 deletions(-)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index a30a7076ea5d4..6e2e5aaff2347 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -10671,11 +10671,6 @@ class Sema final : public SemaBase {
SourceLocation EndLoc);
void ActOnForEachDeclStmt(DeclGroupPtrTy Decl);
- /// DiagnoseDiscardedExprMarkedNodiscard - Given an expression that is
- /// semantically a discarded-value expression, diagnose if any [[nodiscard]]
- /// value has been discarded.
- void DiagnoseDiscardedExprMarkedNodiscard(const Expr *E);
-
/// DiagnoseUnusedExprResult - If the statement passed in is an expression
/// whose result is unused, warn.
void DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID);
diff --git a/clang/lib/Sema/SemaExprMember.cpp b/clang/lib/Sema/SemaExprMember.cpp
index d130e8b86bc56..adb8e3cc90c0c 100644
--- a/clang/lib/Sema/SemaExprMember.cpp
+++ b/clang/lib/Sema/SemaExprMember.cpp
@@ -1136,7 +1136,6 @@ Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
if (Converted.isInvalid())
return true;
BaseExpr = Converted.get();
- DiagnoseDiscardedExprMarkedNodiscard(BaseExpr);
return false;
};
auto ConvertBaseExprToGLValue = [&] {
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 947651d514b3b..b8b59793d6508 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -413,10 +413,6 @@ void DiagnoseUnused(Sema &S, const Expr *E, std::optional<unsigned> DiagID) {
}
} // namespace
-void Sema::DiagnoseDiscardedExprMarkedNodiscard(const Expr *E) {
- DiagnoseUnused(*this, E, std::nullopt);
-}
-
void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) {
if (const LabelStmt *Label = dyn_cast_if_present<LabelStmt>(S))
S = Label->getSubStmt();
diff --git a/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp b/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
index 18f4bd5e9c0fa..0012ab976baa5 100644
--- a/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
+++ b/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
@@ -164,19 +164,21 @@ struct X {
[[nodiscard]] X get_X();
// cxx11-warning at -1 {{use of the 'nodiscard' attribute is a C++17 extension}}
+[[nodiscard]] X* get_Ptr();
+// cxx11-warning at -1 {{use of the 'nodiscard' attribute is a C++17 extension}}
void f() {
+ get_X(); // expected-warning{{ignoring return value of function declared with 'nodiscard' attribute}}
+ (void) get_X();
(void) get_X().variant_member;
(void) get_X().anonymous_struct_member;
(void) get_X().data_member;
(void) get_X().static_data_member;
- // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
(void) get_X().unscoped_enum;
- // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
(void) get_X().scoped_enum;
- // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
(void) get_X().implicit_object_member_function();
(void) get_X().static_member_function();
- // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ (void) get_Ptr()->implicit_object_member_function();
+ (void) get_Ptr()->static_member_function();
#if __cplusplus >= 202302L
(void) get_X().explicit_object_member_function();
#endif
diff --git a/clang/test/SemaCXX/ms-property.cpp b/clang/test/SemaCXX/ms-property.cpp
index d5799a8a4d363..f1424b9cb12bc 100644
--- a/clang/test/SemaCXX/ms-property.cpp
+++ b/clang/test/SemaCXX/ms-property.cpp
@@ -2,6 +2,7 @@
// RUN: %clang_cc1 -triple=x86_64-pc-win32 -fms-compatibility -emit-pch -o %t -verify %s
// RUN: %clang_cc1 -triple=x86_64-pc-win32 -fms-compatibility -include-pch %t %s -ast-print -o - | FileCheck %s
// RUN: %clang_cc1 -fdeclspec -fsyntax-only -verify %s -std=c++23
+// expected-no-diagnostics
#ifndef HEADER
#define HEADER
@@ -103,7 +104,6 @@ struct X {
void f() {
(void) get_x().imp;
(void) get_x().st;
- // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
#if __cplusplus >= 202302L
(void) get_x().exp;
#endif
>From b8288519aa2bae786de4c8dcd1dcd2517f980905 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Mon, 17 Mar 2025 21:50:26 +0100
Subject: [PATCH 2/3] fix abi
---
clang/include/clang/Sema/Sema.h | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 6e2e5aaff2347..5d20d9f4fe6a3 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -10671,6 +10671,10 @@ class Sema final : public SemaBase {
SourceLocation EndLoc);
void ActOnForEachDeclStmt(DeclGroupPtrTy Decl);
+
+ // Unused, kept in Clang 20 for ABI stability.
+ void DiagnoseDiscardedExprMarkedNodiscard(const Expr *E) {};
+
/// DiagnoseUnusedExprResult - If the statement passed in is an expression
/// whose result is unused, warn.
void DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID);
>From 6c1fb121f4901ed6f6b60fb9ea68e4e94b043038 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 18 Mar 2025 09:53:12 +0100
Subject: [PATCH 3/3] fix abi (again)
---
clang/lib/Sema/SemaStmt.cpp | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index b8b59793d6508..947651d514b3b 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -413,6 +413,10 @@ void DiagnoseUnused(Sema &S, const Expr *E, std::optional<unsigned> DiagID) {
}
} // namespace
+void Sema::DiagnoseDiscardedExprMarkedNodiscard(const Expr *E) {
+ DiagnoseUnused(*this, E, std::nullopt);
+}
+
void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) {
if (const LabelStmt *Label = dyn_cast_if_present<LabelStmt>(S))
S = Label->getSubStmt();
More information about the llvm-branch-commits
mailing list