Bug 114763 - [DCR] warn on potentially infinite loops
Summary: [DCR] warn on potentially infinite loops
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Linux
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 413591 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-11-02 10:15 EST by Maxime Daniel CLA
Modified: 2013-07-25 06:27 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Maxime Daniel CLA 2005-11-02 10:15:35 EST
In the following code:
class X {
  boolean bar() {
    return true;
  }
  void foo() {
    for (;;) {
      if (bar()) {
        return;
      }
    }
  }
}
the for loop exits by a side effect (return) instead of exiting when its
condition becomes false. Some developers consider this (and equivalent patterns
with while and do statements) as bad practice. A warning could be issued
whenever the loop condition consistently evaluates to true, before the first
loop iteration and after all subsequent iterations.
Comment 1 Dani Megert CLA 2013-07-24 03:25:48 EDT
*** Bug 413591 has been marked as a duplicate of this bug. ***
Comment 2 Adtc Rulez CLA 2013-07-24 07:00:39 EDT
Will this be implemented?

My comment from the duplicate bug:
-------
Is it possible for the Eclipse IDE to show a (confirmed) 'infinite loop' or 'potential infinite loop' warning (under the category of 'Potential programming problems')?

I think it could be detected where the condition of a while loop will always remain true throughout the execution of the loop (no reachable breaks, no modification of condition variables inside, no chance of modification from outside).

Similar logic could be applied to a for loop or a do-while loop as well.

In the case of for loop, codes such as for(;;) could be tagged with the 'infinite loop' warning if the code block does not have a reachable break statement. This would also be helpful in alerting the user to loop codes such as for(int i=10; i>0; i++). In this example (assuming there's no reachable break), the increment operator will result in an infinite loop as the condition will never become false (the code can only fail with integer overflow).
-------

For the second half of the last paragraph, Stephan makes the comment:
-------
This can only be detected by a tool that understands integer arithmetics.
The compiler does not belong in this group and I guess that most tools have
'difficulties' here.

The only thing I imagine we could detect is:
- condition is constantly true
- no reachable exit
But that's indeed bug 114763 and I don't know if that would actually help much.
-------

I would like to add in response that an infinite loop is a potential programming problem, and an IDE is supposed to help programmers avoid common problems when writing code, so that development is faster and time is not wasted in fixing such problems during testing. In that spirit, it will be useful to forewarn the user of a potential infinite loop, something one can currently only know when running the program and testing it.
Comment 3 Stephan Herrmann CLA 2013-07-25 05:54:23 EDT
(In reply to comment #2)
> Will this be implemented?

I'm afraid we don't have the resources ATM. 
 
I am much in favour of detecting likely programming problems. I just wanted to point
out that the full analysis which you seem to be expecting is close to impossible.
(Just imagine that analysing potential values of integer variables might easily
increase compilation time from milliseconds to hours - that's an entirely different
category than anything we are doing in the compiler).

Concluding that all we *could* do would be limited to the most obvious cases,
I'm simply asking how big would be the benefit.
My current assessment is: our time is much better spent in improving existing analysis
(NPE, resource leak, etc.) than in adding a feeble analysis of infinite loops.
So, how much would an analysis help that can only analyse loops with constant conditions?

The original request is even simpler, but even more limited. How much value would
you see in the proposal from comment 0?
Comment 4 Adtc Rulez CLA 2013-07-25 06:27:37 EDT
I suppose you have a point. It would be nice to have, but I guess a warning of constant conditions may not be all that useful. Anyway thanks for your consideration.