https://github.com/bala-bhargav updated https://github.com/llvm/llvm-project/pull/178661
>From 6f5493409ed22734fbb0b3710a46ff31d281b1a5 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 during parsing (since we don't know the semicolon status yet), then after parsing, re-enables it and diagnoses only for TopLevelStmtDecl expressions where the semicolon IS present (value is being discarded). This ensures [[nodiscard]] warnings still fire for genuinely discarded results like getValue(); All changes are confined to Interpreter.cpp, following the reviewer's guidance. Fixes #178595 --- clang/lib/Interpreter/Interpreter.cpp | 21 +++++++++++++++++++++ clang/test/Interpreter/nodiscard.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 46 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..8c8199e184a4e 100644 --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -40,6 +40,7 @@ #include "clang/Options/OptionUtils.h" #include "clang/Options/Options.h" #include "clang/Sema/Lookup.h" +#include "clang/Sema/Sema.h" #include "clang/Serialization/ObjectFilePCHContainerReader.h" #include "llvm/ExecutionEngine/JITSymbol.h" #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h" @@ -469,11 +470,31 @@ 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) return TuOrErr.takeError(); + // After parsing, re-enable [[nodiscard]] warnings and diagnose for + // top-level expressions where the semicolon IS present (value discarded). + // Expressions without semicolons have their values printed, so they are + // considered "used" and should not trigger [[nodiscard]] warnings. + getCompilerInstance()->getDiagnostics().setSeverity( + clang::diag::warn_unused_result, diag::Severity::Warning, + SourceLocation()); + for (Decl *D : (*TuOrErr)->decls()) { + if (auto *TLSD = llvm::dyn_cast<TopLevelStmtDecl>(D)) + if (!TLSD->isSemiMissing()) + getCompilerInstance()->getSema().DiagnoseUnusedExprResult( + TLSD->getStmt(), diag::warn_unused_result); + } + PartialTranslationUnit &LastPTU = IncrParser->RegisterPTU(*TuOrErr); return LastPTU; 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
