Closed
Description
by inferring nullability from the 2nd parameter
example test:
makeTestHelperWithArgs(
Arrays.asList(
"-d",
temporaryFolder.getRoot().getAbsolutePath(),
"-XepOpt:NullAway:AnnotatedPackages=com.uber",
"-XepOpt:NullAway:AcknowledgeRestrictiveAnnotations=true"))
.addSourceLines(
"Test.java",
"package com.uber;",
"import java.util.Map;",
"import com.google.common.collect.ImmutableMap;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" void testGetOrDefaultMap(Map<String, String> m, String string, @Nullable String nullableString) {",
" m.getOrDefault(\"key\", \"value\").toString();",
" m.getOrDefault(\"key\", string).toString();",
" m.getOrDefault(\"key\", null).toString();",
" m.getOrDefault(\"key\", nullableString).toString();",
" }",
"",
" void testGetOrDefaultImmutableMap(ImmutableMap<String, String> im, String string, @Nullable String nullableString) {",
" // BUG: Diagnostic contains:",
" im.getOrDefault(\"key\", \"value\").toString();",
" // BUG: Diagnostic contains:",
" im.getOrDefault(\"key\", string).toString();",
" // BUG: Diagnostic contains:",
" im.getOrDefault(\"key\", null).toString();",
" // BUG: Diagnostic contains:",
" im.getOrDefault(\"key\", nullableString).toString();",
" }",
"}")
.doTest();
Map.getOrDefault seems to always get treated as returning non-null type
ImmutableMap.getOrDefault is treated as always returning a nullable type (due to its annotations and AcknowledgeRestrictiveAnnotations)
imo both should be considered non-null in the first 2 cases and nullable in the last 2 cases, WDYT?
hit this while trying to improve NullAway code in https://github.com/uber/NullAway/actions/runs/4016591896/jobs/6899826587 😅
Metadata
Metadata
Assignees
Labels
No labels
Activity
msridhar commentedon Jan 27, 2023
I think we want to add
Map.getOrDefaultto this Map (with parameter index 1):https://github.com/uber/NullAway/blob/0a78a82df792014cb10320c4de0253f4e1ec7606/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java#L613
Not 100% sure, but I think that modeling would override the annotations on
ImmutableMap.getOrDefault. Does that sound right @lazaroclapp?lazaroclapp commentedon Jan 27, 2023
Worth testing, specially for the
ImmutableMapcase, but the idea makes sense to me and we would expect library models to override third party annotations (Not sure if anything gets weird with inheritance. Worst case we could add explicit models for both, but I am hoping a model forMapis enough).msridhar commentedon Jan 27, 2023
I tried in #724 and it works for
Mapbut notImmutableMap. Need to understand better whether an inherited library model should win over given annotations with restrictive annotations enabled 😅XN137 commentedon Jan 27, 2023
https://github.com/google/guava/blob/76cd7fa14a567bc74e4a98052de55f4486a8c933/guava/src/com/google/common/collect/ImmutableMap.java#L975
comments in the code discuss these annotations, if i understand this part correctly:
then maybe it suggests that tools are allowed to be smarter than the current annotations.
so i guess it should be implemented in a way which overrides the annotations for
ImmutableMap.getOrDefaultexplicitly, even whenAcknowledgeRestrictiveAnnotationsis set.Also since we're trying to follow the contract of the base interface.
msridhar commentedon Jan 30, 2023
In #724 I added an explicit model for
ImmutableMap.get()and it seemed to work. I think I'm ok with that approach for now. We can move discussion over there.Model `Map.getOrDefault` (#724)