[libcxx-commits] [libcxx] [libc++][string] [libc++][span] Test [[nodiscard]] applied to `basic_string::iterator` (PR #202202)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jun 7 06:26:01 PDT 2026


https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/202202

>From 18e48c9c34e3df7a8db2e17aa06ee9939dbe6860 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Sun, 7 Jun 2026 16:16:24 +0300
Subject: [PATCH] [libc++][string] [libc++][span] Test [[nodiscard]] applied to
 `basic_string::iterator`

Adds test coverage.

`[[nodicard]]` applied in:
- https://github.com/llvm/llvm-project/pull/198489
- https://github.com/llvm/llvm-project/pull/198492

Towards #172124
---
 .../nodiscard.iterator.verify.cpp             | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 libcxx/test/libcxx/strings/basic.string/nodiscard.iterator.verify.cpp

diff --git a/libcxx/test/libcxx/strings/basic.string/nodiscard.iterator.verify.cpp b/libcxx/test/libcxx/strings/basic.string/nodiscard.iterator.verify.cpp
new file mode 100644
index 0000000000000..d9f004c7fffb1
--- /dev/null
+++ b/libcxx/test/libcxx/strings/basic.string/nodiscard.iterator.verify.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <string>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <string>
+
+#include "test_macros.h"
+
+void test() {
+  typedef std::string Container;
+  Container c;
+  Container::iterator it        = c.begin();
+  Container::const_iterator cit = c.cbegin();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  *it;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  *cit;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it[0];
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cit[0];
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it + 1;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cit + 1;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  1 + it;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  1 + cit;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it - 1;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cit - 1;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it - it;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cit - cit;
+}



More information about the libcxx-commits mailing list