Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Treat effectively final variables the same as static final fields when reasoning about "constant" function calls #1318

@wimdeblauwe

Description

@wimdeblauwe

I am trying to add NullAway to my project and I have ran into what I think is a false positive.

I tried to make it as small as possible and this shows the problem:

import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        String httpStatusValue = "200";
        Map<String, String> map = new HashMap<>();
        if (map.get(someOperation()) != null) {
            staticMethod(map.get(someOperation()));
        } else if (map.get(someOtherOperation(httpStatusValue)) != null) {
            staticMethod(map.get(someOtherOperation(httpStatusValue)));
        }
    }

    private static String someOperation() {
        return "test";
    }

    private static String someOtherOperation(String httpStatusValue) {
        return "test2";
    }

    private static void staticMethod(String t) {
        String trimmed = t.trim();
        System.out.println("trimmed = " + trimmed);
    }
}

Running NullAway on this code gives:

 error: [NullAway] passing @Nullable parameter 'map.get(someOtherOperation(httpStatusValue))' where @NonNull is required

Note that is only reports the problem for the someOtherOperation that has a parameter. The other method that has no parameter is ok.

I am using @NullMarked on the package that this class is in, and run with these settings -Xplugin:ErrorProne -Xep:NullAway:ERROR -XepOpt:NullAway:OnlyNullMarked.

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