Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @Nullable annotations to Arg and subtypes #97

Conversation

carterkozak
Copy link
Contributor

jsr305 is a compile-only dependency in order to provide a minimal
footprint while allowing validation in projects which provide the
dependency.

jsr305 is a compile-only dependency in order to provide a minimal
footprint while allowing validation in projects which provide the
dependency.
@carterkozak carterkozak requested a review from a team as a code owner December 18, 2018 16:36
@carterkozak
Copy link
Contributor Author

See palantir/tritium#152

@schlosna
Copy link
Contributor

@cakofony I forgot to mention I had created develop...schlosna:ds/null-away but didn't create a PR. Interestingly the checks flagged something in the Arg#equals implementation develop...schlosna:ds/null-away#diff-bedf3441b6244c252d554a0b96b74860R67 as well as LoggableExceptionAssert that in practice won't actually NPE, but we can probably cleanup to make it more clear and have the compiler & checks prove non-nullness

bulldozer-bot bot pushed a commit that referenced this pull request Jan 7, 2019
<!-- PR title should start with '[fix]', '[improvement]' or '[break]' if this PR would cause a patch, minor or major SemVer bump. Omit the prefix if this PR doesn't warrant a standalone release. -->

## Before this PR
<!-- Describe the problem you encountered with the current state of the world (or link to an issue) and why it's important to fix now. -->

The safe-logging library did not explicitly annotate `Arg` and its subtypes, so consumers of this library would receive false-positive warnings when using static analysis checks such as [NullAway](https://github.com/uber/NullAway) or IntelliJ nullability checks, for example the below as seen in palantir/tritium#152 where [use of `UnsafeArg.of` with a `@Nullable` value](https://github.com/palantir/tritium/blob/develop/tritium-core/src/main/java/com/palantir/tritium/event/CompositeInvocationEventHandler.java#L142) is mistakenly flagged:

```
warning: [NullAway] passing @nullable parameter 'context' where @nonnull is required
                    UnsafeArg.of("context", context),
                                            ^
```

## After this PR
<!-- Describe at a high-level why this approach is better. -->

Enables [NullAway](https://github.com/uber/NullAway) gradle build checks and appropriately annotates Arg and its subtypes to support proper nullability analysis.

<!-- Reference any existing GitHub issues, e.g. 'fixes #000' or 'relevant to #000' -->

Relevant to palantir/tritium#152 and supersedes #97

The NullAway checks identified one possible issue in safe-logging's `LoggableExceptionAssert`, and this has been corrected.

* Handle null LoggableExceptionAssert arg, addresses NullAway warnings:

```
> Task :preconditions-assertj:compileJava
/Volumes/git/safe-logging/preconditions-assertj/src/main/java/com/palantir/logsafe/testing/LoggableExceptionAssert.java:30: warning: [NullAway] initializer method does not guarantee @nonnull field argsAssert is initialized along all control-flow paths (remember to check for exceptions or early returns).
    public LoggableExceptionAssert(T actual) {
           ^
    (see http://t.uber.com/nullaway )
/Volumes/git/safe-logging/preconditions-assertj/src/main/java/com/palantir/logsafe/testing/LoggableExceptionAssert.java:33: warning: [NullAway] assigning @nullable expression to @nonnull field
        argsAssert = actual == null ? null : new ArgsAssert(actual.getArgs());
                   ^
    (see http://t.uber.com/nullaway )
2 warnings
```

Another error-prone warning identified a potential issue with `Arg.equals` implementation

* Arg.equals checks isSafeForLogging, addresses EqualsGetClass error-prone finding:

```
> Task :safe-logging:compileJava
/Volumes/git/safe-logging/safe-logging/src/main/java/com/palantir/logsafe/Arg.java:51: warning: [EqualsGetClass] Overriding Object#equals in a non-final class by using getClass rather than instanceof breaks substitutability of subclasses.
    public final boolean equals(Object other) {
                         ^
    (see https://errorprone.info/bugpattern/EqualsGetClass)
  Did you mean 'if (!(other instanceof Arg)) {'?
1 warning
```
@carterkozak
Copy link
Contributor Author

Thanks @schlosna!

@carterkozak carterkozak closed this Jan 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants