[clang] [clang][dataflow] `bugprone-unchecked-optional-access`: Add support for std::exchange. (PR #227526)

Jan Voung via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 1 09:02:15 PDT 2026


================
@@ -1168,6 +1168,99 @@ TEST_P(UncheckedOptionalAccessTest, SwapUnmodeledValueRightUnset) {
   )");
 }
 
+TEST_P(UncheckedOptionalAccessTest, StdExchange) {
+  ExpectDiagnosticsFor(
+      R"(
+    #include "unchecked_optional_access_test.h"
+
+    void target() {
+      $ns::$optional<int> opt1 = 3;
+
+      $ns::$optional<int> opt2 = std::exchange(opt1, $ns::nullopt);
+
+      opt1.value(); // [[unsafe]]
+
+      opt2.value();
----------------
jvoung wrote:

nit: could you reduce the spacing between lines a bit?

I think at least this `opt2.value();` could be next to the `opt1.value()`, if you are splitting into 3 blocks:

- arrange
- act
- assert

looks like just this and the one below

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


More information about the cfe-commits mailing list