This patch contains an implementation to check whether exceptions where thrown by value and caught by const reference. This is a guideline mentioned in different places, e.g. "C++ Coding Standards" by H. Sutter.
Patch by Tobias Langner
Differential D11328
[clang-tidy] new "throw-by-value-catch-by-reference" check for clang-tidy randomcppprogrammer on Jul 18 2015, 8:44 AM. Authored by
Details
This patch contains an implementation to check whether exceptions where thrown by value and caught by const reference. This is a guideline mentioned in different places, e.g. "C++ Coding Standards" by H. Sutter. Patch by Tobias Langner
Diff Detail Event TimelineComment Actions Updated check according to comments given by Aaron Ballman. Most notable change: added optional check for throwing anonmyous temporaries. Comment Actions This is looking great! Some mostly minor feedback below. The only things I noticed that may require further consideration are throwing function parameters and throwing exception handler variables -- I don't think those scenarios should emit a diagnostic. The former is a bit chatty because of error handling functions, and the latter is correct regardless of the type being thrown (though is a bit silly because the user should really just use throw; instead of rethrowing the variable). General nitpick: please make sure all comments in the code and tests have proper capitalization, punctuation, etc.
Comment Actions reworked code to include the changes suggested by Aaron Ballman main changes
Comment Actions This patch is looking much closer! Thank you for continuing to work on it. I found several mechanical changes that need to be made, like style, comments, formatting, isa<> followed by cast<>, etc. There is one logic issue regarding materialized temporaries that I think still needs to be addressed, but otherwise, this patch is getting close!
Comment Actions Incorporated feedback from Aaron Ballmann main changes:
Comment Actions On Wed, Oct 7, 2015 at 5:08 PM, Tobias Langner <randomcppprogrammer@gmail.com> wrote:
Thank you for your work on this! There are a few minor nitpicks still, but is definitely moving forward nicely.
Consider code like: struct S {}; S& g(); S h(); void f() { throw g(); // Should diagnose, does not currently throw h(); // Should not diagnose, does not currently } "throw g();" isn't throwing a temporary because it's throwing from an lvalue reference object (so the user may have a catch clause expecting the caught object to be the same lvalue reference as what g() returns, except that's not actually the case). If you instead check to see whether the throw expression requires a temporary to be materialized, you'll find that g() will diagnose, while h() still will not. Does that help?
Comment Actions new:
struct S; void foo() { throw function(); }
Comment Actions LGTM! Please run your patch through clang-format before committing (just to fix the last bits of style and whitespace issues; no additional review required). Thank you for your hard work on this! As a cleanup once this goes in: we should add some user-facing documentation to docs\clang-tidy\checks for it. We can even take what's in ThrowByValueCatchByReferenceCheck.h and use that (then link to it from the header file instead of duplicating the words). |
Should be Check instead of check per style guidelines.