https://github.com/bala-bhargav created 
https://github.com/llvm/llvm-project/pull/178661

In clang-repl, expressions typed without a semicolon have their values printed 
to the console. This means the return value is actually being used, not 
discarded.
However, the compiler was emitting `[[nodiscard]]` warnings because it didn't 
know the REPL would use the value for printing.
This fix detects when we're in REPL mode evaluating a top-level expression and 
skips the warning, since printing the result counts as "using" it.


>From 6e20778fba945ba847fa0ff7f960b67c8ab54d56 Mon Sep 17 00:00:00 2001
From: bhargav <[email protected]>
Date: Thu, 29 Jan 2026 19:28:04 +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/lib/Sema/SemaStmt.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

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

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to