avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] Feature request - compiler warning for "if" stmts tha


From: Dave Hansen
Subject: RE: [avr-gcc-list] Feature request - compiler warning for "if" stmts that do nothing
Date: Mon, 13 Dec 2004 11:15:46 -0500

From: "Dave Hylands" <address@hidden>
[...]
> -Wextra?
>
> According to the man page:
>
>  o   An empty body occurs in an if or else statement.
>
> I'd assume this to be warned.

Hey - that's pretty close.

It triggers on the original posters original request:

        if ( condition );
        {
                ... Do something ...
        }

x.c:8: warning: empty body in an if-statement

And doesn't trigger on my example: if (condition);else{...do
something...}

It still doesn't warn about while/for loops that are similar:

        while ( conition );
        {
                ... Do something ...
        }

And

        for (i=0;i<10;i++);
        {
                ... Do something ...
        }

So it's definitely a step in the right direction.

FWIW...

I use Gimpel's PC-lint (and have for over 15 years). It has always done a very noce job of finding these kinds of problems.

--- begin included file ---
C:\Dave>type mt.c
extern int x;

void foo(void)
{
   int i;

   if (x < 10);
   {
       ++x;
   }

   if (x > 100) ;else
   {
       --x;
   }

   for (i=0; i<10; i++);
   {
       x += x/i;
   }

   while (--i > 5);
   {
       x -= x*i;
   }
}



C:\Dave>lint-nt -u mt.c
PC-lint for C/C++ (NT) Ver. 8.00q, Copyright Gimpel Software 1985-2004

--- Module:   mt.c
             _
   if (x < 10);
mt.c  7  Info 721: Suspicious use of ;
   _
   {
mt.c  8  Warning 548: else expected
                      _
   for (i=0; i<10; i++);
mt.c  17  Info 722: Suspicious use of ;
                 _
   while (--i > 5);
mt.c  22  Info 722: Suspicious use of ;

C:\Dave>
---end included file---

The -u option specifies a "unit" lint, so errors like "x not defined" and "foo not called" are suppressed.

Note the first "if" statement generates two warnings because the opening brace of the body doesn't have a leading else (which is kind of suspicious if you really meant the semicolon.

Also note that there is no message for the "if (x > 100) ;else" line because of the space between the parenthesis and the semicolon. I the colon is flush to the paren, the warning is generated. Indeed, any intervening whitespace will suppress the warning on any of the lines. Warnings can also be suppressed by a "lint comment," e.g., "/*lint !e722*/" tells PC-lint to suppress warning 722 on this line.

See www.gimpel.com for more info. A very powerful tool for not a lot of money (less than $250), and it works with pretty much any C compiler.

Lint early, lint often.  Lint is your friend.

Regards,
  -=Dave




reply via email to

[Prev in Thread] Current Thread [Next in Thread]