Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #7734: Update AbstractChecks to log DetailAST - Indentation #8304

Closed
wants to merge 1 commit into from

Conversation

HuGanghui
Copy link
Contributor

@HuGanghui HuGanghui commented Jun 4, 2020

Issue #7734: Update AbstractChecks to log DetailAST - Indentation

this PR is based on comments in Issue #7734 and PR #8038

ATTENTION: PR is on hold to while #8038 is active.

@HuGanghui HuGanghui marked this pull request as draft June 4, 2020 09:20
@HuGanghui
Copy link
Contributor Author

HuGanghui commented Jun 4, 2020

When I update indentation to log DetailAST, I run com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java to test, but one problem appear, this test failure:

public void testInvalidSwitchWithChecker()
throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
checkConfig.addAttribute("arrayInitIndent", "4");
checkConfig.addAttribute("basicOffset", "4");
checkConfig.addAttribute("braceAdjustment", "0");
checkConfig.addAttribute("caseIndent", "4");
checkConfig.addAttribute("forceStrictCondition", "false");
checkConfig.addAttribute("lineWrappingIndentation", "4");
checkConfig.addAttribute("tabWidth", "4");
checkConfig.addAttribute("throwsIndent", "4");
final String fileName = getPath("InputIndentationInvalidSwitchIndent.java");
final String[] expected = {
"30: " + getCheckMessage(MSG_ERROR, "switch", 6, 8),
"32: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 12),
"33: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
"37: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
"39: " + getCheckMessage(MSG_CHILD_ERROR, "case", 14, 12),
"40: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 12),
"43: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 12),
"44: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
"45: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
"53: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
"54: " + getCheckMessage(MSG_CHILD_ERROR, "block", 18, 16),
"55: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 12),
"59: " + getCheckMessage(MSG_ERROR, "block lcurly", 10, 12),
"62: " + getCheckMessage(MSG_ERROR, "block rcurly", 14, 12),
"66: " + getCheckMessage(MSG_ERROR, "block lcurly", 14, 12),
"69: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 12),
"76: " + getCheckMessage(MSG_CHILD_ERROR, "case", 14, 16),
"81: " + getCheckMessage(MSG_CHILD_ERROR, "case", 14, 16),
"89: " + getCheckMessage(MSG_ERROR, "switch rcurly", 6, 8),
"92: " + getCheckMessage(MSG_ERROR, "switch lcurly", 6, 8),
"93: " + getCheckMessage(MSG_ERROR, "switch rcurly", 10, 8),
"95: " + getCheckMessage(MSG_ERROR, "switch lcurly", 10, 8),
"96: " + getCheckMessage(MSG_ERROR, "switch rcurly", 6, 8),
"99: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
"100: " + getCheckMessage(MSG_ERROR, "if", 12, 16),
"101: " + getCheckMessage(MSG_CHILD_ERROR, "if", 16, 20),
"102: " + getCheckMessage(MSG_ERROR, "else", 12, 16),
"103: " + getCheckMessage(MSG_CHILD_ERROR, "else", 16, 20),
};
verifyWarns(checkConfig, fileName, expected);
}

java.lang.Error: Error was thrown while processing /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java

	at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:319)
	at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:221)
	at com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport.verify(AbstractModuleTestSupport.java:291)
	at com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport.verify(AbstractModuleTestSupport.java:267)
	at com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport.verify(AbstractModuleTestSupport.java:246)
	at com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.verify(IndentationCheckTest.java:166)
	at com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.verifyWarns(IndentationCheckTest.java:157)
	at com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.testInvalidSwitchWithChecker(IndentationCheckTest.java:717)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.opentest4j.AssertionFailedError: input expected warning #26 at line 101 to report one of the following: "' child has incorrect indentation level 16, expected level should be 20.", "' child has incorrect indentation level 16, expected level should be 20.", but got instead: 102: 'else' has incorrect indentation level 12, expected level should be 16. ==> expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:40)
	at org.junit.jupiter.api.Assertions.assertTrue(Assertions.java:193)
	at com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest$IndentAudit.addError(IndentationCheckTest.java:2136)
	at com.puppycrawl.tools.checkstyle.Checker.fireErrors(Checker.java:409)
	at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:299)
	... 70 more

@HuGanghui
Copy link
Contributor Author

HuGanghui commented Jun 4, 2020

So I do a test:

/var/tmp $ cat InputIndentationInvalidSwitchIndent.java ( same as checkstyle/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java)

package com.puppycrawl.tools.checkstyle.checks.indentation.indentation; //indent:0 exp:0

