Skip to content

Java: Add query to detect special characters in string literals #19875

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tamasvajk
Copy link
Contributor

@tamasvajk tamasvajk commented Jun 25, 2025

This pull request introduces a new query to detect non-explicit control and whitespace characters in Java literals, improving code readability and reducing potential bugs caused by invisible or hard-to-recognize characters.

Manually testing autofix works, the special characters are replaced with \u0....

@@ -0,0 +1 @@
Violations of Best Practice/SpecialCharactersInLiterals/NonExplicitControlAndWhitespaceCharsInLiterals.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you consider an inline expectations test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I quickly turned it into an inline expectations test, but it failed, I think due to the fact that the strings contain multiple special characters. So then I reverted it, and kept it as it is.

@tamasvajk tamasvajk force-pushed the quality/spec_chars branch 3 times, most recently from e1b966e to 9db9200 Compare June 25, 2025 12:15
@tamasvajk tamasvajk added the no-change-note-required This PR does not need a change note label Jun 25, 2025
@tamasvajk tamasvajk force-pushed the quality/spec_chars branch from 9db9200 to eeaaa87 Compare June 27, 2025 08:32
@tamasvajk tamasvajk force-pushed the quality/spec_chars branch from eeaaa87 to 834cb1b Compare July 1, 2025 09:38
@tamasvajk tamasvajk marked this pull request as ready for review July 1, 2025 12:07
@tamasvajk tamasvajk requested a review from a team as a code owner July 1, 2025 12:07
Comment on lines +10 to +12
* correctness
* maintainability
* readability
Copy link
Contributor

Choose a reason for hiding this comment

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

Just checking: you believe this matches both the correctness and readability categories?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think readability is definitely okay. correctness might/could be challenged. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it can belong to both. But between maintainability and reliability, you think it's better to choose maintainability, because readability is definitely an issue whereas correctness is only occasionally an issue?

@@ -0,0 +1 @@
Violations of Best Practice/SpecialCharactersInLiterals/NonExplicitControlAndWhitespaceCharsInLiterals.ql
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you consider an inline expectations test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Java no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants