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:
#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.
Options¶
- SizeThreshold¶
The minimum size in bytes (exclusive) above which a trivially-copyable type is considered expensive to copy. Types with
sizeof(T) > SizeThresholdtrigger the warning even if they are trivially copyable. Types at or below this threshold only trigger if they are not trivially copyable. Default is16.
- OptionalTypes¶
Semicolon-separated list of regular expressions matching fully-qualified names of optional-like class templates to check. The check matches calls to
value_oron specializations of these templates. Default is::std::optional;::absl::optional;::boost::optional.
- WarnOnOwnershipTaking¶
When
true, the check also warns when the result ofvalue_oris used in an ownership-taking context (e.g., initializing a value variable or passing to a by-value parameter). Default isfalse.