/**                                                                           //indent:0 exp:0
 * This test-input is intended to be checked using following configuration:   //indent:1 exp:1
 *                                                                            //indent:1 exp:1
 * arrayInitIndent = 4                                                        //indent:1 exp:1
 * basicOffset = 4                                                            //indent:1 exp:1
 * braceAdjustment = 0                                                        //indent:1 exp:1
 * caseIndent = 4                                                             //indent:1 exp:1
 * forceStrictCondition = false                                               //indent:1 exp:1
 * lineWrappingIndentation = 4                                                //indent:1 exp:1
 * tabWidth = 4                                                               //indent:1 exp:1
 * throwsIndent = 4                                                           //indent:1 exp:1
 *                                                                            //indent:1 exp:1
 * @author  jrichard                                                         //indent:1 exp:1
 */                                                                           //indent:1 exp:1
public class InputIndentationInvalidSwitchIndent { //indent:0 exp:0

    private static final int CONST = 5; //indent:4 exp:4
    private static final int CONST2 = 2; //indent:4 exp:4
    private static final int CONST3 = 3; //indent:4 exp:4

    /** Creates a new instance of InputIndentationInvalidSwitchIndent */ //indent:4 exp:4
    public InputIndentationInvalidSwitchIndent() { //indent:4 exp:4
    } //indent:4 exp:4

    private void method1() { //indent:4 exp:4
        int s = 3; //indent:8 exp:8

      switch (s) { //indent:6 exp:8 warn

          case 4: //indent:10 exp:12 warn
              System.identityHashCode(""); //indent:14 exp:16 warn
                break; //indent:16 exp:16

            case CONST: //indent:12 exp:12
              break; //indent:14 exp:16 warn

              case CONST2: //indent:14 exp:12 warn
          case CONST3: //indent:10 exp:12 warn
                break; //indent:16 exp:16

          default: //indent:10 exp:12 warn
              System.identityHashCode(""); //indent:14 exp:16 warn
              break; //indent:14 exp:16 warn
        } //indent:8 exp:8


        // some people like to add curlies to their cases: //indent:8 exp:8
        switch (s) { //indent:8 exp:8

            case 4: { //indent:12 exp:12
              System.identityHashCode(""); //indent:14 exp:16 warn
                  break; //indent:18 exp:16 warn
          } //indent:10 exp:12 warn

            case CONST2: //indent:12 exp:12
            case CONST3: //indent:12 exp:12
          { //indent:10 exp:12 warn
                System.identityHashCode(""); //indent:16 exp:16
                break; //indent:16 exp:16
              } //indent:14 exp:12 warn


            case 22: //indent:12 exp:12
              { //indent:14 exp:12 warn
                System.identityHashCode(""); //indent:16 exp:16
                break; //indent:16 exp:16
          } //indent:10 exp:12 warn
        } //indent:8 exp:8

        // check broken 'case' lines //indent:8 exp:8
        switch (s) { //indent:8 exp:8

            case //indent:12 exp:12
              CONST: //indent:14 exp:16 warn
                break; //indent:16 exp:16

            case CONST2: //indent:12 exp:12
            case //indent:12 exp:12
              CONST3: //indent:14 exp:16 warn
            { //indent:12 exp:12
                System.identityHashCode(""); //indent:16 exp:16
                break; //indent:16 exp:16
            } //indent:12 exp:12
        } //indent:8 exp:8

        switch (s) { //indent:8 exp:8
      } //indent:6 exp:8 warn

        switch (s) //indent:8 exp:8
      { //indent:6 exp:8 warn
          } //indent:10 exp:8 warn
        switch (s) //indent:8 exp:8
          { //indent:10 exp:8 warn
      } //indent:6 exp:8 warn

        switch (s) { //indent:8 exp:8
	    case 1: //indent:8 exp:12 warn
	    	if (true) //indent:12 exp:16 warn
	    		break; //indent:16 exp:20 warn
	    	else //indent:12 exp:16 warn
	    		break; //indent:16 exp:20 warn
        } //indent:8 exp:8

    } //indent:4 exp:4

} //indent:0 exp:0

/var/tmp $ cat config.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="Indentation">
            <property name="arrayInitIndent" value="4"/>
            <property name="basicOffset" value="4"/>
            <property name="braceAdjustment" value="0"/>
            <property name="caseIndent" value="4"/>
            <property name="forceStrictCondition" value="false"/>
            <property name="lineWrappingIndentation" value="4"/>
            <property name="tabWidth" value="4"/>
            <property name="throwsIndent" value="4"/>
        </module>
    </module>
