Should the Eclipse detector detect such IndexOutOfBounds exceptions?

Assume this code:

String[] data = new String[2]; data[0] = "OK"; data[1] = "returning data"; data[2] = "data out of bounds"; 

Of course, throw an IndexOutOfBounds exception.

My question is:

  • Is there some reason because the Eclipse parser does not complain about this problem with warning or error ?

Analyzing the case, I find:

  • Easy to spot.
  • Avoid a lot of useless starts.

There are other similar warnings / errors, for example:

  • uninitialized variable when you try to use a variable declared as List<String> = null .
  • unreachable code when writing sentences after a valid return .
  • unused code in dead branches of if .
+6
source share
4 answers

Other cases you mentioned are not directly related:

The last remaining is a dead code . This is not a mistake, and the reasons are also explained in the Unreachable Statementments section:

You can expect the if statement to be processed as follows:

...

This approach is consistent with the processing of other management structures. However, in order for the if statement to be used conveniently for "conditional compilation" purposes, the actual rules are different.

As an example, the following statement results in a compile-time error:

 while (false) { x=3; } 

because the statement x = 3; not available; but an outwardly similar case:

 if (false) { x=3; } 

does not result in a compile-time error. The optimizing compiler can understand that the operator x = 3; will never be executed and may omit the code for this statement from the file of the generated class, but statement x = 3; not considered as β€œunreachable” in the technical sense indicated here.

Thus, the "dead code" is "unusual" and, of course, deserves attention, but is not necessarily a mistake.


Regarding your actual question (ignoring the fact that Parser and Compiler are two different things - this is clear what this is about): This is a very specific case. This applies only when the declaration of the array and access to the array are in the same area. In addition, indexes must be compile-time constants.

Thus, it is obvious that

 void init(String data[]) { data[0] = "OK"; data[1] = "returning data"; data[2] = "data out of bounds"; } 

or

 int i = 0; data[i++] = "OK"; data[i++] = "returning data"; data[i++] = "data out of bounds"; 

There is probably no convincing, deep reason why this is not detected by Eclipse, except that the ratio of efforts to detect such errors and benefits is not high enough - perhaps something like this will be added in a future release. Eclipse already has some pretty complex warnings (like automatically detecting "Null pointer access"). But for more detailed static analysis, there are special tools, such as FindBugs or PMD , which put more effort (and more time, by the way) than tests that are performed randomly and on the fly eclipses.


Note: you can avoid this kind of error with

 String data[] = { "OK", "returning data", "data out of bounds" }; 
+2
source

You can install the FindBugs plugin for Eclipse (available on the Eclipse market). He is able to detect such a problem:

FindBugs Warning

+4
source

Is there some reason the Eclipse parser is not complaining about this warning or error issue?

Eclipse does not give you an error message because it is valid Java code, according to the Java language specification. If Eclipse saw this as a compilation error, this would not be a proper Java compiler.

The warning case is less clear. Yes, Eclipse can (legally) provide a warning, just as it warns of some cases involving NPE. However, there is a trade-off between creating useful warnings and the time the compiler checks for conditions that give rise to them. Too much checking will make the compiler slower for all programs, and programmers will complain about it. (Especially programmers who work with applications with thousands of classes.)

As @Tagir notes, if you want to be warned about this, consider using an error controller such as FindBugs or PMD.


There are other similar warnings / errors, for example ...

Two of the three cases you have listed are all compilation errors, because the Java language specification claims to be invalid Java code. I'm not sure about the "dead code" case, because it is not clear which case you are referring to. If you mean

 if (<compile-time-constant-expr>) { ... } else { ... } 

then this code should not give a compilation error or warning.

+1
source

Shouldn't detect an Eclipse parser detection of this type of IndexOutOfBounds exception?

Obviously, the answer is yes, as it is easy to detect.

But you can also do better and avoid potential errors without using a magic constant:

 String[] data = new String[] { "OK", "returning data", "data not out of bounds" }; 
0
source

All Articles