简体   繁体   English

在eclipse中没有使用数组的未使用变量警告

[英]No unused variable warning using arrays in eclipse

I use Eclipse Kepler and have a java file with this code: 我使用Eclipse Kepler,并使用以下代码创建了一个Java文件:

String[] arr = new String[] {"abc", "def"};

Eclipse now shows a warning: Eclipse现在显示警告:

The value of the local variable arr is not used 未使用局部变量arr的值

If I add one line, the warning vanishes: 如果我添加一行,该警告将消失:

String[] arr = new String[] {"abc", "def"};
arr[0] = "ghi";

Although no element of the array is read, Eclipse does not warn me about this. 尽管没有读取数组的任何元素,但是Eclipse并没有警告我。 Is there a reason for this behaviour or is it just an Eclipse bug? 是否有这种现象的原因,或者仅仅是Eclipse错误? (IntelliJ warns about arrays with no read access of their elements.) (IntelliJ警告有关无法读取其元素的数组。)

Edit: I know that the array itself is used, but I want my IDE to be smart enough to check if any elements inside the array have been used, because an array is in fact a container for its elements. 编辑:我知道使用数组本身,但是我希望我的IDE足够聪明以检查是否使用了数组中的任何元素,因为数组实际上是其元素的容器。

It's not a bug. 这不是错误。 For the compiler, when he see arr[0] , then the value was used. 对于编译器,当他看到arr[0] ,将使用该值。 Because first the program reads the content of arr[0] , then create "ghi" String, then assign it to arr[0] . 因为程序首先读取arr[0]的内容,然后创建"ghi"字符串,然后将其分配给arr[0] In the first case, the compiler notice that the variable is not referenced, so it warns you. 在第一种情况下,编译器会注意到未引用该变量,因此它向您发出警告。

There's a difference between detecting use, reads and writes to structures. 检测使用,读取和写入结构之间是有区别的。 Eclipse evidently only does the former (presumably because it's slightly easier) while others (such as IntelliJ) are more sophisticated and can deduce more information. Eclipse显然只做前者(大概是因为它稍微容易一些),而其他人(例如IntelliJ)则更复杂,并且可以推断出更多信息。

It's not a bug; 这不是错误; they simply didn't prioritize static analysis as highly in Eclipse. 他们只是没有在Eclipse中高度重视静态分析。

Eclipse is basically not smart enough to realise that the line of code you wrote doesn't actually have any effect on the behaviour of the program (In general this is very hard for a computer to determine). Eclipse基本上还不够聪明,无法意识到您编写的代码行实际上对程序的行为没有任何影响(通常,计算机很难确定)。 As far as it's concerned, if a local variable is not referenced after its declaration, it is unused. 就其而言,如果声明后未引用局部变量,则该局部变量未被使用。

In here 在这里

    String[] arr = new String[] {"abc", "def"};
    arr[0] = "ghi";  // you are writing to arr

But never read. 但是从来没有读过。 Now both conditions are correct. 现在两个条件都正确。 arr use in the code and never read arr 在代码中使用arr ,从不读取arr

Then IntelliJ IDEA will said you are not read arr here while Ecplise never show previous warning. 然后IntelliJ IDEA会说您在这里没有读到arr ,而Ecplise从不显示先前的警告。 That is not a bug. 那不是错误。 Those two are features of IntelliJ IDEA and Eclipse . 这两个是IntelliJ IDEAEclipse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM