Super Lint

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Wed Sep 20 14:43:32 PDT 2006


Walter Bright wrote:
> Ivan Senji wrote:
>> Walter Bright wrote:
>>> Pragma wrote:
>>>> Walter Bright wrote:
>>>>> I've been interested in various ideas for static checking for 
>>>>> common bug patterns for D. For example:
>>>>>
>>>>>     for (int i = 0; i < 10; i++)
>>>>>     {    foo();
>>>>>         break;
>>>>>     }
>>>>>
>>>>> would be flagged as a suspicious use of break.
> 
>> But is it safe do declare the above example as a bug? Maybe that break 
>> is placed there to see what just one pass of the loop does (maybe for 
>> debugging purposes).
> 
> That certainly is the question. I don't think I'd want to see such code 
> in released source, but:
> 
> 1) is it legitimate for debug/test code?

Most legal code will probably be legitimate for /some/ debug/test code. 
The question is, would you want to run a tool like this on such code? 
(I'm presuming this will be an exteral tool)
Besides, if debug code like this is found, I'd hope a tool like this 
would alert me to the fact I forgot to remove some debugging code.

> 2) can it come about as the side effect of some other coding pattern?

Probably not as written, but how about this variant:

     for (int i = 0; i < 10; i++)
     {
         if (foo()) continue;
         break;
     }

Granted, it should be relatively simple to detect a continue statement 
that has an effect on the loop since it can't hide in function calls.
I can't think of any other reason to use an unconditional break except 
the above-mentioned debug scenario.

> 3) if it is made illegal, is that going to be a bigger problem than it 
> solves?

Illegal or just flagged by this "super lint"?
I've never used lint, but my understanding is that it's an external tool 
that checks for common errors (that are still legal code). As such, it 
can't actually make things illegal, just advise against them when found.

> 4) does it really solve a problem?
> 
> For example, it could happen with (example found on the internet):
> 
>     for (int i = 0; i < 10; i++)
>     {
>         if (condition)
>             action();
>             break;
>     }
> 
> where the user forgot to put { } around the else clause. It's sort of 
> like how:
> 
>     for (int i = 0; i < 10; i++);
>     {
>         ...
>     }
> 
> is illegal in D (note the ; after the closing parenthesis). I've known 
> people to spend many hours trying to track down this nearly invisible bug.

This is a tougher question. Though it may be nice to have it warn about 
multiple indented statements (without {}s) after a conditional or loop 
statement, that might be a more common problem than unconditional breaks.
Of course, that would mean it has to pay attention to formatting, which 
might complicate things.



More information about the Digitalmars-d mailing list