spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Allow usage of `@CheckReturnValue` on types and constructors

Open odrotbohm opened this issue 6 months ago • 5 comments

@CheckReturnValue currently only targets ElementType.METHOD. IDEs, however, also consider a type-level @CheckReturnValue to express that all methods' return values should be checked. I would appreciate it if our annotation could be used that way, too.

It would also make sense to allow the usage of the annotation on constructors and packages, the latter implying all methods in the package having to be checked.

odrotbohm avatar May 16 '25 11:05 odrotbohm

I can see the value of adding it on types, for example for some builder style APIs, so I will add related target.

For other targets, I am not convinced for now we should support them.

While we support setting nullability at package level (default that makes sense for most cases), for @CheckReturnValue the use cases where that make sense looks much less frequent to me, could be surprising (you need to check package-info.java to know if this is enabled package wide) and our variant does not have (on purpose) a when attribute like the original JSR 305 variant to unset the package-level default.

For constructor, I have not been able to observe a concrete effect when setting it, IDEA gives me no warning, and I am unsure about the use case, so I prefer not adding it until we understand the purpose and use case.

I will target Spring Framework 7 milestones in order to get a chance to have feedback and potentially refine accordingly.

sdeleuze avatar Jun 11 '25 07:06 sdeleuze

Hum, while doing more tests, I saw that type level @CheckReturnValue is not recognized by IntelliJ IDEA, so I prefer to dicuss that point with them before introducing it.

sdeleuze avatar Jun 11 '25 07:06 sdeleuze

Hm, interesting. I could've sworn I had seen this working with the custom annotation I have defined in Spring Modulith. Turns out you're right. I guess I had derived it works from the type level due to JSR-305's @CheckReturnValue being declared with targets as proposed.

It looks like folks had asked for this in IDEA already, but the ticket got closed without comment.

odrotbohm avatar Jun 13 '25 10:06 odrotbohm

I have created https://youtrack.jetbrains.com/issue/IDEA-374480/Support-CheckReturnValue-on-type-constructor-and-packages and brought that issue to the attention of the JetBrains team.

sdeleuze avatar Jun 13 '25 13:06 sdeleuze

IDEA-374480 has been fixed, so let's add type and constructor target to @CheckReturnValue.

sdeleuze avatar Jun 16 '25 13:06 sdeleuze