Commit a238881c authored by Tomasz Wiszkowski's avatar Tomasz Wiszkowski Committed by Commit Bot

Enable errorprone compile-time nullable checks.

This change enables errorprone basic null checking.
The new flag will raise an error when a @Nullable
argument or variable is not checked before being used.

The code updates all sites that declared parameters as
@Nullable without subsequently checking their values to
@NonNull.

The change should help push forward the effort to enable
the NullAway errorprone extension

Bug: 1022427

Change-Id: I6c1e87d87fad98252c3cc3916e3a1034cf4eb8cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2521294
Commit-Queue: Tomasz Wiszkowski <ender@google.com>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825008}
parent 553c38ae
......@@ -32,7 +32,6 @@ _JAVAC_EXTRACTOR = os.path.join(build_utils.DIR_SOURCE_ROOT, 'third_party',
# Full list of checks: https://errorprone.info/bugpatterns
ERRORPRONE_WARNINGS_TO_DISABLE = [
# These should really be turned on.
'ParameterNotNullable',
'CollectionUndefinedEquality',
'ModifyCollectionInEnhancedForLoop',
# The following are super useful, but existing issues need to be fixed first
......@@ -183,6 +182,7 @@ ERRORPRONE_WARNINGS_TO_ENABLE = [
'InvalidThrows',
'LongLiteralLowerCaseSuffix',
'MultiVariableDeclaration',
'ParameterNotNullable',
'RedundantOverride',
'StaticQualifiedUsingExpression',
'StringEquality',
......
......@@ -13,6 +13,7 @@ import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.chromium.chrome.autofill_assistant.R;
......@@ -32,7 +33,7 @@ public class AssistantLoginSection extends AssistantCollectUserDataSection<Assis
}
@Override
protected void createOrEditItem(@Nullable AssistantLoginChoice oldItem) {
protected void createOrEditItem(@NonNull AssistantLoginChoice oldItem) {
assert oldItem != null;
assert oldItem.getInfoPopup() != null;
......
......@@ -95,7 +95,7 @@ public final class ScreenshotTask implements ScreenshotSource {
}
private boolean takeCompositorScreenshot(@Nullable Activity activity) {
if (!shouldTakeCompositorScreenshot((activity))) return false;
if (activity == null || !shouldTakeCompositorScreenshot((activity))) return false;
Rect rect = new Rect();
activity.getWindow().getDecorView().getRootView().getWindowVisibleDisplayFrame(rect);
......
......@@ -21,7 +21,7 @@ public final class TitleUtil {
* it. Otherwise, returns a shortened form of the URL.
*/
public static String getTitleForDisplay(@Nullable String title, @Nullable GURL url) {
if (!TextUtils.isEmpty(title) || GURL.isEmptyOrInvalid(url)) {
if (!TextUtils.isEmpty(title) || url == null || GURL.isEmptyOrInvalid(url)) {
return title;
}
......
......@@ -168,19 +168,19 @@ public class AddressEditor
// If |toEdit| is null, we're creating a new autofill profile with the country code of the
// default locale on this device.
boolean isNewAddress = toEdit == null;
final String editTitle;
final AutofillAddress address;
if (toEdit == null) {
address = new AutofillAddress(mContext, new AutofillProfile());
editTitle = mContext.getString(R.string.autofill_create_profile);
} else {
address = toEdit;
editTitle = toEdit.getEditTitle();
}
// Ensure that |address| and |mProfile| are always not null.
final AutofillAddress address =
isNewAddress ? new AutofillAddress(mContext, new AutofillProfile()) : toEdit;
mEditor = new EditorModel(editTitle);
mProfile = address.getProfile();
// The title of the editor depends on whether we're adding a new address or editing an
// existing address.
mEditor =
new EditorModel(isNewAddress ? mContext.getString(R.string.autofill_create_profile)
: toEdit.getEditTitle());
// When edit is called, a new form is started, so the country on the
// dropdown list is not changed. => mRecentlySelectedCountry should be null.
mRecentlySelectedCountry = null;
......
......@@ -352,19 +352,24 @@ public class CardEditor extends EditorBase<AutofillPaymentInstrument>
final Callback<AutofillPaymentInstrument> cancelCallback) {
super.edit(toEdit, doneCallback, cancelCallback);
// If |toEdit| is null, we're creating a new credit card.
final boolean isNewCard = toEdit == null;
// Ensure that |instrument| and |card| are never null.
final AutofillPaymentInstrument instrument = isNewCard
? new AutofillPaymentInstrument(mWebContents, new CreditCard(),
null /* billingAddress */, null /* methodName */)
: toEdit;
final CreditCard card = instrument.getCard();
final EditorModel editor = new EditorModel(
isNewCard ? mContext.getString(R.string.payments_add_card) : toEdit.getEditTitle());
final boolean isNewCard;
final AutofillPaymentInstrument instrument;
final EditorModel editor;
if (toEdit == null) {
// We're creating a new credit card.
isNewCard = true;
// Ensure that |instrument| is never null.
instrument = new AutofillPaymentInstrument(mWebContents, new CreditCard(),
null /* billingAddress */, null /* methodName */);
editor = new EditorModel(mContext.getString(R.string.payments_add_card));
} else {
isNewCard = false;
instrument = toEdit;
editor = new EditorModel(toEdit.getEditTitle());
}
final CreditCard card = instrument.getCard();
if (card.getIsLocal()) {
Calendar calendar = null;
try {
......
......@@ -4,7 +4,7 @@
package org.chromium.chrome.browser.webapps;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import org.chromium.chrome.browser.app.ChromeActivity;
import org.chromium.chrome.browser.browserservices.ui.controller.webapps.WebappDisclosureController;
......@@ -49,7 +49,7 @@ public class WebApkActivityCoordinator implements Destroyable {
}
public void onDeferredStartupWithStorage(
@Nullable WebappDataStorage storage, boolean didCreateStorage) {
@NonNull WebappDataStorage storage, boolean didCreateStorage) {
assert storage != null;
storage.incrementLaunchCount();
......
......@@ -96,6 +96,7 @@ public class TrustedWebActivityClientLocationDelegationTest {
@Override
public void onExtraCallback(String callbackName, @Nullable Bundle bundle) {
if (TextUtils.equals(callbackName, EXTRA_NEW_LOCATION_AVAILABLE_CALLBACK)) {
Assert.assertNotNull(bundle);
Assert.assertTrue(bundle.containsKey("latitude"));
Assert.assertTrue(bundle.containsKey("longitude"));
Assert.assertTrue(bundle.containsKey("timeStamp"));
......@@ -123,6 +124,7 @@ public class TrustedWebActivityClientLocationDelegationTest {
@Override
public void onExtraCallback(String callbackName, @Nullable Bundle bundle) {
if (TextUtils.equals(callbackName, EXTRA_NEW_LOCATION_ERROR_CALLBACK)) {
Assert.assertNotNull(bundle);
Assert.assertTrue(bundle.containsKey("message"));
locationError.notifyCalled();
}
......
......@@ -7,7 +7,7 @@ package org.chromium.chrome.browser.download.dialogs;
import android.content.Context;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import org.chromium.base.Callback;
import org.chromium.chrome.browser.download.DownloadLaterMetrics;
......@@ -71,14 +71,12 @@ public class DownloadLaterDialogHelper implements DownloadLaterDialogController
* @param callback The callback to reply the new schedule selected by the user. May reply null
* if the user cancels the dialog.
*/
public void showChangeScheduleDialog(@Nullable final OfflineItemSchedule currentSchedule,
public void showChangeScheduleDialog(@NonNull final OfflineItemSchedule currentSchedule,
@Source int source, Callback<OfflineItemSchedule> callback) {
@DownloadLaterDialogChoice
int initialChoice = DownloadLaterDialogChoice.DOWNLOAD_NOW;
if (currentSchedule != null) {
initialChoice = currentSchedule.onlyOnWifi ? DownloadLaterDialogChoice.ON_WIFI
: DownloadLaterDialogChoice.DOWNLOAD_LATER;
}
initialChoice = currentSchedule.onlyOnWifi ? DownloadLaterDialogChoice.ON_WIFI
: DownloadLaterDialogChoice.DOWNLOAD_LATER;
mCallback = callback;
mSource = source;
......
......@@ -92,7 +92,7 @@ public final class EditorScreenshotTask implements EditorScreenshotSource {
}
private boolean takeCompositorScreenshot(@Nullable Activity activity) {
if (!shouldTakeCompositorScreenshot((activity))) return false;
if (activity == null || !shouldTakeCompositorScreenshot((activity))) return false;
Rect rect = new Rect();
activity.getWindow().getDecorView().getRootView().getWindowVisibleDisplayFrame(rect);
......
......@@ -19,6 +19,7 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -60,6 +61,7 @@ public class ScreenshotCoordinatorTest {
@Override
public void capture(@Nullable Runnable callback) {
Assert.assertNotNull(callback);
callback.run();
}
......
......@@ -1225,7 +1225,7 @@ class BottomSheet extends FrameLayout
protected void onSheetContentChanged(@Nullable final BottomSheetContent content) {
mSheetContent = content;
if (isFullHeightWrapContent()) {
if (content != null && isFullHeightWrapContent()) {
// Listen for layout/size changes.
if (!content.setContentSizeListener(this::onContentSizeChanged)) {
content.getContentView().addOnLayoutChangeListener(this);
......
......@@ -7,6 +7,7 @@ package org.chromium.components.browser_ui.contacts_picker;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.chromium.blink.mojom.ContactIconBlob;
......@@ -227,7 +228,7 @@ public class ContactDetails implements Comparable<ContactDetails> {
* @return The contact details registered for this contact.
*/
public AbbreviatedContactDetails getAbbreviatedContactDetails(boolean includeAddresses,
boolean includeEmails, boolean includeTels, @Nullable Resources resources) {
boolean includeEmails, boolean includeTels, @NonNull Resources resources) {
AbbreviatedContactDetails results = new AbbreviatedContactDetails();
results.overflowAddressCount = "";
......
......@@ -8,6 +8,7 @@ import android.app.Activity;
import android.content.ComponentName;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.chromium.base.CollectionUtil;
......@@ -159,7 +160,7 @@ public class ShareServiceImpl implements ShareService {
public void share(ShareParams params);
}
public ShareServiceImpl(@Nullable WebContents webContents, WebShareDelegate delegate) {
public ShareServiceImpl(@NonNull WebContents webContents, WebShareDelegate delegate) {
mWindow = webContents.getTopLevelNativeWindow();
mDelegate = delegate;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment