Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>,
Endre =?utf-8?q?Fülöp?= <[email protected]>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/[email protected]>


================
@@ -0,0 +1,76 @@
+.. title:: clang-tidy - performance-expensive-value-or
+
+performance-expensive-value-or
+==============================
+
+Finds calls to ``value_or`` (and alternative spellings ``valueOr``,
+``ValueOr``) on optional types where the return type is expensive to copy.
+These methods return by value, which involves copying the contained value.
+``value()`` and ``operator*`` return references and can be used to avoid the
+copy when appropriate.
+
+The check is applied to types that are not trivially copyable or whose size
+exceeds a configurable threshold. It supports ``std::optional``,
+``boost::optional``, ``absl::optional``, and other optional-like types via
+configuration.
+
+Example:
+
+.. code-block:: c++
+
+    #include <optional>
+    #include <string>
+
+    void consumeRef(const std::string &);
+
+    void example(std::optional<std::string> opt,
+                 const std::string &fallback) {
+      // Warning: result binds to const reference, copy is avoidable.
+      const std::string &ref = opt.value_or(fallback);
+
+      // Warning: result passed to const reference parameter.
+      consumeRef(opt.value_or(fallback));
+
+      // Warning: const member called on temporary.
+      auto len = opt.value_or(fallback).size();
+
+      // No warning by default: caller takes ownership.
+      std::string val = opt.value_or("default");
+    }
+
+By default, the check only warns in reference-friendly contexts where the copy
+is clearly avoidable: binding to ``const T&``, passing to a ``const T&``
+parameter, or calling a const member function on the temporary. Contexts where
+the caller takes ownership (binding to a value, passing to a by-value
+parameter) are not flagged unless ``WarnOnOwnershipTaking`` is enabled.
----------------
EugeneZelenko wrote:

```suggestion
parameter) are not flagged unless :option:`WarnOnOwnershipTaking` is enabled.
```

Plus formatting.

https://github.com/llvm/llvm-project/pull/200166
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to