diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlock.java b/core/src/main/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlock.java index de8ca29da8c..ea569ddb2c3 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlock.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlock.java @@ -144,7 +144,7 @@ private void handle(ExpressionTree tree) { if (!use.isSubClass(classSymbol, state.getTypes())) { return; } - if (!isStatic(use)) { + if (use.isEnclosedBy(classSymbol) && !isStatic(use)) { // Nested inner classes implicitly take the enclosing instance as a constructor parameter, // and can't be initialized without first initializing their containing class. return; diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlockTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlockTest.java index bce0b5f64a0..b1c51f7f1d1 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlockTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlockTest.java @@ -296,4 +296,17 @@ public void nestedInterface() { "}") .doTest(); } + + @Test + public void nonNestedSubclass() { + testHelper + .addSourceLines( + "Foo.java", + "class A {", + " // BUG: Diagnostic contains:", + " private static Object cycle = new B();", + "}", + "class B extends A {}") + .doTest(); + } }