Skip to content

Commit 13be411

Browse files
cushonError Prone Team
authored andcommitted
Handle null != CONST_CASE in YodaCondition
PiperOrigin-RevId: 636287602
1 parent ae77a5f commit 13be411

2 files changed

Lines changed: 57 additions & 8 deletions

File tree

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private Description fix(
9595
ExpressionTree rhs,
9696
boolean provideNullSafeFix,
9797
VisitorState state) {
98-
if (seemsConstant(lhs) && !seemsConstant(rhs)) {
98+
if (yodaCondition(lhs, rhs)) {
9999
var description = buildDescription(lhs);
100100
if (provideNullSafeFix
101101
&& !getNullnessValue(rhs, state, NullnessAnalysis.instance(state.context))
@@ -116,19 +116,40 @@ private Description fix(
116116
return NO_MATCH;
117117
}
118118

119-
private static boolean seemsConstant(Tree tree) {
119+
private static boolean yodaCondition(ExpressionTree lhs, ExpressionTree rhs) {
120+
ConstantKind l = seemsConstant(lhs);
121+
ConstantKind r = seemsConstant(rhs);
122+
return l.constness > r.constness;
123+
}
124+
125+
enum ConstantKind {
126+
NULL(3),
127+
CONSTANT(2),
128+
CONSTANT_VARIABLE(1),
129+
NON_CONSTANT(0);
130+
131+
final int constness;
132+
133+
ConstantKind(int constness) {
134+
this.constness = constness;
135+
}
136+
}
137+
138+
private static ConstantKind seemsConstant(Tree tree) {
120139
if (constValue(tree) != null) {
121-
return true;
140+
return ConstantKind.CONSTANT;
122141
}
123142
if (tree.getKind().equals(Tree.Kind.NULL_LITERAL)) {
124-
return true;
143+
return ConstantKind.NULL;
125144
}
126145
var symbol = getSymbol(tree);
127-
if (!(symbol instanceof VarSymbol)) {
128-
return false;
146+
if (symbol instanceof VarSymbol
147+
&& (symbol.isEnum()
148+
|| (isStatic(symbol)
149+
&& CONSTANT_CASE.matcher(symbol.getSimpleName().toString()).matches()))) {
150+
return ConstantKind.CONSTANT_VARIABLE;
129151
}
130-
return symbol.isEnum()
131-
|| (isStatic(symbol) && CONSTANT_CASE.matcher(symbol.getSimpleName().toString()).matches());
152+
return ConstantKind.NON_CONSTANT;
132153
}
133154

134155
private static final Pattern CONSTANT_CASE = Pattern.compile("[A-Z0-9_]+");

core/src/test/java/com/google/errorprone/bugpatterns/YodaConditionTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,32 @@ public void nullableConstant() {
223223
"}")
224224
.doTest();
225225
}
226+
227+
@Test
228+
public void nullableYodaCondition() {
229+
refactoring
230+
.addInputLines(
231+
"Test.java",
232+
"class Test {",
233+
" private static final String CONST = \"hello\";",
234+
" public static boolean f(String foo) {",
235+
" return null != foo;",
236+
" }",
237+
" public static boolean g() {",
238+
" return null != CONST;",
239+
" }",
240+
"}")
241+
.addOutputLines(
242+
"Test.java",
243+
"class Test {",
244+
" private static final String CONST = \"hello\";",
245+
" public static boolean f(String foo) {",
246+
" return foo != null;",
247+
" }",
248+
" public static boolean g() {",
249+
" return CONST != null;",
250+
" }",
251+
"}")
252+
.doTest();
253+
}
226254
}

0 commit comments

Comments
 (0)