Skip to content

Commit d2c444c

Browse files
authored
Merge pull request googlesamples#46 from tnorbye/snapshot4
Add better documentation examples for some common checks
2 parents 691bb4e + 60c5584 commit d2c444c

33 files changed

+172
-1002
lines changed

docs/checks/AnimatorKeep.md.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
import android.animation.ObjectAnimator;
7474
import android.animation.PropertyValuesHolder;
75-
import android.support.annotation.Keep;
75+
import androidx.annotation.Keep;
7676
import android.view.View;
7777
import android.widget.Button;
7878
import android.animation.FloatEvaluator;

docs/checks/BottomAppBar.md.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
android:layout_height="match_parent"
5656
android:background="#eeeeee">
5757

58-
<android.support.design.bottomappbar.BottomAppBar
58+
<com.google.android.material.bottomappbar.BottomAppBar
5959
android:id="@+id/bottom_app_bar"
6060
style="@style/Widget.MaterialComponents.BottomAppBar"
6161
android:layout_width="match_parent"

docs/checks/BrokenIterator.md.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
package test.pkg;
111111

112112
import android.os.Build;
113-
import android.support.annotation.RequiresApi;
113+
import androidx.annotation.RequiresApi;
114114

115115
import java.util.Collection;
116116
import java.util.HashMap;

docs/checks/CheckResult.md.html

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,65 +37,37 @@
3737

3838
Here is an example of lint warnings produced by this check:
3939
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
40-
src/test/pkg/CheckPermissions.java:22:Warning: The result of
41-
extractAlpha is not used [CheckResult]
40+
src/test/pkg/test.kt:10:Warning: The result of double is not used
41+
[CheckResult]
4242

43-
bitmap.extractAlpha(); // WARNING
44-
---------------------
43+
score.double()
44+
--------------
4545

4646

4747
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4848

4949
Here is the source file referenced above:
5050

51-
`src/test/pkg/CheckPermissions.java`:
52-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
53-
package test.pkg;
54-
import android.Manifest;
55-
import android.content.Context;
56-
import android.content.pm.PackageManager;
57-
import android.graphics.Bitmap;
58-
59-
@SuppressWarnings("ClassNameDiffersFromFileName")
60-
public class CheckPermissions {
61-
private void foo(Context context) {
62-
context.checkCallingOrSelfPermission(Manifest.permission.INTERNET); // WRONG
63-
context.checkPermission(Manifest.permission.INTERNET, 1, 1);
64-
check(context.checkCallingOrSelfPermission(Manifest.permission.INTERNET)); // OK
65-
int check = context.checkCallingOrSelfPermission(Manifest.permission.INTERNET); // OK
66-
if (context.checkCallingOrSelfPermission(Manifest.permission.INTERNET) // OK
67-
!= PackageManager.PERMISSION_GRANTED) {
68-
showAlert(context, "Error",
69-
"Application requires permission to access the Internet");
70-
}
71-
}
72-
73-
private Bitmap checkResult(Bitmap bitmap) {
74-
bitmap.extractAlpha(); // WARNING
75-
Bitmap bitmap2 = bitmap.extractAlpha(); // OK
76-
call(bitmap.extractAlpha()); // OK
77-
return bitmap.extractAlpha(); // OK
78-
}
79-
80-
private void showAlert(Context context, String error, String s) {
81-
}
82-
83-
private void check(int i) {
84-
}
85-
private void call(Bitmap bitmap) {
86-
}
51+
`src/test/pkg/test.kt`:
52+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
53+
package test.pkg
54+
55+
import androidx.annotation.CheckResult
56+
import java.math.BigDecimal
57+
58+
@CheckResult
59+
fun BigDecimal.double() = this + this
60+
61+
fun test(score: BigDecimal): BigDecimal {
62+
score.double()
63+
return score
8764
}
8865
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8966

9067
You can also visit the
9168
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/CheckResultDetectorTest.kt)
9269
for the unit tests for this check to see additional scenarios.
9370

