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

Contract checking does not work for @Contract("null -> null") #1121

Open
agrieve opened this issue Jan 3, 2025 · 2 comments
Open

Contract checking does not work for @Contract("null -> null") #1121

agrieve opened this issue Jan 3, 2025 · 2 comments

Comments

@agrieve
Copy link

agrieve commented Jan 3, 2025

I'd like to say that if the given method returns non-null, then the passed in value is non-null.

    @Contract("null -> null")
    public static @Nullable Activity activityFromContext(@Nullable Context context) {
        // Only retrieves the base context if the supplied context is a ContextWrapper but not an
        // Activity, because Activity is a subclass of ContextWrapper.
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) return (Activity) context;

            context = ((ContextWrapper) context).getBaseContext();
        }

        return null;
    }

But this still gives a warning:

        if (ContextUtils.activityFromContext(context) != null) {
            mDialogCore = new DialogOverlayCore();
            // Complains that context is nullable:
            mDialogCore.initialize(context, mConfig, DialogOverlayImpl.this, mAsPanel);
            mDialogCore.onWindowToken(window.getWindowToken());
        }

I suspect this might fall under:

Not all possible clauses of @Contract annotations are fully parsed or supported by NullAway

If so, it'd be great to update the docs to say that contract support cannot infer parameters based on nullness of return types.

@msridhar
Copy link
Collaborator

msridhar commented Jan 4, 2025

Interesting! I feel like we should be able to handle this one, but I'll need to dig into the code more to see if there is some fundamental reason this is hard.

@msridhar
Copy link
Collaborator

msridhar commented Jan 4, 2025

I think you're right, actually, that handling this case and inferring parameter nullness from the return nullness is kind of beyond what our code is set up to do right now. I'll think more, but probably we'll need to just update the docs.

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

No branches or pull requests

2 participants