Skip to content

An inconsistent behaviour in EXPENSIVE_LOOP_INVARIANT_CALL detector #1953

@stoneman258

Description

@stoneman258

I discovered an inconsistency in the EXPENSIVE_LOOP_INVARIANT_CALL detector. In the following code example, Infer reports a warning of this type at line 22, but no warnings at line 23, despite both lines being semantically equivalent. This indicates an inconsistent bug in the detection logic.

import java.util.ArrayList;

public class Main {
    int incr(int x) {
        return x + 1;
    }

    void foo1(ArrayList<Integer> list) {
        int x = 0;
        for (Integer elem : list) {
            incr(x);
        }
    }

    void foo2(ArrayList<Integer> list) {
        int x = 0;
        list.forEach(elem -> incr(x)); 
    }

    void func(int size, ArrayList<Integer> list) {
        for (int i = 0; i < size; i++) {
            foo1(list); // report an EXPENSIVE_LOOP_INVARIANT_CALL warning
            foo2(list); // report no warnigns 
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions