-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
None
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
Now, in 2024, is common to use builders that not only create some object, but also make it do its job, like:
```StepVerifier.create(xxx).expectNext(aaa).expectNext(bbb).expectComplete().verify();```
The above code is a test for a Reactor's Flux.
Unfortunately, if you forget to specify the last directive, ```verify()``` in this case, nothing will happen.
What's worse, the test will succeed, because if a test does nothing, it succeeds.
So the problem is:
Actual: a test that contains a "builder-discarded" bug succeeds
Expected: a test that contains a "builder-discarded" bug must not compile
Proposed solution:
An annotation, e.g. @NotVoidable (the name is preliminary, it may be @NoDiscard, @NotDiscardable or whatever), that makes the compiler report an error if the value is discarded.
When used on a method, ```@NoDiscard Y xyz() { ... }```, ignoring the value returned by this method will be a compilation error.
When used on a class or interface, ```@NoDiscard class X { }```, ignoring any value of this class will be a compilation error.
I do not see a real use-case, but we may need something like ```Objects.void(xyz())``` to allow explicit discarding for values that must not be ignored; it's a pity that ```(void) xyz()``` will not work; ```Objects.nonNull(xyz())``` will work, but it's a hack.
This way,
```StepVerifier.create(xxx).expectNext(aaa).expectNext(bbb).expectComplete();```
may result in a compilation error.
Now, in 2024, is common to use builders that not only create some object, but also make it do its job, like:
```StepVerifier.create(xxx).expectNext(aaa).expectNext(bbb).expectComplete().verify();```
The above code is a test for a Reactor's Flux.
Unfortunately, if you forget to specify the last directive, ```verify()``` in this case, nothing will happen.
What's worse, the test will succeed, because if a test does nothing, it succeeds.
So the problem is:
Actual: a test that contains a "builder-discarded" bug succeeds
Expected: a test that contains a "builder-discarded" bug must not compile
Proposed solution:
An annotation, e.g. @NotVoidable (the name is preliminary, it may be @NoDiscard, @NotDiscardable or whatever), that makes the compiler report an error if the value is discarded.
When used on a method, ```@NoDiscard Y xyz() { ... }```, ignoring the value returned by this method will be a compilation error.
When used on a class or interface, ```@NoDiscard class X { }```, ignoring any value of this class will be a compilation error.
I do not see a real use-case, but we may need something like ```Objects.void(xyz())``` to allow explicit discarding for values that must not be ignored; it's a pity that ```(void) xyz()``` will not work; ```Objects.nonNull(xyz())``` will work, but it's a hack.
This way,
```StepVerifier.create(xxx).expectNext(aaa).expectNext(bbb).expectComplete();```
may result in a compilation error.