Feature request: detect misuse (storing) of "short living" values
- Dominant language
- Java
- Stars
- 7.2k
- Forks
- 820
- Avg merge
- 5h 9m
- Merged PRs (30d)
- 50
Description
### Description of the feature request:
I'd like to request for a check that detects (and warns) when my code stores values that are declared as "short living" values.
### Feature requests: what underlying problem are you trying to solve with this feature?
Sometimes its useful to use a very naive, lifecycle-less "pool" of objects that are meant to be short living.
As they are short living (i.e. all meant to be used in a single expression), you can avoid a regular pool lifecycle management (obtain/release), and just return the LRU object for each request (this works as long as the number of "parallel" requests does not exceed the "pool" size).
For example, see this implementation of [Unbox.getSb()](https://github.com/apache/logging-log4j2/blob/6027bae51f85c9fc3415d404e4758dd0ccad0735/log4j-api/src/main/java/org/apache/logging/log4j/util/Unbox.java#L265) that returns a `StringBuilder` object.
My problem is that when using this pattern, it's difficult to track bugs around misusing such code and storing the returned object for later use.
If possible/applicable, an annotation like `@ShortLives` on a method can be useful in signaling that the return value of such methods is meant to be a "short living" value and (usually) should not be stored.
### What version of Error Prone are you using?
2.5.1
### Have you found anything relevant by searching the web?
Not really.
Contributor guide
Assessment
This issue has not been assessed yet.