https://github.com/bala-bhargav updated https://github.com/llvm/llvm-project/pull/178661
>From e0a73df5b689ba2a22d1bb866817976d9f1da528 Mon Sep 17 00:00:00 2001 From: bhargav <[email protected]> Date: Thu, 12 Feb 2026 15:38:32 +0530 Subject: [PATCH] [clang-repl] Suppress [[nodiscard]] warnings for REPL printed expressions In clang-repl, expressions typed without a semicolon 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 suppresses warn_unused_result alongside the existing warn_unused_expr suppression in Interpreter::Parse(), following the same pattern already established for unused expression warnings. Fixes #178595 --- clang/lib/Interpreter/Interpreter.cpp | 6 ++++++ clang/test/Interpreter/nodiscard.cpp | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 clang/test/Interpreter/nodiscard.cpp diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp index 9c94cfa5ee381..3eef722541eb8 100644 --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -469,6 +469,12 @@ Interpreter::Parse(llvm::StringRef Code) { // printing could cause it. getCompilerInstance()->getDiagnostics().setSeverity( clang::diag::warn_unused_expr, diag::Severity::Ignored, SourceLocation()); + // Suppress [[nodiscard]] warnings during parsing since we don't know yet + // if the expression has a missing semicolon (value printed) or not. + // If the value is printed, it's considered "used" so no warning is needed. + getCompilerInstance()->getDiagnostics().setSeverity( + clang::diag::warn_unused_result, diag::Severity::Ignored, + SourceLocation()); llvm::Expected<TranslationUnitDecl *> TuOrErr = IncrParser->Parse(Code); if (!TuOrErr) diff --git a/clang/test/Interpreter/nodiscard.cpp b/clang/test/Interpreter/nodiscard.cpp new file mode 100644 index 0000000000000..bf307ee81835e --- /dev/null +++ b/clang/test/Interpreter/nodiscard.cpp @@ -0,0 +1,20 @@ +// 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). + +extern "C" int printf(const char*,...); + +[[nodiscard]] int getValue() { return 42; } + +// 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
