java Intellij Idea 提示:条件总是假的——这在这里是真的吗?(爪哇)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31829958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intellij Idea hint: Condition is always false - can that be true here? (Java)
提问by Mathias Bader
I have the following code:
我有以下代码:
public String testExitPoints() {
boolean myBoolean = false;
try {
if (getBoolean()) {
return "exit 1";
}
if (getBoolean()) {
throw new RuntimeException();
}
} finally {
myBoolean = true;
}
if (getBoolean()) {
return "exit 2";
}
return "exit 3";
}
public static boolean getBoolean() {
Random rand = new Random();
return rand.nextInt() > 100;
}
Now IntelliJ idea gives me for the second and third invocation of getBoolean()
the following hint:
现在 IntelliJ 的想法给了我第二次和第三次调用getBoolean()
以下提示:
Condition 'getBoolean()' is always 'false'
Now to my understanding, that is not true, since getBoolean()
can either be true
or false
, depending on the generated random value. Am I missing something here, or is that a bug in IntelliJ Idea?
现在据我所知,这不是真的,因为getBoolean()
可以是true
或false
,这取决于生成的随机值。我在这里遗漏了什么,还是 IntelliJ Idea 中的错误?
采纳答案by Diego Martinoia
It's not a bug. It's a feature :)
这不是一个错误。这是一个功能:)
If you look carefully in your IDE, it will tell you that the 2nd and 3rd call to getBoolean() are always false, but not the first one.
如果您在 IDE 中仔细查看,它会告诉您对 getBoolean() 的第二次和第三次调用始终为假,但不是第一次。
Idea assumes (in this case incorrectly) that your method, being parameterless and called "get"..., would return always the same value.
Idea 假设(在这种情况下是错误的)您的方法,无参数并称为“get”...,将始终返回相同的值。
If that were the case, and the first call was true, the other would never be accessed (because of the return).
如果是这种情况,并且第一个调用为真,另一个将永远不会被访问(因为返回)。
If the first call was false, so would be the others.
如果第一个调用是错误的,那么其他调用也是错误的。
IDEA tries to be smart w.r.t. good coding practices, but it's not infallible.
IDEA 试图通过良好的编码实践变得聪明,但它并非万无一失。
If you change your method to have parameters (or rename it so it doesn't look like a getter)
如果您将方法更改为具有参数(或将其重命名,使其看起来不像 getter)
public boolean getBoolean(int x) {
Random rand = new Random();
return rand.nextInt() > 100;
}
The warnings will go away (even if you invoke with the same argument all times).
警告将消失(即使您始终使用相同的参数调用)。
(Note that, even if it was a getter, if it's for a non-final field it's still wrong, as it may change in a multithreaded environment!)
(请注意,即使它是一个 getter,如果它用于非 final 字段,它仍然是错误的,因为它可能在多线程环境中发生变化!)
回答by Ger
Unfortunately, while the accepted answer gives a good explanation, it is not always possible to rename the triggering methods as they might reside in third party code. For example, I was using a first() function from the MongoDB library, which clearly could return a null value, but triggered warnings when I wanted to test it for nullity.
不幸的是,虽然接受的答案给出了很好的解释,但并不总是可以重命名触发方法,因为它们可能驻留在第三方代码中。例如,我使用了 MongoDB 库中的 first() 函数,它显然可以返回一个空值,但是当我想测试它是否为空时触发了警告。
If you are confident that IDEA is getting it wrong, just put
如果您确信 IDEA 弄错了,只需输入
//noinspection ConstantConditions
before the statement that is causing the problem.
在导致问题的语句之前。
In general, a handy option to know about is 'Inspect code...' under the 'Analyze' menu. There you can look at the whole project, or just the file you are concerned with. You might find many more areas of concern than you bargained for! The warning in the Question will be listed under 'probable bugs'. Note "probable" - IDEA knows it is not infallible :)
通常,一个方便的选项是“分析”菜单下的“检查代码...”。在那里您可以查看整个项目,或仅查看您关心的文件。您可能会发现比您预想的更多的关注领域!问题中的警告将列在“可能的错误”下。注意“可能”——IDEA 知道它不是绝对可靠的 :)
回答by StanislavL
IDEA thinks that the getBoolean(
) call is not changed when it's called for the second (or third time). Normally if you return the same value the second call could be never achieved. That's why IDEA highlight it.
IDEA 认为getBoolean(
) 调用在第二次(或第三次)调用时不会改变。通常,如果您返回相同的值,则永远无法实现第二次调用。这就是 IDEA 突出显示它的原因。
回答by Rajesh Goel
In my case if I you use MY_ACTUAL_CLASS_NAME.getBoolean() it does not complain (since the method is static). May be it is because IntelliJ Idea does not take static into account (a possible bug here)
就我而言,如果我使用 MY_ACTUAL_CLASS_NAME.getBoolean() 它不会抱怨(因为该方法是静态的)。可能是因为 IntelliJ Idea 没有考虑静态因素(此处可能存在错误)