Skip to content

Issue #8965: fix false-negative on new keyword's children's Indentation #17181

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

Merged
merged 1 commit into from
Jun 24, 2025

Conversation

SecondClassLongNam7(getString(100000, "Long" //indent:48 exp:48
+ "Foo><"))) || conNoArg() //indent:52 exp:52
|| conNoArg() || //indent:56 exp:56
conNoArg() || conNoArg()) {} //indent:60 exp:60
Copy link
Member Author

Choose a reason for hiding this comment

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

@Zopsss these two files go out of length limit.

Copy link
Member

Choose a reason for hiding this comment

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

is there way to change variables to be shorter and less crazy ?
if all such long names matters, we can add Input to suppression, but I think we should try shortening of Input, such creazy Input code is not readable and nobody can say what is reasonable indentation should be. It might be copy pasted from other Input.

Copy link
Member

Choose a reason for hiding this comment

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

Yup I also think this is very crazy input

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. and it was not easy. 🥲

@Zopsss
Copy link
Member

Zopsss commented Jun 14, 2025

@mohitsatr any update on this?

@mohitsatr mohitsatr force-pushed the new-indent branch 3 times, most recently from ffd119e to 493aa59 Compare June 17, 2025 12:01
@mohitsatr
Copy link
Member Author

Github, generate report

Copy link
Contributor

@romani
Copy link
Member

romani commented Jun 18, 2025

@mohitsatr , can we make progress on this ?
we are about to release hot fix release for indentation, one update is already merged.

@mohitsatr
Copy link
Member Author

mohitsatr commented Jun 19, 2025

@romani
regression report is good. we can merge this as soon as these other PRs get merged.

@mohitsatr
Copy link
Member Author

mohitsatr commented Jun 19, 2025

@Zopsss should I exclude and make InputFormatted.. file in this case too. ?

@Zopsss
Copy link
Member

Zopsss commented Jun 19, 2025

Yes. If we need violation and formatter complains for it then we need to exclude that file and make InputFormatted file for it so formatter can be happy.

mohitsatr added a commit to mohitsatr/sevntu.checkstyle that referenced this pull request Jun 20, 2025
@mohitsatr mohitsatr force-pushed the new-indent branch 2 times, most recently from 8895496 to 7b9aecd Compare June 20, 2025 03:26
davecramer pushed a commit to pgjdbc/pgjdbc that referenced this pull request Jun 20, 2025
@Zopsss
Copy link
Member

Zopsss commented Jun 21, 2025

@mohitsatr
Copy link
Member Author

@Zopsss I fixed it pgjdbc/pgjdbc#3682.
CI needs to be restarted.

@Zopsss
Copy link
Member

Zopsss commented Jun 21, 2025

@romani can you restart CI?

@romani
Copy link
Member

romani commented Jun 21, 2025

No need to restart, it is CS violations:

> Task :postgresql:checkstyleMain
[ant:checkstyle] [ERROR] /home/circleci/project/.ci-temp/pgjdbc/pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java:74:9: 'new' child has incorrect indentation level 8, expected level should be 10. [Indentation]
[ant:checkstyle] [ERROR] /home/circleci/project/.ci-temp/pgjdbc/pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java:75:9: 'new' child has incorrect indentation level 8, expected level should be 10. [Indentation]

> Task :postgresql:checkstyleMain FAILED
  static {
    try {
      Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
      throw new IllegalStateException(
          "BaseDataSource is unable to load org.postgresql.Driver. Please check if you have proper PostgreSQL JDBC Driver jar on the classpath",
          e);
      // violation above and 2 lines above 
    }
  }

and

[INFO] --- antrun:3.1.0:run (ant-phase-verify) @ sevntu-checks --- 02:36
[INFO] Executing tasks 02:36
[INFO]      [echo] Checkstyle started (../../../config/checkstyle-checks.xml): 20/06/2025 03:30:47 AM 02:36
[INFO] [checkstyle] Running Checkstyle 10.25.1-SNAPSHOT on 141 files 02:38
[ERROR] [checkstyle] [ERROR] /home/semaphore/checkstyle/.ci-temp/sevntu.checkstyle/sevntu-checks/src/test/java/com/github/sevntu/checkstyle/internal/ChecksTest.java:88:29: 'new' child has incorrect indentation level 28, expected level should be 32. [Indentation] 02:48

https://github.com/sevntu-checkstyle/sevntu.checkstyle/blob/ca6d8c536254c0762e38ecac2e015578dd596a0c/sevntu-checks/src/test/java/com/github/sevntu/checkstyle/internal/ChecksTest.java#L85-L88

            validateEclipseCsMetaXmlFile(
                    new File(getEclipseCsPath(packageName
                            + "/checkstyle-metadata.xml")), packageName, new HashSet<>(
                            pkgModules));  // violation

I think all of them are false positives.

@mohitsatr
Copy link
Member Author

mohitsatr commented Jun 22, 2025

I have added both the cases to the inputs.

