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 #14716: Fix the violation for modifiers wrapped in class def #14743

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -24,6 +24,7 @@
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;

/**
* Abstract base class for all handlers.
Expand Down Expand Up @@ -500,6 +501,10 @@ protected void checkModifiers() {
modifier != null;
modifier = modifier.getNextSibling()) {
if (isOnStartOfLine(modifier)
&& (!TokenUtil.isOfType(modifier,
TokenTypes.ANNOTATION, TokenTypes.STRICTFP, TokenTypes.LITERAL_STATIC)
|| modifier.getPreviousSibling() == null
|| !isPrecededByAnyModifier(modifier))
Comment on lines +504 to +507
Copy link
Member

@rnveach rnveach Apr 1, 2024

Choose a reason for hiding this comment

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

@romani @nrmancuso

I am continuing on the discussion at #14716 (comment) , I don't see how skipping specific tokens will serve a benefit. I don't think we should be reviewing this PR until we have some agreement here. There doesn't seem to be any clear reason why we are targeting these tokens. For example, why is static on this list and not public? How did we choose these specific tokens that we can explain to others.

Including @nrmancuso 's original comments, the input file in this PR needs to be vastly expanded.

We need a duplicate input file with all indentation at 0 showing the violations produced by bad code. Regression was done on good formatted code, so it will provide less value for this check in those terms. We should also do force strict as it will tell us exactly what indentation should be and when it is absolutely correct and not just meeting the min requirement.

We also need inputs where these tokens being skipped are completely alone on a line and are the first AST in the class def. All the current examples start with public, abstract, final which are not any of the tokens listed here.

Copy link
Member

Choose a reason for hiding this comment

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

There doesn't seem to be any clear reason why we are targeting these tokens

Yep, my sentiments exactly.

&& !getIndent().isAcceptable(expandedTabsColumnNo(modifier))) {
logError(modifier, "modifier",
expandedTabsColumnNo(modifier));
Expand Down Expand Up @@ -590,4 +595,20 @@ && isOnStartOfLine(lparen)) {
}
}

/**
* Check whether given node a preceding access modifier or not.
*
* @param node the node whose predecessors are to be checked.
* @return true if node is preceded by an access modifier else false.
*/
private boolean isPrecededByAnyModifier(DetailAST node) {
DetailAST tempNode = node;
while (tempNode.getPreviousSibling() != null
&& !TokenUtil.isOfType(tempNode.getPreviousSibling(),
TokenTypes.LITERAL_PUBLIC, TokenTypes.FINAL, TokenTypes.ABSTRACT)) {
tempNode = tempNode.getPreviousSibling();
}
return tempNode.getPreviousSibling() != null;
}

}
Expand Up @@ -3019,6 +3019,22 @@ public void testIndentationLineBreakVariableDeclaration()
verifyWarns(checkConfig, fileName, expected);
}

@Test
public void testClassDeclarationWrapped() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
checkConfig.addProperty("tabWidth", "4");
checkConfig.addProperty("basicOffset", "2");
checkConfig.addProperty("braceAdjustment", "2");
checkConfig.addProperty("lineWrappingIndentation", "4");
checkConfig.addProperty("caseIndent", "2");
checkConfig.addProperty("throwsIndent", "2");
checkConfig.addProperty("arrayInitIndent", "2");

final String fileName = getPath("InputIndentationClassDeclarationWrapped.java");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWarns(checkConfig, fileName, expected);
}

private static final class IndentAudit implements AuditListener {

private final IndentComment[] comments;
Expand Down
@@ -0,0 +1,30 @@
package com.puppycrawl.tools.checkstyle.checks.indentation.indentation;//indent:0 exp:0

public class InputIndentationClassDeclarationWrapped {//indent:0 exp:0

public class A {};//indent:2 exp:2

public//indent:2 exp:2
class B {};//indent:6 exp:6

final//indent:2 exp:2
static class C {};//indent:6 exp:6

abstract//indent:2 exp:2
strictfp class G {};//indent:6 exp:6

public//indent:2 exp:2
class T {};//indent:6 exp:6

public//indent:2 exp:2
@X class D {};//indent:6 exp:6

abstract @Y//indent:2 exp:2
@Z strictfp class E {};//indent:6 exp:6

@X class F {};//indent:2 exp:2

@interface X{}//indent:2 exp:2
@interface Y{}//indent:2 exp:2
@interface Z{}//indent:2 exp:2
}//indent:0 exp:0