kafka/checkstyle
Lucas Brutschy fc2e3dfce9
MINOR: Disallow unused local variables (#18963)
Recently, we found a regression that could have been detected by static
analysis, since a local variable wasn't being passed to a method during
a refactoring, and was left unused. It was fixed in
[7a749b5](7a749b589f),
but almost slipped into 4.0. Unused variables are typically detected by
IDEs, but this is insufficient to prevent these kinds of bugs. This
change enables unused local variable detection in checkstyle for Kafka.

A few notes on the usage:
- There are two situations in which people actually want to have a local
variable but not use it. First, there are `for (Type ignored:
collection)` loops which have to loop `collection.length` number of
times, but that do not use `ignored` in the loop body. These are
typically still easier to read than a classical `for` loop. Second, some
IDEs detect it if a return value of a function such as `File.delete` is
not being used. In this case, people sometimes store the result in an
unused local variable to make ignoring the return value explicit and to
avoid the squiggly lines.
- In Java 22, unsued local variables can be omitted by using a single
underscore `_`. This is supported by checkstyle. In pre-22 versions,
IntelliJ allows such variables to be named `ignored` to suppress the
unused local variable warning. This pattern is often (but not
consistently) used in the Kafka codebase. This is, however, not
supported by checkstyle.

Since we cannot switch to Java 22, yet, and we want to use automated
detection using checkstyle, we have to resort to prefixing the unused
local variables with `@SuppressWarnings("UnusedLocalVariable")`. We have
to apply this in 11 cases across the Kafka codebase. While not being
pretty, I'd argue it's worth it to prevent bugs like the one fixed in
[7a749b5](7a749b589f).

Reviewers: Andrew Schofield <aschofield@confluent.io>, David Arthur
<mumrah@gmail.com>, Matthias J. Sax <matthias@confluent.io>, Bruno
Cadonna <cadonna@apache.org>, Kirk True <ktrue@confluent.io>
2025-03-10 09:37:35 +01:00
..
.scalafmt.conf MINOR: Update to Scala 2.13.12 (#14430) 2023-09-24 06:05:12 -07:00
checkstyle.xml MINOR: Disallow unused local variables (#18963) 2025-03-10 09:37:35 +01:00
import-control-coordinator-common.xml KAFKA-18813: ConsumerGroupHeartbeat API and ConsumerGroupDescribe API must check topic describe (#18989) 2025-02-26 13:05:36 -05:00
import-control-core.xml KAFKA-18915: Migrate AdminClientRebootstrapTest to use new test infra (#19094) 2025-03-06 16:05:51 -05:00
import-control-examples.xml KAFKA-18658 add import control for examples module (#18812) 2025-02-11 06:07:40 +08:00
import-control-group-coordinator.xml KAFKA-18813: ConsumerGroupHeartbeat API and ConsumerGroupDescribe API must check topic describe (#18989) 2025-02-26 13:05:36 -05:00
import-control-jmh-benchmarks.xml KAFKA-17569 Rewrite TestLinearWriteSpeed by Java (#17736) 2024-11-26 23:43:01 +08:00
import-control-metadata.xml KAFKA-17587 Refactor test infrastructure (#18602) 2025-01-24 09:03:43 -05:00
import-control-server-common.xml KAFKA-17587 Refactor test infrastructure (#18602) 2025-01-24 09:03:43 -05:00
import-control-server.xml KAFKA-18728 Move ListOffsetsPartitionStatus to server module (#18807) 2025-02-13 10:36:46 +05:30
import-control-share-coordinator.xml KAFKA-18813: ConsumerGroupHeartbeat API and ConsumerGroupDescribe API must check topic describe (#18989) 2025-02-26 13:05:36 -05:00
import-control-storage.xml KAFKA-14484: Move UnifiedLog static methods to storage (#18039) 2025-02-11 09:55:32 +01:00
import-control-test-common-internal-api.xml KAFKA-17587 Refactor test infrastructure (#18602) 2025-01-24 09:03:43 -05:00
import-control-test-common-runtime.xml KAFKA-17587 Refactor test infrastructure (#18602) 2025-01-24 09:03:43 -05:00
import-control-test-common-util.xml KAFKA-17587 Refactor test infrastructure (#18602) 2025-01-24 09:03:43 -05:00
import-control-transaction-coordinator.xml KAFKA-17587 Refactor test infrastructure (#18602) 2025-01-24 09:03:43 -05:00
import-control.xml KAFKA-17856 Move ConfigCommandTest and ConfigCommandIntegrationTest to tool module (#17767) 2025-03-09 21:05:36 +08:00
java.header MINOR: improve license header check by providing head file instead of (prefix) header regex 2017-02-28 12:35:04 -08:00
suppressions.xml KAFKA-18614, KAFKA-18613: Add streams group request plumbing (#18979) 2025-02-26 16:33:26 +01:00