New check for rethrow without current handled exception#3270
Conversation
danmar
left a comment
There was a problem hiding this comment.
I like this check. Have some small nits.
|
I think it would be best to implement this with the CTU (Cross Translation Unit) analysis. Let me know if you want some intro for how that is done. According to https://pvs-studio.com/en/docs/warnings/v667/ , no warning should be written here: |
Oh, I didn't know that Cppcheck has CTU functionality. It could help to avoid FP. |
|
About performance; yes maybe it can be a problem. I don't know. We have a setting for how deep the ctu analysis is allowed to go. normally that setting will limit true positives but I guess in this case a low value could lead to false positives. well .. I hope that determining what functions are called from catch scopes shouldn't mean run time explodes too much. Normally I would assume that catch scopes are pretty simple. Maybe some error message is logged. Some variable is assigned. Not much more than that usually. If we for instance determine that no catch scope in the analyzed code will call any functions then I feel it would be ok to write these warnings written by this checker. If there are function calls it would be good to avoid warnings in those functions that are called.. It is still possible there will be false positives if the code is a library for instance.. but this is probably ok. |
|
Yeah, I expect same type of catch scopes. Anyway, to add CTU extra check I need some example. May be you can tell which check should I use as an example. BTW, one more thing, if we have such hypothetical function with Anyway, what's the plan?) |
|
Maybe you can test your checker with the tools/test-my-pr.py script. Then we get an idea about how much false positives there are.. Just checkout your branch and then run the script without arguments..
With my suggestion we would have a false negative there. But this should not affect many functions. |
|
4 hits in 2 packages:
I guess I could just add check for canonical style of this idiom: |
c53fd5f to
9c8fc2a
Compare
|
my_check_diff.log |
ok sounds good. How do you determine that the hit is legit.. have you looked up all the catch scopes and determined that they don't execute the code? Or do you say so because the code looks dangerous. I think technically we can merge this. But well.. I am still not 100% satisfied with the message. Somehow it sounds like we don't allow the exception dispatcher idiom. I believe your original message was better in that way. I just don't like to say "Calling" throw. I don't know if we can elaborate on your original message somehow. Or maybe something like: Rethrowing current exception with 'throw;', it seems there is no current exception to rethrow. If there is no current exception this calls std::terminate(). |
The last one, it looks dangerous: it's a virtual methods with 2 lines of code:
Yea, technically it's an expression and it should be evaluated, but it sounds much worse. It can be using instead of calling, but still not perfect enough. There are a bright side, it will be really rare message (2 of 512 projects) =) |
This check should warn user about forgotten argument for
throw. I guess it could be quite common in case if you go to look up the right type of exception or macro for it and got distracted or something.Possible FP can be if rethrow which located inside a function which called from a catch block. But in this case it should be one place for entire project if code in not bad shape.
Standard references:
Also similar check presented in another analyzer: https://pvs-studio.com/en/docs/warnings/v667/