[libcxx-commits] [libcxx] [libc++][char_traits] Applied `[[nodiscard]]` (PR #172244)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Sun Dec 14 20:21:19 PST 2025


================
@@ -0,0 +1,162 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Check that functions are marked [[nodiscard]]
+
+#include <string>
+
+#include "test_macros.h"
+
+void test_char() {
+  typedef char char_t;
+  typedef std::char_traits<char_t> traits;
+  typedef typename traits::int_type int_t;
+
+  const char_t buf[1] = {};
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  traits::eq(char_t(), char_t());
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  traits::lt(char_t(), char_t());
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  traits::compare(buf, buf, 0);
+  traits::length(buf); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  traits::find(buf, 0, char_t());
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  traits::not_eof(char_t());
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  traits::to_char_type(int_t());
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  traits::to_int_type(char_t());
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  traits::eq_int_type(int_t(), int_t());
+  traits::eof(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+}
+
+#ifndef TEST_HAS_NO_CHAR8_T
+void test_char8_t() {
----------------
frederick-vs-ja wrote:

Note for review: I attempted to use a function template to handle five character types. However, the count of expected warnings varies in different modes, and it's infeasible to write the different counts in comments. As a result, I chose to repeat the function body 5 times.

https://github.com/llvm/llvm-project/pull/172244


More information about the libcxx-commits mailing list