5

I'm looking at ProducerPerformance.java from https://github.com/apache/kafka.

Looking at this part of the file:

        byte[] payload = null;
        Random random = new Random(0);
        if (recordSize != null) {
            payload = new byte[recordSize];
            for (int i = 0; i < payload.length; ++i)
                payload[i] = (byte) (random.nextInt(26) + 65);
        }

There is an inspection saying that "Condition i < payload.length is always false."

I don't see how it could always be false. recordSize is an Integer coming from a command-line parameter. Is there something I'm not seeing here?

k314159
  • 5,051
  • 10
  • 32
  • 8
    No, looks pretty much like a false positive. The code analysis may treat recordSize as always null thus thinking that the if may never be true. – maio290 Mar 15 '21 at 21:11
  • This only makes sense if `recordSize` could only be `null` or `0` before this chunk of code. How is `recordSize` actually set? – Joachim Sauer Mar 15 '21 at 21:12
  • @JoachimSauer `Integer recordSize = res.getInt("recordSize");` with `Namespace res = parser.parseArgs(args);`. See https://github.com/apache/kafka/blob/trunk/tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java#L57 – Zabuzard Mar 15 '21 at 21:12
  • In that case I agree with maio290: seems like a bug in the analysis. – Joachim Sauer Mar 15 '21 at 21:13
  • Similar to this one https://stackoverflow.com/questions/31829958/intellij-idea-hint-condition-is-always-false-can-that-be-true-here-java – dreamcrash Mar 15 '21 at 21:50
  • 1
    Please tell if the issue is reproduced for you with the latest version? https://www.jetbrains.com/idea/nextversion/ – Olga Klisho Mar 16 '21 at 15:32
  • @OlgaKlisho this has been fixed in 2021.1. Thanks. – k314159 Apr 07 '21 at 22:03
  • @BasilBourque good idea, done. – k314159 Apr 08 '21 at 07:37

1 Answers1

2

This warning happened with IntelliJ IDEA 2020.*. The latest version, 2021.1, fixes this issue.

k314159
  • 5,051
  • 10
  • 32
  • Can you link to the Issue page for more info? – Basil Bourque Apr 08 '21 at 08:21
  • @BasilBourque I will see if I can find it. I said the latest version fixes the issue because I no longer get this warning with the new version (and I've checked that the warning is not suppressed). – k314159 Apr 08 '21 at 08:32