[clang-repl] treat top-level expression evaluation as a "use" of nodiscard values
- 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
Assessment
This issue has not been assessed yet.