diff --git a/infer/src/cost/costModels.ml b/infer/src/cost/costModels.ml index 8134445ce..0fab779b0 100644 --- a/infer/src/cost/costModels.ml +++ b/infer/src/cost/costModels.ml @@ -279,6 +279,9 @@ module Call = struct ; +PatternMatch.Java.implements_list &:: "contains" <>$ capt_exp $+...$--> BoundsOfCollection.linear_length ~of_function:"List.contains" + ; +PatternMatch.Java.implements_collection + &:: "contains" <>$ capt_exp + $+...$--> BoundsOfCollection.linear_length ~of_function:"Collection.contains" ; +PatternMatch.Java.implements_collections &:: "binarySearch" <>$ capt_exp $+...$--> BoundsOfCollection.logarithmic_length ~of_function:"Collections.binarySearch" diff --git a/infer/tests/codetoanalyze/java/performance/CollectionTest.java b/infer/tests/codetoanalyze/java/performance/CollectionTest.java index 1e32a7d15..9f8bfc061 100644 --- a/infer/tests/codetoanalyze/java/performance/CollectionTest.java +++ b/infer/tests/codetoanalyze/java/performance/CollectionTest.java @@ -8,6 +8,7 @@ import android.util.SparseArray; import com.google.common.collect.ImmutableSet; import java.util.Collection; +import java.util.HashMap; import java.util.concurrent.ConcurrentLinkedQueue; public class CollectionTest { @@ -102,4 +103,8 @@ public class CollectionTest { ImmutableSet set = ImmutableSet.of(1, 2, 3, 4, 5); for (int i = 0; i < set.size(); i++) {} } + + void containsNull_linear(HashMap keyMap) { + keyMap.values().contains(null); + } } diff --git a/infer/tests/codetoanalyze/java/performance/cost-issues.exp b/infer/tests/codetoanalyze/java/performance/cost-issues.exp index ea096e805..94bf50b90 100644 --- a/infer/tests/codetoanalyze/java/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/java/performance/cost-issues.exp @@ -99,6 +99,7 @@ codetoanalyze/java/performance/CollectionTest.java, CollectionTest$MyEnumType.(), 2, OnUIThread:false, [] +codetoanalyze/java/performance/CollectionTest.java, CollectionTest.containsNull_linear(java.util.HashMap):void, 5 + keyMap.length, OnUIThread:false, [{keyMap.length},Modeled call to Collection.contains] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.ensure_call(CollectionTest$MyCollection):void, 9 + 5 ⋅ list.length, OnUIThread:false, [{list.length},Call to void CollectionTest.iterate_over_mycollection(CollectionTest$MyCollection),Loop] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.immutable_set_of_constant():void, 9, OnUIThread:false, [] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.immutable_set_of_multiple_constant():void, 64, OnUIThread:false, []