Skip to content

Commit c4092d3

Browse files
authored
[Clang] warn on discarded [[nodiscard]] function results after casting in C (#104677)
Fixes #104391
1 parent 10fe531 commit c4092d3

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ Improvements to Clang's diagnostics
218218

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

221+
- Clang now diagnoses when the result of a [[nodiscard]] function is discarded after being cast in C. Fixes #GH104391.
222+
221223
Improvements to Clang's time-trace
222224
----------------------------------
223225

clang/lib/Sema/SemaStmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) {
281281
E = WarnExpr;
282282
if (const auto *Cast = dyn_cast<CastExpr>(E))
283283
if (Cast->getCastKind() == CK_NoOp ||
284-
Cast->getCastKind() == CK_ConstructorConversion)
284+
Cast->getCastKind() == CK_ConstructorConversion ||
285+
Cast->getCastKind() == CK_IntegralCast)
285286
E = Cast->getSubExpr()->IgnoreImpCasts();
286287

287288
if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {

clang/test/Sema/c2x-nodiscard.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ void test_missiles(void) {
5454
launch_missiles();
5555
}
5656

57+
[[nodiscard]] int f3();
58+
59+
void GH104391() {
60+
#define M (unsigned int) f3()
61+
M; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
62+
}

0 commit comments

Comments
 (0)