[feature][@CheckReturnValue] Add @lombok.CheckReturnValue to generated methods where the return value should not be ignored.

Adds the annotation to @With, @WithBy, @Builder.build(), and @SuperBuilder.build() methods. Controlled via lombok.addCheckReturnValueAnnotation config key (default: true). Static analysis tools (Error Prone, IntelliJ, SpotBugs) recognize @CheckReturnValue by simple name and will warn when the return value is discarded.
This commit is contained in:
Tim te Beek authored and Reinier Zwitserloot committed 2026-08-21 02:41:59 +02:00
1 parent 277c7bf34a
commit cdfb0f551e
21 files changed
+277 -8

No files matched your search

@@ -0,0 +1,59 @@
class CheckReturnValueBuilder {
private final int x;
private final String name;
@java.lang.SuppressWarnings("all")
@lombok.Generated
CheckReturnValueBuilder(final int x, final String name) {
this.x = x;
this.name = name;
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static class CheckReturnValueBuilderBuilder {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private int x;
@java.lang.SuppressWarnings("all")
@lombok.Generated
private String name;
@java.lang.SuppressWarnings("all")
@lombok.Generated
CheckReturnValueBuilderBuilder() {
}
/**
* @return {@code this}.
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public CheckReturnValueBuilder.CheckReturnValueBuilderBuilder x(final int x) {
this.x = x;
return this;
}
/**
* @return {@code this}.
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public CheckReturnValueBuilder.CheckReturnValueBuilderBuilder name(final String name) {
this.name = name;
return this;
}
@lombok.CheckReturnValue
@java.lang.SuppressWarnings("all")
@lombok.Generated
public CheckReturnValueBuilder build() {
return new CheckReturnValueBuilder(this.x, this.name);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
public java.lang.String toString() {
return "CheckReturnValueBuilder.CheckReturnValueBuilderBuilder(x=" + this.x + ", name=" + this.name + ")";
}
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static CheckReturnValueBuilder.CheckReturnValueBuilderBuilder builder() {
return new CheckReturnValueBuilder.CheckReturnValueBuilderBuilder();
}
}
@@ -0,0 +1,14 @@
class CheckReturnValueOff {
final int x;
CheckReturnValueOff(int x) {
this.x = x;
}
/**
* @return a clone of this object, except with this updated property (returns {@code this} if an identical value is passed).
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public CheckReturnValueOff withX(final int x) {
return this.x == x ? this : new CheckReturnValueOff(x);
}
}
@@ -0,0 +1,26 @@
class CheckReturnValueWith {
final int x;
final String name;
CheckReturnValueWith(int x, String name) {
this.x = x;
this.name = name;
}
/**
* @return a clone of this object, except with this updated property (returns {@code this} if an identical value is passed).
*/
@lombok.CheckReturnValue
@java.lang.SuppressWarnings("all")
@lombok.Generated
public CheckReturnValueWith withX(final int x) {
return this.x == x ? this : new CheckReturnValueWith(x, this.name);
}
/**
* @return a clone of this object, except with this updated property (returns {@code this} if an identical value is passed).
*/
@lombok.CheckReturnValue
@java.lang.SuppressWarnings("all")
@lombok.Generated
public CheckReturnValueWith withName(final String name) {
return this.name == name ? this : new CheckReturnValueWith(this.x, name);
}
}
@@ -0,0 +1,39 @@
@lombok.Builder class CheckReturnValueBuilder {
public static @java.lang.SuppressWarnings("all") @lombok.Generated class CheckReturnValueBuilderBuilder {
private @java.lang.SuppressWarnings("all") @lombok.Generated int x;
private @java.lang.SuppressWarnings("all") @lombok.Generated String name;
@java.lang.SuppressWarnings("all") @lombok.Generated CheckReturnValueBuilderBuilder() {
super();
}
/**
* @return {@code this}.
*/
public @java.lang.SuppressWarnings("all") @lombok.Generated CheckReturnValueBuilder.CheckReturnValueBuilderBuilder x(final int x) {
this.x = x;
return this;
}
/**
* @return {@code this}.
*/
public @java.lang.SuppressWarnings("all") @lombok.Generated CheckReturnValueBuilder.CheckReturnValueBuilderBuilder name(final String name) {
this.name = name;
return this;
}
public @lombok.CheckReturnValue @java.lang.SuppressWarnings("all") @lombok.Generated CheckReturnValueBuilder build() {
return new CheckReturnValueBuilder(this.x, this.name);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") @lombok.Generated java.lang.String toString() {
return (((("CheckReturnValueBuilder.CheckReturnValueBuilderBuilder(x=" + this.x) + ", name=") + this.name) + ")");
}
}
private final int x;
private final String name;
@java.lang.SuppressWarnings("all") @lombok.Generated CheckReturnValueBuilder(final int x, final String name) {
super();
this.x = x;
this.name = name;
}
public static @java.lang.SuppressWarnings("all") @lombok.Generated CheckReturnValueBuilder.CheckReturnValueBuilderBuilder builder() {
return new CheckReturnValueBuilder.CheckReturnValueBuilderBuilder();
}
}
@@ -0,0 +1,14 @@
import lombok.With;
class CheckReturnValueOff {
final @With int x;
CheckReturnValueOff(int x) {
super();
this.x = x;
}
/**
* @return a clone of this object, except with this updated property (returns {@code this} if an identical value is passed).
*/
public @java.lang.SuppressWarnings("all") @lombok.Generated CheckReturnValueOff withX(final int x) {
return ((this.x == x) ? this : new CheckReturnValueOff(x));
}
}
@@ -0,0 +1,22 @@
import lombok.With;
class CheckReturnValueWith {
final @With int x;
final @With String name;
CheckReturnValueWith(int x, String name) {
super();
this.x = x;
this.name = name;
}
/**
* @return a clone of this object, except with this updated property (returns {@code this} if an identical value is passed).
*/
public @lombok.CheckReturnValue @java.lang.SuppressWarnings("all") @lombok.Generated CheckReturnValueWith withX(final int x) {
return ((this.x == x) ? this : new CheckReturnValueWith(x, this.name));
}
/**
* @return a clone of this object, except with this updated property (returns {@code this} if an identical value is passed).
*/
public @lombok.CheckReturnValue @java.lang.SuppressWarnings("all") @lombok.Generated CheckReturnValueWith withName(final String name) {
return ((this.name == name) ? this : new CheckReturnValueWith(this.x, name));
}
}
@@ -0,0 +1,5 @@
@lombok.Builder
class CheckReturnValueBuilder {
private final int x;
private final String name;
}
@@ -0,0 +1,9 @@
//CONF: lombok.addCheckReturnValueAnnotation = false
import lombok.With;
class CheckReturnValueOff {
@With final int x;
CheckReturnValueOff(int x) {
this.x = x;
}
}
@@ -0,0 +1,10 @@
import lombok.With;
class CheckReturnValueWith {
@With final int x;
@With final String name;
CheckReturnValueWith(int x, String name) {
this.x = x;
this.name = name;
}
}