From a0e3314b7b858d9bac4797aa7e633d9f51bb6680 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Tue, 22 May 2018 09:58:21 -0700 Subject: [PATCH] Eradicate: add a test for condition-redundant check Reviewed By: jeremydubreil, jvillard Differential Revision: D8095121 fbshipit-source-id: ad73cc4 --- .../codetoanalyze/java/eradicate/Makefile | 7 +++- .../java/eradicate/Redundant.java | 32 +++++++++++++++++++ .../codetoanalyze/java/eradicate/issues.exp | 1 + 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 infer/tests/codetoanalyze/java/eradicate/Redundant.java diff --git a/infer/tests/codetoanalyze/java/eradicate/Makefile b/infer/tests/codetoanalyze/java/eradicate/Makefile index 3baa3f173..459b819b6 100644 --- a/infer/tests/codetoanalyze/java/eradicate/Makefile +++ b/infer/tests/codetoanalyze/java/eradicate/Makefile @@ -8,7 +8,12 @@ TESTS_DIR = ../../.. ANALYZER = checkers -INFER_OPTIONS = --eradicate-only --eradicate-return-over-annotated --eradicate-optional-present --debug-exceptions +INFER_OPTIONS = \ + --eradicate-only \ + --eradicate-return-over-annotated \ + --eradicate-optional-present \ + --eradicate-condition-redundant \ + --debug-exceptions INFERPRINT_OPTIONS = --issues-tests SOURCES = $(wildcard *.java) diff --git a/infer/tests/codetoanalyze/java/eradicate/Redundant.java b/infer/tests/codetoanalyze/java/eradicate/Redundant.java new file mode 100644 index 000000000..88dfeb643 --- /dev/null +++ b/infer/tests/codetoanalyze/java/eradicate/Redundant.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2014 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ +public class Redundant { + + void bad() { + String s = "123"; + if (s != null) { + int n = s.length(); + } + } + + static void maythrow() throws java.io.IOException {} + + void good() throws java.io.IOException { + String s = null; + + try { + maythrow(); + s = "123"; + } finally { + if (s != null) { // this is not redundant + int n = s.length(); + } + } + } +} diff --git a/infer/tests/codetoanalyze/java/eradicate/issues.exp b/infer/tests/codetoanalyze/java/eradicate/issues.exp index 16c80986d..3a25032a4 100644 --- a/infer/tests/codetoanalyze/java/eradicate/issues.exp +++ b/infer/tests/codetoanalyze/java/eradicate/issues.exp @@ -69,6 +69,7 @@ codetoanalyze/java/eradicate/PresentTest.java, Optional PresentTest$TestPresentA codetoanalyze/java/eradicate/PresentTest.java, Optional PresentTest$TestPresentAnnotationBasic.returnPresentBad(), 1, ERADICATE_VALUE_NOT_PRESENT, ERROR, [origin,The value of `PresentTest$TestPresentAnnotationBasic.absent` in the call to `get()` is not `@Present`. (Origin: field PresentTest$TestPresentAnnotationBasic.absent at line 47)] codetoanalyze/java/eradicate/PresentTest.java, void PresentTest$TestPresentAnnotationBasic.testOptionalAbsent(), 1, ERADICATE_PARAMETER_VALUE_ABSENT, ERROR, [origin,`PresentTest$TestPresentAnnotationBasic.expectPresent(...)` needs a present value in parameter 1 but argument `absent()` can be absent. (Origin: call to absent() at line 65)] codetoanalyze/java/eradicate/PresentTest.java, void PresentTest.testPresent(Optional,Optional), 4, ERADICATE_PARAMETER_VALUE_ABSENT, ERROR, [`PresentTest.argPresent(...)` needs a present value in parameter 1 but argument `absent` can be absent. (Origin: method parameter absent)] +codetoanalyze/java/eradicate/Redundant.java, void Redundant.bad(), 2, ERADICATE_CONDITION_REDUNDANT, ERROR, [The condition s is always true according to the existing annotations.] codetoanalyze/java/eradicate/ReturnNotNullable.java, Object ReturnNotNullable$ConditionalAssignment.test(boolean), 0, ERADICATE_RETURN_NOT_NULLABLE, ERROR, [origin,Method `test(...)` may return null but it is not annotated with `@Nullable`. (Origin: field ReturnNotNullable$ConditionalAssignment.f1 at line 147)] codetoanalyze/java/eradicate/ReturnNotNullable.java, Object ReturnNotNullable.tryWithResourcesReturnNullable(String), 0, ERADICATE_RETURN_NOT_NULLABLE, ERROR, [origin,Method `tryWithResourcesReturnNullable(...)` may return null but it is not annotated with `@Nullable`. (Origin: call to returnNullOK() at line 92)] codetoanalyze/java/eradicate/ReturnNotNullable.java, String ReturnNotNullable.redundantEq(), 0, ERADICATE_RETURN_OVER_ANNOTATED, ERROR, [Method `redundantEq()` is annotated with `@Nullable` but never returns null.]