Skip to content

Commit

Permalink
[Clang] warn on discarded [[nodiscard]] function results after castin…
Browse files Browse the repository at this point in the history
…g in C (#104677)

Fixes #104391
  • Loading branch information
a-tarasyuk authored Aug 18, 2024
1 parent 10fe531 commit c4092d3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ Improvements to Clang's diagnostics

- Clang now diagnoses the use of ``main`` in an ``extern`` context as invalid according to [basic.start.main] p3. Fixes #GH101512.

- Clang now diagnoses when the result of a [[nodiscard]] function is discarded after being cast in C. Fixes #GH104391.

Improvements to Clang's time-trace
----------------------------------

Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) {
E = WarnExpr;
if (const auto *Cast = dyn_cast<CastExpr>(E))
if (Cast->getCastKind() == CK_NoOp ||
Cast->getCastKind() == CK_ConstructorConversion)
Cast->getCastKind() == CK_ConstructorConversion ||
Cast->getCastKind() == CK_IntegralCast)
E = Cast->getSubExpr()->IgnoreImpCasts();

if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Sema/c2x-nodiscard.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ void test_missiles(void) {
launch_missiles();
}

[[nodiscard]] int f3();

void GH104391() {
#define M (unsigned int) f3()
M; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}

0 comments on commit c4092d3

Please sign in to comment.