Tuesday, February 22, 2011

Re: C syntax, and spurious semicolons

On Hugh Sasse wrote:

> I've been assisting a colleague with their C code, and he has
> learned to add semicolons at the end of statements too well.  His
> current favourite mistake is to write
>  if (expression == value);
>  {
>    do_something();
>  };
> I frequently miss the trailing semicolon on the first line.   So I
> was wondering if it would make sense to modify the C syntax file to
> highlight
>   if (expr);
> such that the semicolon shows up as an error.  Clearly it is a legal
> null statement, and so is syntactically correct.  But if one only
> wanted the expr for its side effects, one would not need to put it
> in an if.  To do this because one is only using the else part is
> poor communication with other programmers.
>
> Is there a good reason not to change the syntax file?  Otherwise,
> can anyone suggest a patch: my skills in this area are suboptimal,
> as I don't change syntax files very often.
>        Thank you
>        Hugh


Without using Vim, gcc can give a warning for such dubious code:

$ cat foo.cpp | nl
1 int main(int argc, char **argv)
2 {
3 int a;
4 if (argc == 1);
5 {
6 a = 1;
7 }
8 a = 0;
9 return a;
10 }

$ g++ -Wall -Wempty-body foo.cpp
foo.cpp: In function 'int main(int, char**)':
foo.cpp:4: warning: suggest braces around empty body in an 'if' statement

-- Dominique

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: