Why didn't the compiler warn me about an empty if-statement?

前端 未结 2 1296
孤城傲影
孤城傲影 2020-12-17 09:08

I\'m using Keil uVision v4.74 and have enabled the option \"All Warnings\".

I wrote the following intentional code:

if(condition mat         


        
2条回答
  •  既然无缘
    2020-12-17 09:27

    As Matteo's answer indicated, the code is absolutely valid. It's being interpreted this way:

    if(condition)
        ;  // do nothing
    
    // unrelated block
    {
        // do something
    }
    

    It's a bit of a technicality, but conditions with empty bodies do have some very nice uses.

    Lint and other such code sanity tools will warn about the unexpected change in indentation, and catch additional errors that may be stylistic though not technically compiler errors.

    Or security problems, variable tainting, buffer management, potential maintenance problems like bad casts, etc. There are an awful lot of code problems that don't fall into the category of "compiler errors".

    As @jpmc26 mentioned, this approach may be better since you don't have to switch compilers to use it. Though I also personally find value in the ability to run the two independently.

提交回复
热议问题