Skip to content

Commit e3f0ce3

Browse files
cor3ntintstellar
authored andcommitted
[Clang] Do not emit nodiscard warnings for the base expr of static member access (llvm#131450)
For an expression `nodiscard_function().static_member(), the nodiscard warnings added by llvm#120223, are not useful or actionable, and are disruptive to some library implementations; we just remove them. Fixes llvm#131410 (cherry picked from commit 9a1e390)
1 parent 0ceb4ef commit e3f0ce3

File tree

5 files changed

+7
-15
lines changed

5 files changed

+7
-15
lines changed

clang/include/clang/Sema/Sema.h

-5
Original file line numberDiff line numberDiff line change
@@ -10671,11 +10671,6 @@ class Sema final : public SemaBase {
1067110671
SourceLocation EndLoc);
1067210672
void ActOnForEachDeclStmt(DeclGroupPtrTy Decl);
1067310673

10674-
/// DiagnoseDiscardedExprMarkedNodiscard - Given an expression that is
10675-
/// semantically a discarded-value expression, diagnose if any [[nodiscard]]
10676-
/// value has been discarded.
10677-
void DiagnoseDiscardedExprMarkedNodiscard(const Expr *E);
10678-
1067910674
/// DiagnoseUnusedExprResult - If the statement passed in is an expression
1068010675
/// whose result is unused, warn.
1068110676
void DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID);

clang/lib/Sema/SemaExprMember.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,6 @@ Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
11361136
if (Converted.isInvalid())
11371137
return true;
11381138
BaseExpr = Converted.get();
1139-
DiagnoseDiscardedExprMarkedNodiscard(BaseExpr);
11401139
return false;
11411140
};
11421141
auto ConvertBaseExprToGLValue = [&] {

clang/lib/Sema/SemaStmt.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,6 @@ void DiagnoseUnused(Sema &S, const Expr *E, std::optional<unsigned> DiagID) {
413413
}
414414
} // namespace
415415

416-
void Sema::DiagnoseDiscardedExprMarkedNodiscard(const Expr *E) {
417-
DiagnoseUnused(*this, E, std::nullopt);
418-
}
419-
420416
void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) {
421417
if (const LabelStmt *Label = dyn_cast_if_present<LabelStmt>(S))
422418
S = Label->getSubStmt();

clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,21 @@ struct X {
164164

165165
[[nodiscard]] X get_X();
166166
// cxx11-warning@-1 {{use of the 'nodiscard' attribute is a C++17 extension}}
167+
[[nodiscard]] X* get_Ptr();
168+
// cxx11-warning@-1 {{use of the 'nodiscard' attribute is a C++17 extension}}
167169
void f() {
170+
get_X(); // expected-warning{{ignoring return value of function declared with 'nodiscard' attribute}}
171+
(void) get_X();
168172
(void) get_X().variant_member;
169173
(void) get_X().anonymous_struct_member;
170174
(void) get_X().data_member;
171175
(void) get_X().static_data_member;
172-
// expected-warning@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
173176
(void) get_X().unscoped_enum;
174-
// expected-warning@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
175177
(void) get_X().scoped_enum;
176-
// expected-warning@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
177178
(void) get_X().implicit_object_member_function();
178179
(void) get_X().static_member_function();
179-
// expected-warning@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
180+
(void) get_Ptr()->implicit_object_member_function();
181+
(void) get_Ptr()->static_member_function();
180182
#if __cplusplus >= 202302L
181183
(void) get_X().explicit_object_member_function();
182184
#endif

clang/test/SemaCXX/ms-property.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: %clang_cc1 -triple=x86_64-pc-win32 -fms-compatibility -emit-pch -o %t -verify %s
33
// RUN: %clang_cc1 -triple=x86_64-pc-win32 -fms-compatibility -include-pch %t %s -ast-print -o - | FileCheck %s
44
// RUN: %clang_cc1 -fdeclspec -fsyntax-only -verify %s -std=c++23
5+
// expected-no-diagnostics
56

67
#ifndef HEADER
78
#define HEADER
@@ -103,7 +104,6 @@ struct X {
103104
void f() {
104105
(void) get_x().imp;
105106
(void) get_x().st;
106-
// expected-warning@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
107107
#if __cplusplus >= 202302L
108108
(void) get_x().exp;
109109
#endif

0 commit comments

Comments
 (0)