https://github.com/bala-bhargav updated https://github.com/llvm/llvm-project/pull/178661
>From 190f6b5ff2e7ece086fbb6d35a734ad232d7a709 Mon Sep 17 00:00:00 2001 From: bhargav <[email protected]> Date: Thu, 29 Jan 2026 22:20:31 +0530 Subject: [PATCH] [clang-repl] Suppress [[nodiscard]] warnings for REPL printed expressions In clang-repl, expressions without semicolons have their values printed by the value printing mechanism. Since the result is used (for printing), we should not emit [[nodiscard]] warnings for these expressions. This fixes the false positive warning when evaluating expressions like v.size() in the REPL. [clang-repl] Suppress [[nodiscard]] warnings for printed expressions Fixes #178595 In clang-repl, expressions typed without a semicolon have their values printed. This means the return value is actually being used, not discarded. This fix suppresses [[nodiscard]] warnings for expressions that will be printed, while still emitting warnings for expressions with semicolons where the value is truly discarded# --- clang/lib/Parse/ParseDecl.cpp | 4 ++++ clang/lib/Sema/SemaStmt.cpp | 10 ++++++++++ clang/test/Interpreter/nodiscard.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 clang/test/Interpreter/nodiscard.cpp diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index df9e3878bffc0..59280f5ad8d29 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -5721,6 +5721,10 @@ Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() { Tok.getAnnotationValue() != nullptr) { ConsumeAnnotationToken(); TLSD->setSemiMissing(); + } else if (R.isUsable()) { + // Semicolon is present, so the value is being discarded. + // Re-diagnose [[nodiscard]] warnings that were suppressed during parsing. + Actions.DiagnoseUnusedExprResult(R.get(), diag::warn_unused_expr); } SmallVector<Decl *, 2> DeclsInGroup; diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 5ab10fdfc7b74..468bb4a544026 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -239,6 +239,16 @@ void DiagnoseUnused(Sema &S, const Expr *E, std::optional<unsigned> DiagID) { if (S.isUnevaluatedContext()) return; + // In incremental processing mode (REPL), expressions inside TopLevelStmtDecl + // without a semicolon will have their values printed by the value printing + // mechanism. The result is therefore "used" and we should not warn about + // [[nodiscard]] attributes. + if (S.PP.isIncrementalProcessingEnabled()) { + if (isa<TopLevelStmtDecl>(S.CurContext)) + return; + } + + SourceLocation ExprLoc = E->IgnoreParenImpCasts()->getExprLoc(); // In most cases, we don't want to warn if the expression is written in a // macro body, or if the macro comes from a system header. If the offending diff --git a/clang/test/Interpreter/nodiscard.cpp b/clang/test/Interpreter/nodiscard.cpp new file mode 100644 index 0000000000000..154e8ae39f618 --- /dev/null +++ b/clang/test/Interpreter/nodiscard.cpp @@ -0,0 +1,25 @@ +// REQUIRES: host-supports-jit +// RUN: cat %s | clang-repl 2>&1 | FileCheck %s + +// Test that [[nodiscard]] warnings are suppressed for REPL top-level +// expressions that will have their values printed (no semicolon), +// but are still emitted when the value is actually discarded (with semicolon). + +extern "C" int printf(const char*,...); + +[[nodiscard]] int getValue() { return 42; } + +// Negative test: Warning when value is discarded (with semicolon) +getValue(); +// CHECK: warning: ignoring return value of function declared with 'nodiscard' attribute + +// Positive test: No warning when expression value is printed (no semicolon) +getValue() +// CHECK: (int) 42 + +// Verify assignment doesn't warn +int x = getValue(); +printf("x = %d\n", x); +// CHECK: x = 42 + +%quit _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
