Skip to content

Model java.util.Map.remove(Object) return value as @Nullable, similar to Map.get() #1591

Description

@magicghostVu

Description

NullAway currently warns for unsafe dereference of Map.get(...), since Map.get(...) may return null when no mapping exists.

However, java.util.Map.remove(Object key) has a very similar return contract: it returns the previous value associated with the key, or null if there was no mapping.

Currently, NullAway appears not to warn when dereferencing the result of Map.remove(...), which can lead to a false negative.

Minimal repro

package com.example;

import java.util.Map;

class Test {
  void removeShouldBeNullable(Map<String, Object> map) {
    map.remove("x").toString(); // Expected NullAway warning, but no warning currently
  }

  void getIsAlreadyNullable(Map<String, Object> map) {
    map.get("x").toString(); // NullAway warns
  }
}

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