Skip to content

Commit 14b6481

Browse files
cpovirkError Prone Team
authored andcommitted
Make CacheLoaderNull check for more kinds of null returns.
...by making it use `AbstractAsyncTypeReturnsNull`. PiperOrigin-RevId: 852765644
1 parent d3a8c00 commit 14b6481

File tree

6 files changed

+52
-82
lines changed

6 files changed

+52
-82
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/CacheLoaderNull.java

Lines changed: 0 additions & 77 deletions
This file was deleted.

core/src/main/java/com/google/errorprone/bugpatterns/ImpossibleNullComparison.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ private void handleSwitch(ExpressionTree expression, List<? extends CaseTree> ca
241241
.filter(unused -> getFixer(withoutParens, subState).isPresent())
242242
.ifPresent(
243243
e ->
244-
// NOTE: This fix is possibly too big: you can write `case null, default ->`.
244+
// NOTE: user ->`.
245245
state.reportMatch(describeMatch(caseTree, SuggestedFix.delete(caseTree))));
246246
}
247247
}

core/src/main/java/com/google/errorprone/bugpatterns/TypeParameterNaming.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private static String suggestedSingleLetter(String id, Tree tree) {
225225
// T -> T2
226226
// T2 -> T3
227227
// T -> T4 (if T2 and T3 already exist)
228-
// TODO(user) : combine this method with TypeParameterShadowing.replacementTypeVarName
228+
// TODO(siyuanl) : combine this method with TypeParameterShadowing.replacementTypeVarName
229229
private static String firstLetterReplacementName(String name, List<String> superTypeVars) {
230230
String firstLetterOfBase = Character.toString(name.charAt(0));
231231
int typeVarNum = 2;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2019 The Error Prone Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.errorprone.bugpatterns.nullness;
18+
19+
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
20+
import static com.google.errorprone.fixes.SuggestedFix.emptyFix;
21+
22+
import com.google.common.cache.CacheLoader;
23+
import com.google.errorprone.BugPattern;
24+
import com.google.errorprone.bugpatterns.BugChecker;
25+
import com.google.errorprone.fixes.SuggestedFix;
26+
import com.sun.source.tree.ExpressionTree;
27+
28+
/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
29+
@BugPattern(summary = "The result of CacheLoader#load must be non-null.", severity = WARNING)
30+
public final class CacheLoaderNull extends AbstractAsyncTypeReturnsNull {
31+
public CacheLoaderNull() {
32+
super(CacheLoader.class);
33+
}
34+
35+
@Override
36+
protected SuggestedFix provideFix(ExpressionTree tree) {
37+
// The default suggestion is immediateFuture(null), which doesn't make sense for CacheLoader.
38+
return emptyFix();
39+
}
40+
}

core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import com.google.errorprone.bugpatterns.BugChecker;
6666
import com.google.errorprone.bugpatterns.BugPatternNaming;
6767
import com.google.errorprone.bugpatterns.ByteBufferBackingArray;
68-
import com.google.errorprone.bugpatterns.CacheLoaderNull;
6968
import com.google.errorprone.bugpatterns.CannotMockFinalClass;
7069
import com.google.errorprone.bugpatterns.CannotMockMethod;
7170
import com.google.errorprone.bugpatterns.CanonicalDuration;
@@ -579,6 +578,7 @@
579578
import com.google.errorprone.bugpatterns.nullness.AddNullMarkedToPackageInfo;
580579
import com.google.errorprone.bugpatterns.nullness.AsyncCallableReturnsNull;
581580
import com.google.errorprone.bugpatterns.nullness.AsyncFunctionReturnsNull;
581+
import com.google.errorprone.bugpatterns.nullness.CacheLoaderNull;
582582
import com.google.errorprone.bugpatterns.nullness.DereferenceWithNullBranch;
583583
import com.google.errorprone.bugpatterns.nullness.EqualsBrokenForNull;
584584
import com.google.errorprone.bugpatterns.nullness.EqualsMissingNullable;

core/src/test/java/com/google/errorprone/bugpatterns/CacheLoaderNullTest.java renamed to core/src/test/java/com/google/errorprone/bugpatterns/nullness/CacheLoaderNullTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.errorprone.bugpatterns;
17+
package com.google.errorprone.bugpatterns.nullness;
1818

1919
import com.google.errorprone.CompilationTestHelper;
2020
import org.junit.Test;
@@ -45,6 +45,13 @@ public String load(String key) {
4545
return null;
4646
}
4747
};
48+
new CacheLoader<String, String>() {
49+
@Override
50+
public String load(String key) {
51+
// BUG: Diagnostic contains:
52+
return key.equals("") ? null : key;
53+
}
54+
};
4855
abstract class MyCacheLoader extends CacheLoader<String, String> {}
4956
new MyCacheLoader() {
5057
@Override
@@ -92,7 +99,7 @@ public String get() {
9299
return null;
93100
}
94101
}
95-
;
102+
96103
return "";
97104
}
98105
};

0 commit comments

Comments
 (0)