[libcxx-commits] [libcxx] [libc++][vector] Test `[[nodiscard]]` applied to `vector::iterator` (PR #202262)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jun 7 23:18:35 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Hristo Hristov (H-G-Hristov)
<details>
<summary>Changes</summary>
Adds test coverage.
[[nodicard]] applied in:
- [libc++] Applied [[nodiscard]] to optional::iterator #<!-- -->198489
- [libc++] Applied [[nodiscard]] to array::iterator #<!-- -->198492
Towards #<!-- -->172124
---
Full diff: https://github.com/llvm/llvm-project/pull/202262.diff
1 Files Affected:
- (added) libcxx/test/libcxx/containers/sequences/vector/nodiscard.iterator.verify.cpp (+52)
``````````diff
diff --git a/libcxx/test/libcxx/containers/sequences/vector/nodiscard.iterator.verify.cpp b/libcxx/test/libcxx/containers/sequences/vector/nodiscard.iterator.verify.cpp
new file mode 100644
index 0000000000000..8570a920270d9
--- /dev/null
+++ b/libcxx/test/libcxx/containers/sequences/vector/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
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <vector>
+
+#include "test_macros.h"
+
+void test() {
+ typedef std::vector<int> 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;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/202262
More information about the libcxx-commits
mailing list