Summary: This will help making error reporting more actionable. Often methods that are nullable in general (like View.findViewById) are used as not-nullable due to app-invariants. In such cases suggesting a non-nullable alternative that does an assertion under the hood makes the error report more actionable and provides necessary guidance with respect to coding best practices Follow up will include adding more methods to models. If this goes well, we might support it in user-defined area (nullability repository) Reviewed By: artempyanykh Differential Revision: D20001416 fbshipit-source-id: 46f03467cmaster
parent
50e5bfd32f
commit
f57dc78679
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package android.view;
|
||||
|
||||
public class View {
|
||||
public <T extends View> T findViewById(int id) {
|
||||
// STUB: we need signature for tests; implementation is not tested
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
// STUB: we need signature for tests; implementation is not tested
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package codetoanalyze.java.nullsafe_default;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* Test to ensure we have special messaging when misusing known nullable methods that have
|
||||
* non-nullable alternatives.
|
||||
*/
|
||||
public class AlternativeRecommendations {
|
||||
@SuppressLint("eradicate-field-not-initialized")
|
||||
View field;
|
||||
|
||||
static void dereference_ShouldSuggestAlternative(View view) {
|
||||
view.findViewById(2).setId(3);
|
||||
}
|
||||
|
||||
static void passingParam_ShouldSuggestAlternative(View view) {
|
||||
acceptsNonnullView(view.findViewById(2));
|
||||
}
|
||||
|
||||
static View returnValue_ShouldSuggestAlternative(View view) {
|
||||
return view.findViewById(2);
|
||||
}
|
||||
|
||||
void assigningField_ShouldSuggestAlternative(View view) {
|
||||
field = view.findViewById(2);
|
||||
}
|
||||
|
||||
static void acceptsNonnullView(View view) {}
|
||||
}
|
Loading…
Reference in new issue