When is an IntStream actually closed? Is SonarQube S2095 a false positive for IntStream?

后端 未结 1 1592
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 11:47

I am using Java 8 streams in place of many old style for loops to iterate through a bunch of results and produce summary statistics. For example:

int message         


        
相关标签:
1条回答
  • 2020-12-06 12:27

    It isn't closed, because AutoCloseable interface works only inside try-with-resources. But this close operation is totally unnecessary for IntStream as it said in AutoCloseable interface javadoc:

    However, when using facilities such as java.util.stream.Stream that support both I/O-based and non-I/O-based forms, try-with-resources blocks are in general unnecessary when using non-I/O-based forms.

    So yes S2095 is a false positive for IntStream. That will be hopefully fixed by SONARJAVA-1478

    0 讨论(0)
提交回复
热议问题