Skip to content

Contract checking does not check for proper overriding #803

Open

Description

@msridhar

Our checking of @Contract annotations (which is off by default) does not check for correct method overriding. So, if a supertype method has a particular @Contract, we do not check that overriding methods adhere to the @Contract, which could lead to missed NPE bugs. Originally discussed here.

Activity

  1. msridhar commented on Aug 29, 2026

    @msridhar
    ContributorAuthor

    Note from #1758 that whatever support we build here should also account for library models that are equivalent to contracts.

  2. vlsi commented on Oct 22, 2026

    @vlsi
    Contributor

    Still applies on master (5023d67). With CheckContracts=true, ContractCheckHandler checks a method only against its own @Contract, so an override that drops or weakens the supertype's contract is silent while callers typed as the supertype keep trusting it. #1758 and #1771 did not change this: they make nullImpliesNull library models apply to overrides at call sites, which per the comment above the eventual check should also cover. The field-contract handlers already have the shape this would follow (RequiresNonNullHandler.validateOverridingRules, EnsuresNonNullIfHandler.validateOverridingRules).

    Open design points for whoever picks this up: whether an override with no @Contract inherits the supertype's clauses (and its body is checked against them) or is reported for missing them; how to decide that an override's own @Contract implies the supertype's (clause-by-clause match, or only the checkable _/null/!null -> !null subset that ContractCheckHandler verifies today).

    Reproducer (NullAway 5023d67, JDK 21)

    Flags: -XepOpt:NullAway:AnnotatedPackages=com.uber -XepOpt:NullAway:CheckContracts=true

    package com.uber;
    import javax.annotation.Nullable;
    import org.jetbrains.annotations.Contract;
    class Test {
      static class Super {
        @Contract("!null -> !null")
        @Nullable Object m(@Nullable Object o) { return o; }
      }
      static class Sub extends Super {
        @Override
        @Nullable Object m(@Nullable Object o) {
          return null; // expected: error, violates inherited @Contract("!null -> !null")
        }
      }
      static class Sub2 extends Super {
        @Override
        @Contract("null -> null") // expected: error, does not imply the supertype's contract
        @Nullable Object m(@Nullable Object o) { return null; }
      }
      static void use() {
        Super s = new Sub();
        s.m(new Object()).toString(); // NPE at run time; no warning anywhere
      }
    }

    Observed: no diagnostic. Expected: an error in Sub.m and on Sub2.m's contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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