uber / uber/NullAway

Use @Present annotation on the method that is assuredly returning a non-empty optional

Open
#367 5 comments 0 reactions 1 assignee View on GitHub

@shubhamugare is already working on this.

Since Nov 1, 2019.

Dominant language
Java
Stars
4.1k
Forks
370
Avg merge
1d 13h
Merged PRs (30d)
68

Description

Use @Present annotation on the return of the method that is assuredly returning a non-empty optional.
Following test should pass.

  @Test
  public void OptionalEmptinessPresentTest() {
    compilationHelper
        .setArgs(
            Arrays.asList(
                "-d",
                temporaryFolder.getRoot().getAbsolutePath(),
                "-XepOpt:NullAway:AnnotatedPackages=com.uber",
                "-XepOpt:NullAway:UnannotatedSubPackages=com.uber.lib.unannotated",
                "-XepOpt:NullAway:CheckOptionalEmptiness=true"))
        .addSourceLines(
            "TestNegative.java",
            "package com.uber;",
            "import com.google.common.base.Optional;",
            "rg.checkerframework.checker.optional.qual.Present",
            "import com.google.common.base.Function;",
            "public class TestNegative {",
            "  class ABC { ",
            "       @present Optional<Object> getTheOptional(int x){",
            "           if(x != 0){",
            "             return Optional.of(new Object());",
            "           }",
            "       return Optional.of(new Object());",
            "       }",
            "  }",
            "  void foo() {",
            "      ABC abc = new ABC();",
            "      // no error since @present annotation on the invoked method",
            "       abc.getTheOptional(5).get().toString();",
            "      }",
            "    }",
            "}")
        .doTest();
  }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.