xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/misc/throw-by-value-catch-by-reference.rst (revision 3cf574da407b9be01cc86f0ad2902c5d2ff8f078)
1.. title:: clang-tidy - misc-throw-by-value-catch-by-reference
2
3misc-throw-by-value-catch-by-reference
4======================================
5
6`cert-err09-cpp` and `cert-err61-cpp` redirect here as aliases of this check.
7
8Finds violations of the rule "Throw by value, catch by reference" presented for
9example in "C++ Coding Standards" by H. Sutter and A. Alexandrescu, as well as
10the CERT C++ Coding Standard rule `ERR61-CPP. Catch exceptions by lvalue reference
11<https://wiki.sei.cmu.edu/confluence/display/cplusplus/ERR61-CPP.+Catch+exceptions+by+lvalue+reference>`_.
12
13
14Exceptions:
15  * Throwing string literals will not be flagged despite being a pointer. They
16    are not susceptible to slicing and the usage of string literals is
17    idiomatic.
18  * Catching character pointers (``char``, ``wchar_t``, unicode character types)
19    will not be flagged to allow catching string literals.
20  * Moved named values will not be flagged as not throwing an anonymous
21    temporary. In this case we can be sure that the user knows that the object
22    can't be accessed outside catch blocks handling the error.
23  * Throwing function parameters will not be flagged as not throwing an
24    anonymous temporary. This allows helper functions for throwing.
25  * Re-throwing caught exception variables will not be flagged as not throwing
26    an anonymous temporary. Although this can usually be done by just writing
27    ``throw;`` it happens often enough in real code.
28
29Options
30-------
31
32.. option:: CheckThrowTemporaries
33
34   Triggers detection of violations of the CERT recommendation ERR09-CPP. Throw
35   anonymous temporaries.
36   Default is `true`.
37
38.. option:: WarnOnLargeObject
39
40   Also warns for any large, trivial object caught by value. Catching a large
41   object by value is not dangerous but affects the performance negatively. The
42   maximum size of an object allowed to be caught without warning can be set
43   using the `MaxSize` option.
44   Default is `false`.
45
46.. option:: MaxSize
47
48   Determines the maximum size of an object allowed to be caught without
49   warning. Only applicable if :option:`WarnOnLargeObject` is set to `true`. If
50   the option is set by the user to `std::numeric_limits<uint64_t>::max()` then
51   it reverts to the default value.
52   Default is the size of `size_t`.
53