Skip to content

Issue #18614: Fix false positive in IndentationCheck for new operator - #19684

Merged
romani merged 1 commit into
checkstyle:masterfrom
mdhamed238:issue-18614-indentation-new-tabs
Jul 19, 2026
Merged

romani merged 1 commit into
checkstyle:masterfrom
mdhamed238:issue-18614-indentation-new-tabs

Conversation

@mdhamed238

@mdhamed238 mdhamed238 commented Apr 11, 2026 •

Copy link
Copy Markdown
Contributor

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.

@utafrali utafrali left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review.


public void testCorrectNewInTryCatch() { //indent:4 exp:4
try { //indent:8 exp:8
client = new Inner( //indent:12 exp:12

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I reverted that unrelated refactor and restored the original fileName local variable usage so the diff stays focused on the regression fix.

@mdhamed238

mdhamed238 commented Apr 11, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

Thanks! @utafrali Updated the PR description to match the actual test file (InputIndentationNewWithTabs.java) and current fix details.

@mdhamed238
mdhamed238 force-pushed the issue-18614-indentation-new-tabs branch 2 times, most recently from 27608e2 to 81b299f Compare April 11, 2026 17:09
@mdhamed238

Copy link
Copy Markdown
Contributor Author

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

@github-actions

Copy link
Copy Markdown
Contributor

Report generation failed. Please check the logs for more details.

Link: https://github.com/checkstyle/checkstyle/actions/runs/24313737712

@romani

romani commented Jul 16, 2026

Copy link
Copy Markdown
Member

please let us know if you have no time to finish it now, and somebody else can reuse your code to continue fix.

@mdhamed238

Copy link
Copy Markdown
Contributor Author

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.
I now have dedicated time to bring this to completion. I've rebased on the latest master and can address any remaining feedback promptly. Could you let me know if the report generation failure needs action on my end, or if it can be re-triggered? Thank you for your patience.

@romani

romani commented Jul 18, 2026

Copy link
Copy Markdown
Member

rebase you PR on latest, and I will glance as they finish.

@mdhamed238
mdhamed238 force-pushed the issue-18614-indentation-new-tabs branch from 81b299f to 3b9892e Compare July 18, 2026 02:41
@romani

romani commented Jul 18, 2026

Copy link
Copy Markdown
Member

@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.

@mdhamed238
mdhamed238 force-pushed the issue-18614-indentation-new-tabs branch 2 times, most recently from 4e04fa1 to 6a473cc Compare July 18, 2026 14:13
@romani

romani commented Jul 18, 2026

Copy link
Copy Markdown
Member

Please rebase one more time, we merged update for Indentation few minutes ago.
Thanks a lot in advance

@mdhamed238
mdhamed238 force-pushed the issue-18614-indentation-new-tabs branch 3 times, most recently from 3d448ea to d7b1fe9 Compare July 19, 2026 00:03
@mdhamed238
mdhamed238 force-pushed the issue-18614-indentation-new-tabs branch from d7b1fe9 to bb0d354 Compare July 19, 2026 00:32

@romani romani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

issue is fixed by some other update as side effect, thanks a lot for test extension !

@romani
romani merged commit db5a90f into checkstyle:master Jul 19, 2026
130 of 131 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.

3 participants