[libcxx-commits] [libcxx] [libc++][string_view] Applied `[[nodiscard]]` (PR #169010)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Nov 24 01:25:54 PST 2025


================
@@ -12,12 +12,147 @@
 
 #include <string_view>
 
+#include "type_algorithms.h"
 #include "test_macros.h"
 
-void test() {
-  std::string_view string_view;
-  string_view.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+void test_members() {
+  std::string_view sv;
+
+  sv.begin();   // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.end();     // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.cbegin();  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.cend();    // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.rbegin();  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.rend();    // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.crbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.crend();   // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  sv.size();     // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.length();   // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.max_size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  sv.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  sv[0];    // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.at(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  sv.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.back();  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.data();  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  sv.substr(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
 #if TEST_STD_VER >= 26
-  string_view.subview(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.subview(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.compare(sv);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.compare(0, 0, sv);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.compare(0, 0, sv, 0, 0);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.compare("");
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.compare(0, 0, "");
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.compare(0, 0, "", 0);
+
+  sv.find(sv);       // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find(' ');      // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find("", 0, 0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find("", 0);    // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  sv.rfind(sv);       // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.rfind(' ');      // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.rfind("", 0, 0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.rfind("", 0);    // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_first_of(sv);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_first_of(' ');
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_first_of("", 0, 0);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_first_of("", 0);
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_last_of(sv);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_last_of(' ');
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_last_of("", 0, 0);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_last_of("", 0);
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_first_not_of(sv);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_first_not_of(' ');
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_first_not_of("", 0, 0);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_first_not_of("", 0);
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_last_not_of(sv);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_last_not_of(' ');
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_last_not_of("", 0, 0);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.find_last_not_of("", 0);
+
+#if TEST_STD_VER >= 20
+  sv.starts_with(sv);  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.starts_with(' '); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.starts_with("");  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  sv.ends_with(sv);  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.ends_with(' '); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.ends_with("");  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
+
+#if TEST_STD_VER >= 23
+  sv.contains(sv);  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.contains(' '); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sv.contains("");  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
+}
+
+class TestHash {
+public:
+  template <typename CharT>
+  void operator()() {
+    using StringViewT = std::basic_string_view<CharT>;
+    std::hash<StringViewT> hash;
+    // expected-warning at +1 4-5 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    hash(StringViewT{});
+  }
+};
+
----------------
philnik777 wrote:

I don't think it's necessary to test for every character type here, since we don't have individual functions for them. The point of these tests is that we don't accidentally drop `[[nodiscard]]`s, so one should be enough.

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


More information about the libcxx-commits mailing list