brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · 00581b1 Raw
81 lines · plain
1.. title:: clang-tidy - performance-unnecessary-copy-initialization2 3performance-unnecessary-copy-initialization4===========================================5 6Finds local variable declarations that are initialized using the copy7constructor of a non-trivially-copyable type but it would suffice to obtain a8const reference.9 10The check is only applied if it is safe to replace the copy by a const11reference. This is the case when the variable is const qualified or when it is12only used as a const, i.e. only const methods or operators are invoked on it,13or it is used as const reference or value argument in constructors or function14calls.15 16Example:17 18.. code-block:: c++19 20  const string& constReference();21  void Function() {22    // The warning will suggest making this a const reference.23    const string UnnecessaryCopy = constReference();24  }25 26  struct Foo {27    const string& name() const;28  };29  void Function(const Foo& foo) {30    // The warning will suggest making this a const reference.31    string UnnecessaryCopy1 = foo.name();32    UnnecessaryCopy1.find("bar");33 34    // The warning will suggest making this a const reference.35    string UnnecessaryCopy2 = UnnecessaryCopy1;36    UnnecessaryCopy2.find("bar");37  }38 39Options40-------41 42.. option:: AllowedTypes43 44   A semicolon-separated list of names of types allowed to be initialized by45   copying. Regular expressions are accepted, e.g. ``[Rr]ef(erence)?$`` matches46   every type with suffix ``Ref``, ``ref``, ``Reference`` and ``reference``.47   The default is empty. If a name in the list contains the sequence `::`, it48   is matched against the qualified type name (i.e. ``namespace::Type``),49   otherwise it is matched against only the type name (i.e. ``Type``).50 51.. option:: ExcludedContainerTypes52 53   A semicolon-separated list of names of types whose methods are allowed to54   return the const reference the variable is copied from. When an expensive to55   copy variable is copy initialized by the return value from a type on this56   list the check does not trigger. This can be used to exclude types known to57   be const incorrect or where the lifetime or immutability of returned58   references is not tied to mutations of the container. An example are view59   types that don't own the underlying data. Like for `AllowedTypes` above,60   regular expressions are accepted and the inclusion of `::` determines whether61   the qualified typename is matched or not.62 63 64Limitations65-----------66 67This check does not perform lifetime analysis and may suggest replacing copies68with const references that could become dangling. Be cautious when the69referenced object might be invalidated by subsequent operations.70 71.. code-block:: c++72 73  void consume(const S&);74 75  void func(std::vector<S> &Vec) {76    const auto It = Vec.begin();77    const S Value(*It); // The warning will suggest making this a const reference.78    Vec.erase(It); // Container modifications could invalidate references.79    consume(Value); // Safe with copy, dangling reference otherwise.80  }81