[clang] [clang-repl] Suppress [[nodiscard]] warnings for REPL printed express (PR #178661)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 29 06:03:45 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: None (bala-bhargav)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/178661.diff
1 Files Affected:
- (modified) clang/lib/Sema/SemaStmt.cpp (+10)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/178661
More information about the cfe-commits
mailing list