Findbugs告诉我,我没有明显的理由将非负值与-1进行比较

Ped*_*roD 1 java findbugs

我有这个代码,它在FindBugs中显示以下错误:

在此输入图像描述

Bug: Bad comparison of nonnegative value with -1 in hydra.extensions.drivers.eg2.internal.EG2GatewaySimulator$1.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)
Run Code Online (Sandbox Code Playgroud)

对于代码:

String receivedMessage = "";
char c;
boolean isValidChar;
do {
    c = (char) in.read();
    isValidChar = (c != '\r') && (c != -1);
    if (isValidChar) {
        receivedMessage += c;
    }
} while (isValidChar);
Run Code Online (Sandbox Code Playgroud)

ali*_*her 6

char的值是16位非负的:

char:char数据类型是一个16位Unicode字符.它的最小值为'\ u0000'(或0),最大值为'\ uffff'(或65,535(含)).

您正在将它与-1进行比较.