</module>

before update to log

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar /var/tmp/checkstyle-8.31-SNAPSHOT-all.jar-c config.xml /var/tmp/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java

Starting audit...
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:30: 'switch' has incorrect indentation level 6, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:32: 'case' child has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:33: 'block' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:37: 'block' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:39: 'case' child has incorrect indentation level 14, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:40: 'case' child has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:43: 'case' child has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:44: 'block' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:45: 'block' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:53: 'block' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:54: 'block' child has incorrect indentation level 18, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:55: 'block rcurly' has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:59: 'block lcurly' has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:62: 'block rcurly' has incorrect indentation level 14, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:66: 'block lcurly' has incorrect indentation level 14, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:69: 'block rcurly' has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:76: 'case' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:81: 'case' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:89: 'switch rcurly' has incorrect indentation level 6, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:92: 'switch lcurly' has incorrect indentation level 6, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:93: 'switch rcurly' has incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:95: 'switch lcurly' has incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:96: 'switch rcurly' has incorrect indentation level 6, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:99: 'case' child has incorrect indentation level 8, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:100: 'if' has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:101: 'if' child has incorrect indentation level 16, expected level should be 20. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:102: 'else' has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:103: 'else' child has incorrect indentation level 16, expected level should be 20. [Indentation]
Audit done.
Checkstyle ends with 28 errors.

after update to log

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar /var/tmp/checkstyle/target/checkstyle-8.34-SNAPSHOT-all.jar -c config.xml /var/tmp/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java

Starting audit...
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:30:7: 'switch' has incorrect indentation level 6, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:32:11: 'case' child has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:33:15: 'block' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:37:15: 'block' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:39:15: 'case' child has incorrect indentation level 14, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:40:11: 'case' child has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:43:11: 'case' child has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:44:15: 'block' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:45:15: 'block' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:53:15: 'block' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:54:19: 'block' child has incorrect indentation level 18, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:55:11: 'block rcurly' has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:59:11: 'block lcurly' has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:62:15: 'block rcurly' has incorrect indentation level 14, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:66:15: 'block lcurly' has incorrect indentation level 14, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:69:11: 'block rcurly' has incorrect indentation level 10, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:76:15: 'case' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:81:15: 'case' child has incorrect indentation level 14, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:89:7: 'switch rcurly' has incorrect indentation level 6, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:92:7: 'switch lcurly' has incorrect indentation level 6, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:93:11: 'switch rcurly' has incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:95:11: 'switch lcurly' has incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:96:7: 'switch rcurly' has incorrect indentation level 6, expected level should be 8. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:99:9: 'case' child has incorrect indentation level 8, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:100:13: 'if' has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Desktop/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationInvalidSwitchIndent.java:102:13: 'else' has incorrect indentation level 12, expected level should be 16. [Indentation]
Audit done.
Checkstyle ends with 26 errors.
Process finished with exit code 26

@HuGanghui
Copy link
Contributor Author

HuGanghui commented Jun 4, 2020

test2:
/var/tmp $ cat Test.java

package checkstyle_demo.ast_indentation;

class Test {
    private static void foo() {
        int s = 3; //indent:8 exp:8

        switch (s) { //indent:8 exp:8
        case 1: //indent:8 exp:12 warn
            if (true) //indent:12 exp:16 warn
                break; //indent:16 exp:20 warn
            else //indent:12 exp:16 warn
                break; //indent:16 exp:20 warn
        } //indent:8 exp:8

    }

}

/var/tmp $ cat config.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="Indentation">
            <property name="arrayInitIndent" value="4"/>
            <property name="basicOffset" value="4"/>
            <property name="braceAdjustment" value="0"/>
            <property name="caseIndent" value="4"/>
            <property name="forceStrictCondition" value="false"/>
            <property name="lineWrappingIndentation" value="4"/>
            <property name="tabWidth" value="4"/>
            <property name="throwsIndent" value="4"/>
        </module>
    </module>
</module>

before update to log

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar /var/tmp/checkstyle-8.31-SNAPSHOT-all.jar -c config.xml Test.java

