Findbugs 告诉我,我无缘无故地将非负值与 -1 进行了比较
Findbugs is telling me that I have comparison of nonnegative value with -1 for no apparent reason
我有这段代码,它在 FindBugs 中显示以下错误:
Bug: Bad comparison of nonnegative value with -1 in hydra.extensions.drivers.eg2.internal.EG2GatewaySimulator.run()
This code compares a value that is guaranteed to be non-negative with a negative constant or zero.
Rank: Scary (5), confidence: High
Pattern: INT_BAD_COMPARISON_WITH_NONNEGATIVE_VALUE
Type: INT, Category: CORRECTNESS (Correctness)
对于代码:
String receivedMessage = "";
char c;
boolean isValidChar;
do {
c = (char) in.read();
isValidChar = (c != '\r') && (c != -1);
if (isValidChar) {
receivedMessage += c;
}
} while (isValidChar);
char 的值为 16 位非负值:
char:char 数据类型是单个 16 位 Unicode 字符。它的最小值为 '\u0000'(或 0),最大值为 '\uffff'(或 65,535,含)。
您正在将它与 -1 进行比较。
我有这段代码,它在 FindBugs 中显示以下错误:
Bug: Bad comparison of nonnegative value with -1 in hydra.extensions.drivers.eg2.internal.EG2GatewaySimulator.run()
This code compares a value that is guaranteed to be non-negative with a negative constant or zero.
Rank: Scary (5), confidence: High
Pattern: INT_BAD_COMPARISON_WITH_NONNEGATIVE_VALUE
Type: INT, Category: CORRECTNESS (Correctness)
对于代码:
String receivedMessage = "";
char c;
boolean isValidChar;
do {
c = (char) in.read();
isValidChar = (c != '\r') && (c != -1);
if (isValidChar) {
receivedMessage += c;
}
} while (isValidChar);
char 的值为 16 位非负值:
char:char 数据类型是单个 16 位 Unicode 字符。它的最小值为 '\u0000'(或 0),最大值为 '\uffff'(或 65,535,含)。
您正在将它与 -1 进行比较。