llvm / llvm/llvm-project

[clang-repl] treat top-level expression evaluation as a "use" of nodiscard values

Open
#178,595 3 comments 0 reactions 0 assignees View on GitHub
clang-repl clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

When using clang-repl, we often evaluate expressions to inspect their return values directly. For functions decorated with [[nodiscard]] (like `std::vector::size()`), this triggers a -Wunused-result warning, which clutters the output.

In the context of a REPL, the act of printing the result to the console is logically equivalent to "consuming" the value. Therefore, treating it as an "ignored result" is a false positive in terms of user intent.

Simply muting -Wunused-result globally feels too blunt as it might hide legitimate issues in nested code blocks. A more refined approach would be to implement a check in Sema to identify top-level expressions whose results are bound to the REPL's printing mechanism and treat them as "used."
Example
```bash
clang-repl> #include
clang-repl> auto v = std::vector();
clang-repl> v.push_back(1);
clang-repl> v.size()
In file included from <<< inputs >>>:1:
input_line_4:1:1: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
1 | v.size()
| ^~~~~~~~
(unsigned long) 1
clang-repl>
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.