Skip to content

[Clang] Do not emit nodiscard warnings for the base expr of static member access #131450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -10755,11 +10755,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);
Expand Down
1 change: 0 additions & 1 deletion clang/lib/Sema/SemaExprMember.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,6 @@ Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
if (Converted.isInvalid())
return true;
BaseExpr = Converted.get();
DiagnoseDiscardedExprMarkedNodiscard(BaseExpr);
return false;
};
auto ConvertBaseExprToGLValue = [&] {
Expand Down
4 changes: 0 additions & 4 deletions clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 6 additions & 4 deletions clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,21 @@ struct X {

[[nodiscard]] X get_X();
// cxx11-warning@-1 {{use of the 'nodiscard' attribute is a C++17 extension}}
[[nodiscard]] X* get_Ptr();
// cxx11-warning@-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@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
(void) get_X().unscoped_enum;
// expected-warning@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
(void) get_X().scoped_enum;
// expected-warning@-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@-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
Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaCXX/ms-property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -103,7 +104,6 @@ struct X {
void f() {
(void) get_x().imp;
(void) get_x().st;
// expected-warning@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
#if __cplusplus >= 202302L
(void) get_x().exp;
#endif
Expand Down