Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @CanIgnoreReturnValue annotations #868

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions caffeine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ tasks.named('compileJava').configure {
dependsOn generateLocalCaches, generateNodes
finalizedBy compileCodeGenJava
options.errorprone.disable('CheckReturnValue')
options.errorprone.error('CanIgnoreReturnValueSuggester')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might go into codeQuality.gradle?

options.errorprone {
def enabledChecks = [

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I missed that. Lemme try!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way it also flags some test code. That's fine; will add the annotations there too.

}
tasks.named('compileTestJava').configure {
dependsOn jar, compileCodeGenJava
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.github.benmanes.caffeine.cache.stats.ConcurrentStatsCounter;
import com.github.benmanes.caffeine.cache.stats.StatsCounter;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.FormatMethod;

/**
Expand Down Expand Up @@ -291,6 +292,7 @@ public static Caffeine<Object, Object> from(String spec) {
* @throws IllegalArgumentException if {@code initialCapacity} is negative
* @throws IllegalStateException if an initial capacity was already set
*/
@CanIgnoreReturnValue
public Caffeine<K, V> initialCapacity(@NonNegative int initialCapacity) {
requireState(this.initialCapacity == UNSET_INT,
"initial capacity was already set to %s", this.initialCapacity);
Expand Down Expand Up @@ -324,6 +326,7 @@ int getInitialCapacity() {
* @return this {@code Caffeine} instance (for chaining)
* @throws NullPointerException if the specified executor is null
*/
@CanIgnoreReturnValue
public Caffeine<K, V> executor(Executor executor) {
requireState(this.executor == null, "executor was already set to %s", this.executor);
this.executor = requireNonNull(executor);
Expand Down Expand Up @@ -355,6 +358,7 @@ Executor getExecutor() {
* @return this {@code Caffeine} instance (for chaining)
* @throws NullPointerException if the specified scheduler is null
*/
@CanIgnoreReturnValue
public Caffeine<K, V> scheduler(Scheduler scheduler) {
requireState(this.scheduler == null, "scheduler was already set to %s", this.scheduler);
this.scheduler = requireNonNull(scheduler);
Expand Down Expand Up @@ -389,6 +393,7 @@ Scheduler getScheduler() {
* @throws IllegalArgumentException if {@code size} is negative
* @throws IllegalStateException if a maximum size or weight was already set
*/
@CanIgnoreReturnValue
public Caffeine<K, V> maximumSize(@NonNegative long maximumSize) {
requireState(this.maximumSize == UNSET_INT,
"maximum size was already set to %s", this.maximumSize);
Expand Down Expand Up @@ -425,6 +430,7 @@ public Caffeine<K, V> maximumSize(@NonNegative long maximumSize) {
* @throws IllegalArgumentException if {@code maximumWeight} is negative
* @throws IllegalStateException if a maximum weight or size was already set
*/
@CanIgnoreReturnValue
public Caffeine<K, V> maximumWeight(@NonNegative long maximumWeight) {
requireState(this.maximumWeight == UNSET_INT,
"maximum weight was already set to %s", this.maximumWeight);
Expand Down Expand Up @@ -517,6 +523,7 @@ <K1 extends K, V1 extends V> Weigher<K1, V1> getWeigher(boolean isAsync) {
* @return this {@code Caffeine} instance (for chaining)
* @throws IllegalStateException if the key strength was already set
*/
@CanIgnoreReturnValue
public Caffeine<K, V> weakKeys() {
requireState(keyStrength == null, "Key strength was already set to %s", keyStrength);
keyStrength = Strength.WEAK;
Expand Down Expand Up @@ -546,6 +553,7 @@ boolean isStrongKeys() {
* @return this {@code Caffeine} instance (for chaining)
* @throws IllegalStateException if the value strength was already set
*/
@CanIgnoreReturnValue
public Caffeine<K, V> weakValues() {
requireState(valueStrength == null, "Value strength was already set to %s", valueStrength);
valueStrength = Strength.WEAK;
Expand Down Expand Up @@ -582,6 +590,7 @@ boolean isWeakValues() {
* @return this {@code Caffeine} instance (for chaining)
* @throws IllegalStateException if the value strength was already set
*/
@CanIgnoreReturnValue
public Caffeine<K, V> softValues() {
requireState(valueStrength == null, "Value strength was already set to %s", valueStrength);
valueStrength = Strength.SOFT;
Expand All @@ -604,6 +613,7 @@ public Caffeine<K, V> softValues() {
* @throws IllegalStateException if the time to live or variable expiration was already set
* @throws ArithmeticException for durations greater than +/- approximately 292 years
*/
@CanIgnoreReturnValue
public Caffeine<K, V> expireAfterWrite(Duration duration) {
return expireAfterWrite(saturatedToNanos(duration), TimeUnit.NANOSECONDS);
}
Expand All @@ -627,6 +637,7 @@ public Caffeine<K, V> expireAfterWrite(Duration duration) {
* @throws IllegalArgumentException if {@code duration} is negative
* @throws IllegalStateException if the time to live or variable expiration was already set
*/
@CanIgnoreReturnValue
public Caffeine<K, V> expireAfterWrite(@NonNegative long duration, TimeUnit unit) {
requireState(expireAfterWriteNanos == UNSET_INT,
"expireAfterWrite was already set to %s ns", expireAfterWriteNanos);
Expand Down Expand Up @@ -663,6 +674,7 @@ boolean expiresAfterWrite() {
* @throws IllegalStateException if the time to idle or variable expiration was already set
* @throws ArithmeticException for durations greater than +/- approximately 292 years
*/
@CanIgnoreReturnValue
public Caffeine<K, V> expireAfterAccess(Duration duration) {
return expireAfterAccess(saturatedToNanos(duration), TimeUnit.NANOSECONDS);
}
Expand All @@ -689,6 +701,7 @@ public Caffeine<K, V> expireAfterAccess(Duration duration) {
* @throws IllegalArgumentException if {@code duration} is negative
* @throws IllegalStateException if the time to idle or variable expiration was already set
*/
@CanIgnoreReturnValue
public Caffeine<K, V> expireAfterAccess(@NonNegative long duration, TimeUnit unit) {
requireState(expireAfterAccessNanos == UNSET_INT,
"expireAfterAccess was already set to %s ns", expireAfterAccessNanos);
Expand Down Expand Up @@ -779,6 +792,7 @@ boolean expiresVariable() {
* @throws IllegalStateException if the refresh interval was already set
* @throws ArithmeticException for durations greater than +/- approximately 292 years
*/
@CanIgnoreReturnValue
public Caffeine<K, V> refreshAfterWrite(Duration duration) {
return refreshAfterWrite(saturatedToNanos(duration), TimeUnit.NANOSECONDS);
}
Expand Down Expand Up @@ -806,6 +820,7 @@ public Caffeine<K, V> refreshAfterWrite(Duration duration) {
* @throws IllegalArgumentException if {@code duration} is zero or negative
* @throws IllegalStateException if the refresh interval was already set
*/
@CanIgnoreReturnValue
public Caffeine<K, V> refreshAfterWrite(@NonNegative long duration, TimeUnit unit) {
requireNonNull(unit);
requireState(refreshAfterWriteNanos == UNSET_INT,
Expand Down Expand Up @@ -835,6 +850,7 @@ boolean refreshAfterWrite() {
* @throws IllegalStateException if a ticker was already set
* @throws NullPointerException if the specified ticker is null
*/
@CanIgnoreReturnValue
public Caffeine<K, V> ticker(Ticker ticker) {
requireState(this.ticker == null, "Ticker was already set to %s", this.ticker);
this.ticker = requireNonNull(ticker);
Expand Down Expand Up @@ -961,6 +977,7 @@ public <K1 extends K, V1 extends V> Caffeine<K1, V1> removalListener(
*
* @return this {@code Caffeine} instance (for chaining)
*/
@CanIgnoreReturnValue
public Caffeine<K, V> recordStats() {
requireState(this.statsCounterSupplier == null, "Statistics recording was already set");
statsCounterSupplier = ENABLED_STATS_COUNTER_SUPPLIER;
Expand All @@ -977,6 +994,7 @@ public Caffeine<K, V> recordStats() {
* @param statsCounterSupplier a supplier instance that returns a new {@link StatsCounter}
* @return this {@code Caffeine} instance (for chaining)
*/
@CanIgnoreReturnValue
public Caffeine<K, V> recordStats(Supplier<? extends StatsCounter> statsCounterSupplier) {
requireState(this.statsCounterSupplier == null, "Statistics recording was already set");
requireNonNull(statsCounterSupplier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import io.reactivex.subjects.PublishSubject;

import com.google.errorprone.annotations.CanIgnoreReturnValue;

/**
* This class allows a cache to have "write-behind" semantics. The passed in writeAction will only
* be called every 'bufferTime' time with a Map containing the keys and the values that have been
Expand Down Expand Up @@ -67,18 +69,21 @@ public static final class Builder<K, V> {
* The duration that the calls to the cache should be buffered before calling the
* <code>writeAction</code>.
*/
@CanIgnoreReturnValue
public Builder<K, V> bufferTime(long duration, TimeUnit unit) {
this.bufferTimeNanos = TimeUnit.NANOSECONDS.convert(duration, unit);
return this;
}

/** The callback to perform the writing to the database or repository. */
@CanIgnoreReturnValue
public Builder<K, V> writeAction(Consumer<Map<K, V>> writeAction) {
this.writeAction = requireNonNull(writeAction);
return this;
}

/** The action that decides which value to take in case a key was updated multiple times. */
@CanIgnoreReturnValue
public Builder<K, V> coalesce(BinaryOperator<V> coalescer) {
this.coalescer = requireNonNull(coalescer);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ExecutionError;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

/**
* A Caffeine-backed cache through a Guava facade.
Expand Down Expand Up @@ -240,6 +241,7 @@ static final class CacheLoaderException extends RuntimeException {
CacheLoaderException(Exception e) {
super(e);
}
@CanIgnoreReturnValue
@SuppressWarnings({"lgtm [java/non-sync-override]", "UnsynchronizedOverridesSynchronized"})
@Override public Throwable fillInStackTrace() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.auto.value.AutoValue.CopyAnnotations;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

/**
* Statistics gathered by a policy execution. A policy can extend this class as a convenient way to
Expand Down Expand Up @@ -318,6 +319,7 @@ public abstract static class Builder {
public abstract ImmutableSet.Builder<Characteristic> characteristicsBuilder();
public abstract Metric build();

@CanIgnoreReturnValue
public final Builder addCharacteristic(Characteristic characteristic) {
characteristicsBuilder().add(characteristic);
return this;
Expand Down