| Summary: | [DCR] Quick Assist : unneeded else | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Mohamed ZERGAOUI <xmlizer> |
| Component: | Core | Assignee: | JDT-Core-Inbox <jdt-core-inbox> |
| Status: | RESOLVED WONTFIX | QA Contact: | |
| Severity: | enhancement | ||
| Priority: | P3 | ||
| Version: | 3.0 | ||
| Target Milestone: | --- | ||
| Hardware: | All | ||
| OS: | All | ||
| Whiteboard: | |||
| Bug Depends on: | 49905 | ||
| Bug Blocks: | |||
I'm not sure to undertand why the else block is not needed in your example. it is unneeded because the flow is broken (after a <return>, a <throw>, a
<break> or a <continue>) so it obvious that if we remove the else the control
flow is the same
for example :
if (isAnExitCondition()) {
// do some finalization stuff
return false; // for example
} else {
// the rest of the method
...
}
there is one level of indentation that is superfluous
it is equivalent
if (isAnExitCondition()) {
// do some finalization stuff
return false; // for example
}
{
// the rest of the method
...
}
and if it is possible to remove the unneeded brace to have a more READABLE
source (see also #49905)
if (isAnExitCondition()) {
// do some finalization stuff
return false; // for example
}
// the rest of the method
...
I see. I thought you wanted to remove the whole else block. This would indeed be nice to have. I like this suggestion, and it is easy to implement. Will do right now. public class X {
void foo() {
if (this == null) {
return;
} else {
System.out.println("foo");
}
}
}
----------
1. WARNING in ...\X.java (at line 6)
} else {
System.out.println("foo");
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Statement unnecessarily nested within else clause. The corresponding then
clause does not complete normally
----------
Fixed. Enabled this warning in compiler regression test suites.
Verified in 200405180816 Good job guys, it works well, But I propose to reopen this bug (may be could you split this demand) because the initial purpose was to give a Quick Assist I let you move it to JDT/UI Could you please enter a separate defects against JDT/UI ? The compiler warning is the basis for subsequent UI actions. What about "System.exit()" ? Can "indirect flow be detected ?
example
void throwException() throws Exception {
throw new Exception();
}
void b() {
if (test()) {
doSomeStuff();
throwException();
} else {
doSomeOtherStuff();
}
}
could "else { doSomeOtherStuff }" be flagged as unneeded else
Of course i forgot to mention that "throwException()" is either 'final' or 'static' or 'private' (or some combination of these) I don't think that the else should be flagged using your example.
void b() {
if (test()) {
doSomeStuff();
throwException();
} else {
doSomeOtherStuff();
}
}
Nothing says that throwException(); will throw an exception for sure. It is
possible that an exception is thrown. So when no exception is thrown, the else
statement makes sense.
The fact that your implementation of throwException throws an exception is not
relevant in this example. So I would say that in your case the else should NOT
be flagged as unnecessary.
I agree with Olivier, we can only consider local control flow. It is may be a philosophical question, but, do you mean "System.exit(int)" as a
java flow breaker like "<return>" ?
In the case, the answer is yes, it must be handled and trigger in unnecessarily
nested for the example
if (isAnExitCondition()) {
// do some finalization stuff
System.exit(0); // for example
} else {
// the rest of the method
...
}
Reopen for System.exit(int) ? Deferring post 3.1 As of now 'LATER' and 'REMIND' resolutions are no longer supported. Please reopen this bug if it is still valid for you. |
it will be nice to have a tool which can detect that there is unneeded else. I have for the moment only one case in mind : <code> if (....) { .... .... return ....; // or throw ....; // or break ....; // or continue ....; } else { ...... } in that case a quick assist would be to propose to remove the else keyword only (to keep the brackets) and in a second time to propose to remove the brackets if there will be no variable name conflict.