llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

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

Open fogsong233 opened this issue 10 months ago • 3 comments

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

clang-repl> #include <vector>
clang-repl> auto v = std::vector<int>();
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> 

fogsong233 avatar Jan 29 '26 04:01 fogsong233

@fogsong233 can i work on this ??

bala-bhargav avatar Jan 29 '26 05:01 bala-bhargav

@fogsong233 can i work on this ??

Fine.

fogsong233 avatar Jan 29 '26 06:01 fogsong233

@fogsong233 pls find the PR #178661 for this issue.

bala-bhargav avatar Jan 29 '26 14:01 bala-bhargav