Skip to content
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
34 changes: 11 additions & 23 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2805,32 +2805,20 @@ bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,

case CXXTemporaryObjectExprClass:
case CXXConstructExprClass: {
if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
const auto *WarnURAttr = Type->getAttr<WarnUnusedResultAttr>();
if (Type->hasAttr<WarnUnusedAttr>() ||
(WarnURAttr && WarnURAttr->IsCXX11NoDiscard())) {
WarnE = this;
Loc = getBeginLoc();
R1 = getSourceRange();
return true;
}
}

const auto *CE = cast<CXXConstructExpr>(this);
if (const CXXConstructorDecl *Ctor = CE->getConstructor()) {
const auto *WarnURAttr = Ctor->getAttr<WarnUnusedResultAttr>();
if (WarnURAttr && WarnURAttr->IsCXX11NoDiscard()) {
WarnE = this;
Loc = getBeginLoc();
R1 = getSourceRange();
const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl();

if (unsigned NumArgs = CE->getNumArgs())
R2 = SourceRange(CE->getArg(0)->getBeginLoc(),
CE->getArg(NumArgs - 1)->getEndLoc());
return true;
}
}
if ((Type && Type->hasAttr<WarnUnusedAttr>()) ||
CE->hasUnusedResultAttr(Ctx)) {
WarnE = this;
Loc = getBeginLoc();
R1 = getSourceRange();

if (unsigned NumArgs = CE->getNumArgs())
R2 = SourceRange(CE->getArg(0)->getBeginLoc(),
CE->getArg(NumArgs - 1)->getEndLoc());
return true;
}
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void usage() {
S(); // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard' attribute}}
S('A'); // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't let that S-Char go!}}
S(1);
S(2.2);
S(2.2); // expected-warning {{ignoring temporary created by a constructor declared with 'gnu::warn_unused_result' attribute}}
Y(); // expected-warning {{ignoring temporary of type 'Y' declared with 'nodiscard' attribute: Don't throw me away either!}}
S s;
ConvertTo{}; // expected-warning {{ignoring return value of type 'ConvertTo' declared with 'nodiscard' attribute: Don't throw me away!}}
Expand Down
4 changes: 2 additions & 2 deletions clang/test/SemaCXX/warn-unused-result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ void use() {

S<double>(2); // no warning
S<int>(2); // expected-warning {{ignoring temporary of type 'S<int>' declared with 'nodiscard'}}
S<const char>(2); // no warning (warn_unused_result does not diagnose constructor temporaries)
S<const char>(2); // expected-warning {{ignoring temporary of type 'S<const char>' declared with 'clang::warn_unused_result' attribute}}

// function should take precedence over type
obtain2(1.0); // expected-warning {{ignoring return value of function declared with 'nodiscard'}}
Expand All @@ -336,7 +336,7 @@ struct [[nodiscard]] G {
void use2() {
H{2}; // no warning
H(2.0); // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard'}}
H("Hello"); // no warning (warn_unused_result does not diagnose constructor temporaries)
H("Hello"); // expected-warning {{ignoring temporary created by a constructor declared with 'warn_unused_result' attribute}}

// no warning for explicit cast to void
(void)H(2);
Expand Down