当 if/for 语句后只有一行代码,没有大括号时,有没有办法通过 linting(或者可能seding/ ing)来摆脱或找到这些令人讨厌的情况?regexp像这个:
if(condition)
return;
Run Code Online (Sandbox Code Playgroud)
作为参考,为什么我想摆脱它 - 这个线程中给出了很多原因:
我维护一些遗留代码,并处理其他人的一些未真正完成的代码,并且在调试时时不时会发现这种代码风格像绊线一样工作的情况:
if(condition_for_early_return)
LOG("Im here") // surprise surprise, I just broke control logic
return;
Run Code Online (Sandbox Code Playgroud)
另外,我见过这样的代码:
if(condition)
<tabs> do_smth();
<spaces> do_smth_else();
Run Code Online (Sandbox Code Playgroud)
当然if只包含first do_smth(),编译器不会混淆。但由于这些do_功能在视觉上是一致的,我想知道这是预期的行为还是在遗留代码中从未发现的错误。
我知道cppcheck没有抓住这些情况 - 已经尝试过了。你有办法自动发现这些陷阱吗?