Starting audit...
[ERROR] /Users/hgh/Downloads/my_file/IDEA_workspace/Leetcode_maven/engineering_ability/src/main/java/checkstyle_demo/ast_indentation/Test.java:8: 'case' child has incorrect indentation level 8, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Downloads/my_file/IDEA_workspace/Leetcode_maven/engineering_ability/src/main/java/checkstyle_demo/ast_indentation/Test.java:9: 'if' has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Downloads/my_file/IDEA_workspace/Leetcode_maven/engineering_ability/src/main/java/checkstyle_demo/ast_indentation/Test.java:10: 'if' child has incorrect indentation level 16, expected level should be 20. [Indentation]
[ERROR] /Users/hgh/Downloads/my_file/IDEA_workspace/Leetcode_maven/engineering_ability/src/main/java/checkstyle_demo/ast_indentation/Test.java:11: 'else' has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Downloads/my_file/IDEA_workspace/Leetcode_maven/engineering_ability/src/main/java/checkstyle_demo/ast_indentation/Test.java:12: 'else' child has incorrect indentation level 16, expected level should be 20. [Indentation]
Audit done.
Checkstyle ends with 5 errors.
Process finished with exit code 5

after update to log

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar /var/tmp/checkstyle/target/checkstyle-8.34-SNAPSHOT-all.jar -c config.xml Test.java

Starting audit...
[ERROR] /Users/hgh/Downloads/my_file/IDEA_workspace/Leetcode_maven/engineering_ability/src/main/java/checkstyle_demo/ast_indentation/Test.java:8:9: 'case' child has incorrect indentation level 8, expected level should be 12. [Indentation]
[ERROR] /Users/hgh/Downloads/my_file/IDEA_workspace/Leetcode_maven/engineering_ability/src/main/java/checkstyle_demo/ast_indentation/Test.java:9:13: 'if' has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Downloads/my_file/IDEA_workspace/Leetcode_maven/engineering_ability/src/main/java/checkstyle_demo/ast_indentation/Test.java:10:17: 'if' child has incorrect indentation level 16, expected level should be 20. [Indentation]
[ERROR] /Users/hgh/Downloads/my_file/IDEA_workspace/Leetcode_maven/engineering_ability/src/main/java/checkstyle_demo/ast_indentation/Test.java:11:13: 'else' has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /Users/hgh/Downloads/my_file/IDEA_workspace/Leetcode_maven/engineering_ability/src/main/java/checkstyle_demo/ast_indentation/Test.java:12:17: 'else' child has incorrect indentation level 16, expected level should be 20. [Indentation]
Audit done.
Checkstyle ends with 5 errors.

Process finished with exit code 5

final int start = getLineStart(line);
// if must match is set, it is a violation if the line start is not
// at the correct indention level; otherwise, it is an only an
// violation if this statement starts the line and it is less than
// the correct indentation level
if (mustMatch && !indentLevel.isAcceptable(start)
|| !mustMatch && colNum == start && indentLevel.isGreaterThan(start)) {
logChildError(lineNum, start, indentLevel);
|| !mustMatch && ast.getColumnNo() == start && indentLevel.isGreaterThan(start)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I update indentation to log DetailAST, I run com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java to test, but one problem appear, this test failure:

There were some bugs in the code due to which it was not taking tabspaces, now I have fixed it in PR #8038.

Reason for Failure:

 final int columnNumber = expandedTabsColumnNo(ast);
        // if must match is set, it is a violation if the line start is not
        // at the correct indention level; otherwise, it is an only an
        // violation if this statement starts the line and it is less than
        // the correct indentation level
        if (mustMatch && !indentLevel.isAcceptable(start)
                || !mustMatch && colNum == start && indentLevel.isGreaterThan(start)) {
            logChildError(lineNum, start, indentLevel);
                || !mustMatch && columnNumber == start && indentLevel.isGreaterThan(start)) {
            logChildError(ast, start, indentLevel);

ast.getColumnNumber is creating the problem because ast.columnNumber() dont unfold the tabspaces so wrong linestart log were created by it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Abhishek-kumar09 , it is welcome if you are willing to continue to finish your PR on indentation. This PR is based on your PR and the reason that push this PR is that the original PR has not been replied for a long time

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HuGanghui I am working on it, Indentation is one of the most complex fields in Checkstyle. The changes is not only limited to one or two checks but there are many subchecks inside it,that create complexity.
Well you don't have to worry I will be fixing all those myself. You can untill focus on your next goals. Goodluck.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, lets @Abhishek-kumar09 to finish his PR ...
@HuGanghui , please keep your draft for now, we might come back to it if we loss connection to @Abhishek-kumar09 .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, got it, thanks @Abhishek-kumar09

@romani romani added the blocked label Jun 5, 2020
@HuGanghui
Copy link
Contributor Author

the PR should be closed, because the issue #7734 has been solved in #8038.

@HuGanghui HuGanghui closed this Jul 25, 2020
@HuGanghui HuGanghui deleted the indentation branch July 25, 2020 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants