Issue #5124: removed usages of branchContains for Variable Distance
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java
index 0e7d9fd..096e14a 100644
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java
@@ -424,8 +424,13 @@
      */
     private static int getDistToVariableUsageInChildNode(DetailAST childNode, DetailAST varIdent,
                                                          int currentDistToVarUsage) {
+        DetailAST examineNode = childNode;
+        if (examineNode.getType() == TokenTypes.LABELED_STAT) {
+            examineNode = examineNode.getFirstChild().getNextSibling();
+        }
+
         int resultDist = currentDistToVarUsage;
-        switch (childNode.getType()) {
+        switch (examineNode.getType()) {
             case TokenTypes.VARIABLE_DEF:
                 resultDist++;
                 break;
@@ -437,7 +442,7 @@
             case TokenTypes.LITERAL_DO:
             case TokenTypes.LITERAL_IF:
             case TokenTypes.LITERAL_SWITCH:
-                if (isVariableInOperatorExpr(childNode, varIdent)) {
+                if (isVariableInOperatorExpr(examineNode, varIdent)) {
                     resultDist++;
                 }
                 else {
@@ -447,11 +452,11 @@
                 }
                 break;
             default:
-                if (childNode.branchContains(TokenTypes.SLIST)) {
-                    resultDist = 0;
+                if (examineNode.findFirstToken(TokenTypes.SLIST) == null) {
+                    resultDist++;
                 }
                 else {
-                    resultDist++;
+                    resultDist = 0;
                 }
         }
         return resultDist;
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java
index 7e17610..5c12d04 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java
@@ -27,6 +27,7 @@
 
 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
+import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
 
 public class VariableDeclarationUsageDistanceCheckTest extends
         AbstractModuleTestSupport {
@@ -239,7 +240,27 @@
             "542: " + getCheckMessage(MSG_KEY_EXT, "parentId", 4, 3),
         };
 
-        createChecker(checkConfig);
         verify(checkConfig, getPath("InputVariableDeclarationUsageDistance.java"), expected);
     }
+
+    @Test
+    public void testAnonymousClass() throws Exception {
+        final DefaultConfiguration checkConfig =
+            createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
+        final String[] expected = {
+            "9: " + getCheckMessage(MSG_KEY_EXT, "prefs", 4, 3),
+        };
+
+        verify(checkConfig, getPath("InputVariableDeclarationUsageDistanceAnonymous.java"),
+                expected);
+    }
+
+    @Test
+    public void testLabels() throws Exception {
+        final DefaultConfiguration checkConfig =
+            createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
+        final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
+
+        verify(checkConfig, getPath("InputVariableDeclarationUsageDistanceLabels.java"), expected);
+    }
 }
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceAnonymous.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceAnonymous.java
new file mode 100644
index 0000000..969aa80
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceAnonymous.java
@@ -0,0 +1,22 @@
+package com.puppycrawl.tools.checkstyle.checks.coding.variabledeclarationusagedistance;
+
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+import javax.swing.JMenuItem;
+
+public class InputVariableDeclarationUsageDistanceAnonymous {
+    public void method() {
+        JMenuItem prefs = new JMenuItem("Preferences...");
+
+        nothing();
+        nothing();
+        nothing();
+        prefs.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent e) {
+            }
+        });
+    }
+
+    public void nothing() {
+    }
+}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceLabels.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceLabels.java
new file mode 100644
index 0000000..e9e0e3f
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceLabels.java
@@ -0,0 +1,22 @@
+package com.puppycrawl.tools.checkstyle.checks.coding.variabledeclarationusagedistance;
+
+public class InputVariableDeclarationUsageDistanceLabels {
+    public void method() {
+        boolean eol = false;
+
+        nothing();
+        nothing();
+        nothing();
+        nothing();
+        myLoop:
+        for (int i = 0; i < 5; i++) {
+            if (i == 5) {
+                eol = true;
+                break myLoop;
+            }
+        }
+    }
+
+    public void nothing() {
+    }
+}