Repository navigation
Issue #18614: Fix false positive in IndentationCheck for new operator - #19684
Conversation
utafrali
left a comment
There was a problem hiding this comment.
The one-line fix in NewHandler#getIndentImpl is correct: the old code mixed visual (tab-expanded) and raw column coordinates, which is now fixed by reusing the existing isOnStartOfLine helper. The test file exercises the fixed code path via testCorrectNewLineWrapped, but the testCorrectNewInTryCatch method does not actually test new at the start of a line (where the bug triggers), and an unrelated inline refactoring in testTryBlockConstructorParameters adds unnecessary diff noise. Also, the PR description names the test file as InputIndentationTryBlockNewIssue.java when the actual file added is InputIndentationNewWithTabs.java.
| // if our expression isn't first on the line, just use the start | ||
| // of the line | ||
| if (getLineStart(mainAst) == mainAst.getColumnNo()) { | ||
| if (isOnStartOfLine(mainAst)) { |
There was a problem hiding this comment.
The fix is correct. getLineStart(mainAst) returns the visual (tab-expanded) column of the line's first non-whitespace character, while mainAst.getColumnNo() returns the raw token column (each tab counts as 1). With tab indentation these two numbers diverge even when new is at the start of the line, causing the condition to be unexpectedly false and falling into the else branch. Replacing it with isOnStartOfLine(mainAst) (defined as getLineStart(ast) == expandedTabsColumnNo(ast)) makes both sides use the same coordinate system, which is consistent with how checkIndentation() already checks the same token at line 61 of this same class.
There was a problem hiding this comment.
Thanks for the review.
|
|
||
| public void testCorrectNewInTryCatch() { //indent:4 exp:4 | ||
| try { //indent:8 exp:8 | ||
| client = new Inner( //indent:12 exp:12 |
There was a problem hiding this comment.
In testCorrectNewInTryCatch, new appears on the same line as client =, so it is NOT at the start of a line. isOnStartOfLine(mainAst) returns false for it, which means the fixed code path in getIndentImpl is never triggered by this method. The regression described in the issue title (false positive for new inside a try block) is not directly covered here. Consider adding a case where new is on its own line inside a try block, e.g.:
try {
client =
new Inner(null); //indent:16 exp:16
} catch (Exception e) {
}The existing testCorrectNewLineWrapped does exercise the fix, but a try-block-specific case would make the regression more traceable to the original issue.
There was a problem hiding this comment.
Good catch, thanks. I updated testCorrectNewInTryCatch so new is now on its own wrapped line inside the try block, which directly exercises the isOnStartOfLine(mainAst) path in getIndentImpl.
| checkConfig.addProperty("tabWidth", "4"); | ||
|
|
||
| final String fileName = getPath("InputIndentationTryCtorParams.java"); | ||
| final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; |
There was a problem hiding this comment.
The change to testTryBlockConstructorParameters (inlining fileName into the verifyWarns call) is unrelated to the bug fix and adds noise to the diff. It makes it harder to spot that this existing test was not modified for any functional reason. Please revert this change so the diff stays focused on the regression test.
There was a problem hiding this comment.
Agreed. I reverted that unrelated refactor and restored the original fileName local variable usage so the diff stays focused on the regression fix.
Thanks! @utafrali Updated the PR description to match the actual test file ( |
27608e2 to
81b299f
Compare
|
Github, generate report for Indentation/all-examples-in-one |
|
Report generation failed. Please check the logs for more details. |
|
please let us know if you have no time to finish it now, and somebody else can reuse your code to continue fix. |
|
Apologies for the long silence on this PR. After pushing my last changes, all CI checks passed locally, but the report generation step failed, and I was unsure whether the issue was on my side or in the pipeline. I decided to wait for reviewer input before making further changes, and the PR ended up stalling longer than intended. |
|
rebase you PR on latest, and I will glance as they finish. |
81b299f to
3b9892e
Compare
|
@mdhamed238 , there is still conflict. Please read and watch playlist videos at Starting_Development. it explains how to deal with Github ,plase read and watch. |
4e04fa1 to
6a473cc
Compare
|
Please rebase one more time, we merged update for Indentation few minutes ago. |
3d448ea to
d7b1fe9
Compare
d7b1fe9 to
bb0d354
Compare
romani
left a comment
There was a problem hiding this comment.
issue is fixed by some other update as side effect, thanks a lot for test extension !
Issue: #18614
Problem:
IndentationCheck reported false positives for the new operator when tabs were used for indentation.
In NewHandler#getIndentImpl, the old condition compared values from different column systems
(visual/tab expanded vs raw token column), which could mis-detect whether new starts the line.
Fix:
Updated NewHandler#getIndentImpl to use isOnStartOfLine(mainAst), so the check consistently uses tab-expanded visual columns.
Testing:
Added/updated regression coverage in InputIndentationNewWithTabs.java and IndentationCheckTest#testNewWithTabsIndentation, including a try-block case where line-wrapped new is at the start of the line to exercise the fixed path directly. Also reverted unrelated refactoring in testTryBlockConstructorParameters to keep this PR focused.