treat guava preconditions checks as assume instead of exeption throwing assertions

Summary:
public
The case when a resource leaks is reported because the the resource was not closed on the execution branch created by the preconditions checks are not very interesting in practice because the exceptions thrown, either `NullPointerException` or `IllegalStateException` are very rarely caught anyway. So the legimate use of preconditions checks is creating spurious resource leak reports.

Reviewed By: sblackshear

Differential Revision: D2707227

fb-gh-sync-id: 6aece73
master
jrm 9 years ago committed by facebook-github-bot-5
parent a774f5e733
commit 5a218a6d02

@ -10,53 +10,39 @@
package com.google.common.base;
import javax.annotation.Nullable;
import static com.facebook.infer.models.InferBuiltins.assume;
public final class Preconditions {
public static <T> T checkNotNull(T reference) {
if (reference == null) {
throw new NullPointerException();
}
assume(reference != null);
return reference;
}
public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) {
if (reference == null) {
throw new NullPointerException();
}
return reference;
return checkNotNull(reference);
}
public static <T> T checkNotNull(T reference,
@Nullable String errorMessageTemplate,
@Nullable Object... errorMessageArgs) {
if (reference == null) {
// If either of these parameters is null, the right thing happens anyway
throw new NullPointerException(errorMessageTemplate);
}
return reference;
return checkNotNull(reference);
}
public static void checkState(boolean expression) {
if (!expression) {
throw new IllegalStateException();
}
assume(expression);
}
public static void checkState(boolean expression,
@Nullable Object errorMessage) {
if (!expression) {
throw new IllegalStateException();
}
assume(expression);
}
public static void checkState(boolean expression,
@Nullable String errorMessageTemplate,
@Nullable Object... errorMessageArgs) {
if (!expression) {
throw new IllegalStateException(errorMessageTemplate);
}
assume(expression);
}
}

@ -463,7 +463,7 @@ public class NullPointerExceptions {
try {
FileLock lock = chan.tryLock();
return (lock != null ? lock.toString() : "");
} catch (IOException _) {
} catch (IOException e) {
Object o = null;
return o.toString(); // expect NullPointerException as tryLock can throw
}

@ -932,4 +932,10 @@ public class ResourceLeaks {
decomp.end();
}
void NoResourceLeakWarningAfterCheckState(File f, int x) throws IOException {
InputStream stream = new FileInputStream(f);
Preconditions.checkState(x > 0);
stream.close();
}
}

Loading…
Cancel
Save