summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/docs/clang-tidy/checks/misc-throw-by-value-catch-by-reference.rst
blob: cc970fe014cc1c3913f627a874fe850ea9211171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
.. title:: clang-tidy - misc-throw-by-value-catch-by-reference

misc-throw-by-value-catch-by-reference
======================================

`cert-err09-cpp` redirects here as an alias for this check.
`cert-err61-cpp` redirects here as an alias for this check.

Finds violations of the rule "Throw by value, catch by reference" presented for
example in "C++ Coding Standards" by H. Sutter and A. Alexandrescu, as well as
the CERT C++ Coding Standard rule `ERR61-CPP. Catch exceptions by lvalue reference
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/ERR61-CPP.+Catch+exceptions+by+lvalue+reference>`_.


Exceptions:
  * Throwing string literals will not be flagged despite being a pointer. They
    are not susceptible to slicing and the usage of string literals is idomatic.
  * Catching character pointers (``char``, ``wchar_t``, unicode character types)
    will not be flagged to allow catching sting literals.
  * Moved named values will not be flagged as not throwing an anonymous
    temporary. In this case we can be sure that the user knows that the object
    can't be accessed outside catch blocks handling the error.
  * Throwing function parameters will not be flagged as not throwing an
    anonymous temporary. This allows helper functions for throwing.
  * Re-throwing caught exception variables will not be flragged as not throwing
    an anonymous temporary. Although this can usually be done by just writing
    ``throw;`` it happens often enough in real code.

Options
-------

.. option:: CheckThrowTemporaries

   Triggers detection of violations of the CERT recommendation ERR09-CPP. Throw
   anonymous temporaries.
   Default is `1`.

.. option:: WarnOnLargeObject

   Also warns for any large, trivial object caught by value. Catching a large
   object by value is not dangerous but affects the performance negatively. The
   maximum size of an object allowed to be caught without warning can be set
   using the `MaxSize` option.
   Default is `0`.

.. option:: MaxSize

   Determines the maximum size of an object allowed to be caught without
   warning. Only applicable if `WarnOnLargeObject` is set to `1`. If option is
   set by the user to `std::numeric_limits<uint64_t>::max()` then it reverts to
   the default value.
   Default is the size of `size_t`.
OpenPOWER on IntegriCloud