Is there way to get rid or find by linting (or maybe sed
ing/regexp
ing) these nasty situations when your have just one line of code after if/for statement, without curly braces? Like this one:
if(condition)
return;
For reference why would I want to get rid of that - there are lots of reasons given in this thread:
What's the purpose of using braces (i.e. {}) for a single-line if or loop?
I maintain some legacy code, and deal with some not-really-finished code from other people, and from time to time stumble on situation when this code-style works like a trip wire when debugging:
if(condition_for_early_return)
LOG("Im here") // surprise surprise, I just broke control logic
return;
Also, I've seen code like that:
if(condition)
<tabs> do_smth();
<spaces> do_smth_else();
Of course if
contains only first do_smth()
, compiler is not confused. But because the do_
functions are visually aligned, I wonder - is this intended behaviour or is it a bug that was never found in this legacy code.
I know cppcheck
does not catch these situation - already tried that.
Do you have any way of finding these traps automatically?