94-
The above example was automatically extracted from the first unit test
95-
found for this lint check, `CheckResultDetector.testCheckResult`.
96-
To report a problem with this extracted sample, visit
97-
https://issuetracker.google.com/issues/new?component=192708.
98-
9971
(##) Suppressing
10072

10173
You can suppress false positives using one of the following mechanisms:

docs/checks/CommitTransaction.md.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@
117117

118118
// Support library
119119

120-
private android.support.v4.app.FragmentManager getSupportFragmentManager() {
120+
private androidx.fragment.app.FragmentManager getSupportFragmentManager() {
121121
return null;
122122
}
123123

124124
public void ok5() {
125125
getSupportFragmentManager().beginTransaction().commit();
126126
}
127127

128-
public void ok6(android.support.v4.app.FragmentManager manager, String tag) {
129-
android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
128+
public void ok6(androidx.fragment.app.FragmentManager manager, String tag) {
129+
androidx.fragment.app.FragmentTransaction ft = manager.beginTransaction();
130130
ft.add(null, tag);
131131
ft.commit();
132132
}
@@ -135,7 +135,7 @@
135135
getSupportFragmentManager().beginTransaction();
136136
}
137137

138-
android.support.v4.app.Fragment mFragment1 = null;
138+
androidx.fragment.app.Fragment mFragment1 = null;
139139
Fragment mFragment2 = null;
140140

141141
public void ok7() {

docs/checks/CustomSplashScreen.md.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
5353
package test.pkg
5454

55-
import android.support.v7.app.AppCompatActivity
55+
import androidx.appcompat.app.AppCompatActivity
5656
import android.os.Bundle
5757

5858
class SplashActivity : AppCompatActivity() {

docs/checks/DiffUtilEquals.md.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
5454
package test.pkg
5555

56-
import android.support.v7.util.DiffUtil
56+
import androidx.recyclerview.widget.DiffUtil
5757

5858
private val diffCallback = object : DiffUtil.ItemCallback<Cheese>() {
5959
override fun areItemsTheSame(oldItem: Cheese, newItem: Cheese): Boolean =

docs/checks/HalfFloat.md.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@
9393
package test.pkg;
9494

9595

96-
import android.support.annotation.ColorInt;
97-
import android.support.annotation.DimenRes;
98-
import android.support.annotation.HalfFloat;
99-
import android.support.annotation.Px;
100-
import android.support.annotation.StringRes;
96+
import androidx.annotation.ColorInt;
97+
import androidx.annotation.DimenRes;
98+
import androidx.annotation.HalfFloat;
99+
import androidx.annotation.Px;
100+
import androidx.annotation.StringRes;
101101

102102
@SuppressWarnings({"UnnecessaryLocalVariable", "ResultOfMethodCallIgnored", "WeakerAccess"})
103103
public class HalfFloatTest {

docs/checks/LogConditional.md.html

Lines changed: 9 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -44,151 +44,27 @@
4444

4545
Here is an example of lint warnings produced by this check:
4646
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
47-
src/test/pkg/LogTest.java:21:Warning: The log call Log.i(...) should be
47+
src/LogExample.kt:7:Warning: The log call Log.i(...) should be
4848
conditional: surround with if (Log.isLoggable(...)) or if
4949
(BuildConfig.DEBUG) { ... } [LogConditional]
5050

51-
Log.i(TAG1, "message" + m); // error: unconditional w/ computation
52-
--------------------------
53-
54-
55-
src/test/pkg/LogTest.java:22:Warning: The log call Log.i(...) should be
56-
conditional: surround with if (Log.isLoggable(...)) or if
57-
(BuildConfig.DEBUG) { ... } [LogConditional]
58-
59-
Log.i(TAG1, toString()); // error: unconditional w/ computation
51+
Log.i(TAG, "message$m") // string is not constant; computed each time
6052
-----------------------
6153

6254

63-
src/test/pkg/LogTest.java:106:Warning: The log call Log.d(...) should be
64-
conditional: surround with if (Log.isLoggable(...)) or if
65-
(BuildConfig.DEBUG) { ... } [LogConditional]
66-
67-
Log.d("Test", "Test" + getClass().toString()); // warn: unconditional
68-
---------------------------------------------
69-
70-
7155
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7256

7357
Here is the source file referenced above:
7458

75-
`src/test/pkg/LogTest.java`:
76-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
77-
package test.pkg;
59+
`src/LogExample.kt`:
60+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
61+
import android.util.Log
7862

79-
import android.annotation.SuppressLint;
80-
import android.util.Log;
81-
import static android.util.Log.DEBUG;
63+
const val TAG = "tag1"
8264

83-
@SuppressWarnings({"UnusedDeclaration", "ClassNameDiffersFromFileName"})
84-
public class LogTest {
85-
private static final String TAG1 = "MyTag1";
86-
private static final String TAG2 = "MyTag2";
87-
private static final String TAG22 = "1234567890123456789012";
88-
private static final String TAG23 = "12345678901234567890123";
89-
private static final String TAG24 = "123456789012345678901234";
90-
private static final String LONG_TAG = "MyReallyReallyReallyReallyReallyLongTag";
91-
92-
public void checkConditional(String m) {
93-
Log.d(TAG1, "message"); // ok: unconditional, but not performing computation
94-
Log.d(TAG1, m); // ok: unconditional, but not performing computation
95-
Log.d(TAG1, "ab"); // ok: unconditional, but not performing non-constant computation
96-
Log.d(TAG1, Constants.MY_MESSAGE); // ok: unconditional, but constant string
97-
Log.i(TAG1, "message" + m); // error: unconditional w/ computation
98-
Log.i(TAG1, toString()); // error: unconditional w/ computation
99-
Log.e(TAG1, toString()); // ok: only flagging debug/info messages
100-
Log.w(TAG1, toString()); // ok: only flagging debug/info messages
101-
Log.wtf(TAG1, toString()); // ok: only flagging debug/info messages
102-
if (Log.isLoggable(TAG1, 0)) {
103-
Log.d(TAG1, toString()); // ok: conditional
104-
}
105-
}
106-
107-
public void checkWrongTag(String tag) {
108-
if (Log.isLoggable(TAG1, Log.DEBUG)) {
109-
Log.d(TAG2, "message"); // warn: mismatched tags!
110-
}
111-
if (Log.isLoggable("my_tag", Log.DEBUG)) {
112-
Log.d("other_tag", "message"); // warn: mismatched tags!
113-
}
114-
if (Log.isLoggable("my_tag", Log.DEBUG)) {
115-
Log.d("my_tag", "message"); // ok: strings equal
116-
}
117-
if (Log.isLoggable(TAG1, Log.DEBUG)) {
118-
Log.d(LogTest.TAG1, "message"); // OK: same tag; different access syntax
119-
}
120-
if (Log.isLoggable(tag, Log.DEBUG)) {
121-
Log.d(tag, "message"); // ok: same variable
122-
}
123-
}
124-
125-
public void checkLongTag(boolean shouldLog) {
126-
if (shouldLog) {
127-
// String literal tags
128-
Log.d("short_tag", "message"); // ok: short
129-
Log.d("really_really_really_really_really_long_tag", "message"); // error: too long
130-
131-
// Resolved field tags
132-
Log.d(TAG1, "message"); // ok: short
133-
Log.d(TAG22, "message"); // ok: short
134-
Log.d(TAG23, "message"); // ok: threshold
135-
Log.d(TAG24, "message"); // error: too long
136-
Log.d(LONG_TAG, "message"); // error: way too long
137-
138-
// Locally defined variable tags
139-
final String LOCAL_TAG = "MyReallyReallyReallyReallyReallyLongTag";
140-
Log.d(LOCAL_TAG, "message"); // error: too long
141-
142-
// Concatenated tags
143-
Log.d(TAG22 + TAG1, "message"); // error: too long
144-
Log.d(TAG22 + "MyTag", "message"); // error: too long
145-
}
146-
}
147-
148-
public void checkWrongLevel(String tag) {
149-
if (Log.isLoggable(TAG1, Log.DEBUG)) {
150-
Log.d(TAG1, "message"); // ok: right level
151-
}
152-
if (Log.isLoggable(TAG1, Log.INFO)) {
153-
Log.i(TAG1, "message"); // ok: right level
154-
}
155-
if (Log.isLoggable(TAG1, Log.DEBUG)) {
156-
Log.v(TAG1, "message"); // warn: wrong level
157-
}
158-
if (Log.isLoggable(TAG1, DEBUG)) { // static import of level
159-
Log.v(TAG1, "message"); // warn: wrong level
160-
}
161-
if (Log.isLoggable(TAG1, Log.VERBOSE)) {
162-
Log.d(TAG1, "message"); // warn? verbose is a lower logging level, which includes debug
163-
}
164-
if (Log.isLoggable(TAG1, Constants.MY_LEVEL)) {
165-
Log.d(TAG1, "message"); // ok: unknown level alias
166-
}
167-
}
168-
169-
@SuppressLint("all")
170-
public void suppressed1() {
171-
Log.d(TAG1, "message"); // ok: suppressed
172-
}
173-
174-
@SuppressLint("LogConditional")
175-
public void suppressed2() {
176-
Log.d(TAG1, "message"); // ok: suppressed
177-
}
178-
179-
// Regression test for https://issuetracker.google.com/111063607
180-
public void notActuallyConditional() {
181-
if (true) {
182-
Log.d("Test", "Test" + getClass().toString()); // warn: unconditional
183-
}
184-
if (false) {
185-
Log.d("Test", "Test" + getClass().toString()); // ok: never called
186-
}
187-
}
188-
189-
private static class Constants {
190-
public static final String MY_MESSAGE = "My Message";
191-
public static final int MY_LEVEL = 5;
65+
class LogExample {
66+
fun test(m: String) {
67+
Log.i(TAG, "message$m") // string is not constant; computed each time
19268
}
19369
}
19470
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -197,11 +73,6 @@
19773
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/LogDetectorTest.kt)
19874
for the unit tests for this check to see additional scenarios.
19975

200-
The above example was automatically extracted from the first unit test
201-
found for this lint check, `LogDetector.testBasic`.
202-
To report a problem with this extracted sample, visit
203-
https://issuetracker.google.com/issues/new?component=192708.
204-
20576
(##) Suppressing
20677

20778
You can suppress false positives using one of the following mechanisms:

0 commit comments

Comments
 (0)