Why this matters
Failing to use try-with-resources can lead to resource leaks, as manually closing resources is error-prone and harder to maintain.
Ensure that resources such as streams or file handlers are managed using try-with-resources to avoid leaks.
Failing to use try-with-resources can lead to resource leaks, as manually closing resources is error-prone and harder to maintain.
Side-by-side examples engineers can pattern-match during review.
FileInputStream fis = new FileInputStream("file.txt");
try {
// Read file
} finally {
fis.close();
}try (FileInputStream fis = new FileInputStream("file.txt")) {
// Read file
}FileInputStream fis = new FileInputStream("file.txt");
try {
// Read file
} finally {
fis.close();
}try (FileInputStream fis = new FileInputStream("file.txt")) {
// Read file
}From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.