for sevntu case,
I think there is mismatch in config values.
Notice, this file uses lineWrappingIndentation value of 8,

        for (String packageName : packages) {
            Assertions.assertTrue(new File(
                    getEclipseCsPath(packageName)).exists(), "folder " + packageName 
                    + " must exist in eclipsecs");   


            final Set<Class<?>> pkgModules = CheckUtil.getModulesInPackage(modules, packageName);


            validateEclipseCsMetaXmlFile(
                    new File(getEclipseCsPath(packageName
                            + "/checkstyle-metadata.xml")), packageName, new HashSet<>(
                            pkgModules));

but it is getting tested on lineWrappingIndentation of 4 from checkstyle-checks.xml

[INFO]      [echo] Checkstyle started (../../../config/checkstyle-checks.xml): 20/06/2025 03:30:47 AM 02:36  
                                                         ^^^
[INFO] [checkstyle] Running Checkstyle 10.25.1-SNAPSHOT on 141 files 02:38
[ERROR] [checkstyle] [ERROR] /home/semaphore/checkstyle/.ci-temp/sevntu.checkstyle/sevntu-checks/src/test/java/com/github/sevntu/checkstyle/internal/ChecksTest.java:88:29: 'new' child has incorrect indentation level 28, expected level should be 32. [Indentation] 02:48

lineWrappingIndentation has default value of 4.

<module name="Indentation">
<property name="basicOffset" value="4"/>
<property name="braceAdjustment" value="0"/>
<property name="caseIndent" value="4"/>
<property name="throwsIndent" value="8"/>
</module>

@romani
Copy link
Member

romani commented Jun 22, 2025

Github, generate report

@romani
Copy link
Member

romani commented Jun 22, 2025

Github, generate report for Indentation/all-examples-in-one

Copy link
Contributor

@mohitsatr
Copy link
Member Author

no change in report.

Copy link
Contributor

@romani
Copy link
Member

romani commented Jun 22, 2025

@mohitsatr ,

Please add this simple code snippet to input.
We have crazy inputs, but we lack simple real code that slightly not correct.

https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/adec420f62d6623a0db14d414ea13c0b86129121_2025062740/reports/diff/spoon/index.html#A1

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

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

Minor test extension is required, but in general ready for merge

@mohitsatr mohitsatr force-pushed the new-indent branch 2 times, most recently from f1aedea to ec79307 Compare June 23, 2025 03:28
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

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

@mohitsatr
Copy link
Member Author

mohitsatr commented Jun 23, 2025

@romani was my reasoning right at #17181 (comment) ?

Comment on lines +12 to +18
public Object foo() {
return Optional.empty()
.orElseThrow(
() ->
new IllegalArgumentException(
"Something wrong 1, something wrong 2, something wrong 3"));
}
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add a similar case like following:

  public Object foo() { 
     return Optional.empty() 
         .orElseThrow( 
             () ->  new IllegalArgumentException( 
                     "Something wrong 1, something wrong 2, something wrong 3")); 
   }

Sorry I'm commenting from phone so indentation might be wrong.

Also try to add a violation case with incorrect indentation for the last line.


https://google.github.io/styleguide/javaguide.html#s4.5.1-line-wrapping-where-to-break

A line is never broken adjacent to the arrow in a lambda or a switch rule, except that a break may come immediately after the arrow if the text following it consists of a single unbraced expression

It says a break "may" come, so I think it is not compulsory to have a break after -> so my suggested case should not give violation.

Copy link
Member

Choose a reason for hiding this comment

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

Also we should add this case to main indentation inputs too

@mohitsatr mohitsatr force-pushed the new-indent branch 2 times, most recently from f187405 to ffeeb06 Compare June 24, 2025 04:08
Comment on lines +65 to +66
() -> new IllegalArgumentException(
"something wrong 1, something wrong 2, something wrong 3"));
Copy link
Member

Choose a reason for hiding this comment

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

Is the Indentation wrong here? "Something wrong 1.... shouldn't have 2 more spaces?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it is correct. lineWrappingIndentation is 4.
line 65 has indentation of 12, so line 66 have indentation of 16

Comment on lines +61 to +76
/** some data. */
public Object foo4(int data) {
return Optional.empty()
.orElseThrow(
() -> new IllegalArgumentException(
"something wrong 1, something wrong 2, something wrong 3"));
}

/** some data. */
public Object foo5(int data) {
return Optional.empty()
.orElseThrow(
() -> new IllegalArgumentException(
"something wrong 1, something wrong 2, something wrong 3"));
// violation above '.* incorrect indentation level 8, expected .* 16.'
}
Copy link
Member

Choose a reason for hiding this comment

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

Please add these cases to src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationNewChildren.java as well

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Please add correct case also

@Zopsss
Copy link
Member

Zopsss commented Jun 24, 2025

@romani ok to merge if ci passes

@romani romani merged commit 846d71b into checkstyle:master Jun 24, 2025
119 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Indentation check doesn't give violation when google formatter does
3 participants