diff --git a/infer/man/man1/infer-full.txt b/infer/man/man1/infer-full.txt index 6ebc30f56..716490984 100644 --- a/infer/man/man1/infer-full.txt +++ b/infer/man/man1/infer-full.txt @@ -1667,11 +1667,12 @@ INTERNAL OPTIONS This feature is needed for compatibility reasons. (Conversely: --no-nullsafe-disable-field-not-initialized-in-nonstrict-classes) - --no-nullsafe-optimistic-third-party-params-in-non-strict - Deactivates: Nullsafe: in this mode we treat non annotated third - party method params as if they were annotated as nullable. - (Conversely: - --nullsafe-optimistic-third-party-params-in-non-strict) + --no-nullsafe-optimistic-third-party-in-default-mode + Deactivates: Nullsafe: Unless @Nullsafe annotation is used, treat + not annotated third party method params as if they were annotated + as nullable, and return values as if they were annotated as + non-null (Conversely: + --nullsafe-optimistic-third-party-in-default-mode) --nullsafe-strict-containers Activates: Warn when containers are used with nullable keys or diff --git a/infer/src/base/Config.ml b/infer/src/base/Config.ml index 2a846c5a4..db7576bef 100644 --- a/infer/src/base/Config.ml +++ b/infer/src/base/Config.ml @@ -1650,17 +1650,15 @@ and nullsafe_disable_field_not_initialized_in_nonstrict_classes = marked as @NullsafeStrict. This feature is needed for compatibility reasons." -and nullsafe_optimistic_third_party_params_in_non_strict = +and nullsafe_optimistic_third_party_in_default_mode = CLOpt.mk_bool ~long: - "nullsafe-optimistic-third-party-params-in-non-strict" - (* Turned on for compatibility reasons. - Historically this is because there was no actionable way to change third party annotations. - Now that we have such a support, this behavior should be reconsidered, provided - our tooling and error reporting is friendly enough to be smoothly used by developers. - *) ~default:true - "Nullsafe: in this mode we treat non annotated third party method params as if they were \ - annotated as nullable." + "nullsafe-optimistic-third-party-in-default-mode" + (* Turned on for compatibility reasons + *) ~default:true + "Nullsafe: Unless @Nullsafe annotation is used, treat not annotated third party method params \ + as if they were annotated as nullable, and return values as if they were annotated as \ + non-null" and nullsafe_third_party_signatures = @@ -2996,8 +2994,8 @@ and nullsafe_disable_field_not_initialized_in_nonstrict_classes = !nullsafe_disable_field_not_initialized_in_nonstrict_classes -and nullsafe_optimistic_third_party_params_in_non_strict = - !nullsafe_optimistic_third_party_params_in_non_strict +and nullsafe_optimistic_third_party_in_default_mode = + !nullsafe_optimistic_third_party_in_default_mode and nullsafe_third_party_signatures = !nullsafe_third_party_signatures diff --git a/infer/src/base/Config.mli b/infer/src/base/Config.mli index e9c4103fe..b61397769 100644 --- a/infer/src/base/Config.mli +++ b/infer/src/base/Config.mli @@ -401,7 +401,7 @@ val nullsafe_annotation_graph : bool val nullsafe_disable_field_not_initialized_in_nonstrict_classes : bool -val nullsafe_optimistic_third_party_params_in_non_strict : bool +val nullsafe_optimistic_third_party_in_default_mode : bool val nullsafe_third_party_signatures : string option diff --git a/infer/src/nullsafe/AssignmentRule.ml b/infer/src/nullsafe/AssignmentRule.ml index f9e507cf3..3011817ee 100644 --- a/infer/src/nullsafe/AssignmentRule.ml +++ b/infer/src/nullsafe/AssignmentRule.ml @@ -49,8 +49,9 @@ module ReportableViolation = struct let from nullsafe_mode ({lhs; rhs} as violation) = let falls_under_optimistic_third_party = - Config.nullsafe_optimistic_third_party_params_in_non_strict + Config.nullsafe_optimistic_third_party_in_default_mode && NullsafeMode.equal nullsafe_mode Default + (* Treat third party params as if they were [@Nullable] *) && Nullability.equal (AnnotatedNullability.get_nullability lhs) ThirdPartyNonnull in let is_non_reportable = diff --git a/infer/src/nullsafe/ErrorRenderingUtils.ml b/infer/src/nullsafe/ErrorRenderingUtils.ml index 42e647856..c70d074e8 100644 --- a/infer/src/nullsafe/ErrorRenderingUtils.ml +++ b/infer/src/nullsafe/ErrorRenderingUtils.ml @@ -150,12 +150,8 @@ let mk_recommendation_for_third_party_field nullsafe_mode field = match nullsafe_mode with | NullsafeMode.Strict -> F.sprintf "access %s via a nullsafe strict getter" field - | NullsafeMode.Local _ -> + | NullsafeMode.Local _ | NullsafeMode.Default -> F.sprintf "access %s via a nullsafe getter" field - | NullsafeMode.Default -> - Logging.die InternalError - "mk_recommendation_for_third_party_field:: Should not happen: we should tolerate third \ - party in default mode" let get_info object_origin nullsafe_mode untrusted_kind = diff --git a/infer/src/nullsafe/Nullability.ml b/infer/src/nullsafe/Nullability.ml index a9945f12b..3e9bce55e 100644 --- a/infer/src/nullsafe/Nullability.ml +++ b/infer/src/nullsafe/Nullability.ml @@ -68,8 +68,10 @@ let is_considered_nonnull ~nullsafe_mode nullability = | NullsafeMode.Local NullsafeMode.Trust.All -> UncheckedNonnull | NullsafeMode.Default -> - (* In default mode, we trust everything, even not annotated third party. *) - ThirdPartyNonnull + (* We trust all internal method declarations, and, if specified separately, + even not annotated third party. *) + if Config.nullsafe_optimistic_third_party_in_default_mode then ThirdPartyNonnull + else UncheckedNonnull in is_subtype ~subtype:nullability ~supertype:least_required