Why does IntelliJ IDEA think my Boolean method is always inverted when it is not?

Sebastiaan van den Broek :

I'm new to using IntelliJ Idea and I have the following code:

private boolean verifyToken(TokenTypeEnum expectedTokenType, Token token) {
        return token != null &&
               token.getTokenType() == expectedTokenType &&
               token.getExpiryDate().isAfter(Instant.now());
}

Now IntelliJ-IDEA complains that this Boolean method is always inverted. But I specifically wrote the method so it returns a positive, I do not want to make a method isInvalidToken or something like that. I believe the point of this warning is actually to avoid negatives such as those.

What's more, if I actually let IntelliJ invert the method automatically, it makes the expression like this which is exactly NOT what I would want as it is much harder to reason about (even if I would rewrite the date comparison)

private boolean newNegativeMethod(TokenTypeEnum expectedTokenType, Token token) {
        return token == null ||
               token.getTokenType() != expectedTokenType ||
               !token.getExpiryDate().isAfter(Instant.now());
}

Why is it doing this? I can disable the warning of course, but I'd rather not.

yole :

"Always inverted" means that in all places where the method is called, the ! operator is applied to the call expression. IntelliJ IDEA has no clue whether this is hard or easy to reason about from the semantic point of view; it just performs syntactic analysis. If you believe that inverting the method will make the code harder to understand, simply suppress the inspection for this method.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=442105&siteId=1