checker-framework
checker-framework copied to clipboard
Suppress RLC warnings for safe first-time non-final field overwrites in constructors
The Resource Leak Checker currently flags warnings for field assignments in constructors, even when the constructor is clearly performing the first and only write to that field. This results in false positives for common patterns.
A warning should be suppressed when all of the following are true:
- The field is private
- It has no non-null inline initializer
- It’s not assigned in any instance initializer block
- The constructor doesn't call another constructor using this(...)
- The assignment is directly in the constructor
- There are no method calls (other than super) or earlier assignments to the same field before this one in the constructor body
Based on the JLS:
- Field initializers and instance initializer blocks run first
- Then the superclass constructor
- Then the constructor body runs So if a private field is only written in the constructor body (and no earlier initialization happened), that assignment is guaranteed to be the first one.