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,62 @@
+.. title:: clang-tidy - performance-expensive-value-or
+
+performance-expensive-value-or
+==============================
+
+Finds calls to ``value_or`` on optional types where the underlying value type
+is expensive to copy. While ``value()`` and ``operator*`` return references,
+``value_or`` always returns by value, which involves copying the contained
+value.
+
+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 example(std::optional<std::string> opt) {
+ // Warning: copies the std::string out of the optional.
+ auto val = opt.value_or("default");
+
+ // Alternatives that avoid the copy:
+ const std::string fallback = "default";
+ const auto &ref = opt.has_value() ? *opt : fallback;
+ }
+
+Options
+-------
+
+.. option:: SizeThreshold
+
+ The minimum size in bytes (exclusive) above which a trivially-copyable type
+ is considered expensive to copy. Types with ``sizeof(T) > SizeThreshold``
+ trigger the warning even if they are trivially copyable. Types at or below
+ this threshold only trigger if they are not trivially copyable.
+ Default is `8`.
----------------
nicovank wrote:
I often see 16 bytes as the expensive-to-copy threshold but I could be wrong.
https://github.com/llvm/llvm-project/pull/200166
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits