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

Introduce RedundantStringEscape check #1138

Merged

Conversation

Stephan202
Copy link
Member

@Stephan202 Stephan202 commented Apr 14, 2024

Suggested commit message:

Introduce `RedundantStringEscape` check (#1138)

This check aims to simplify string constants by dropping redundant
single quote escape sequences. The check is optimized for performance.

While there, update existing checks such that they do not introduce
violations of the type flagged by this new check.

@Stephan202 Stephan202 added this to the 0.17.0 milestone Apr 14, 2024
@Stephan202 Stephan202 requested a review from rickie April 14, 2024 17:27
Copy link

Copy link

  • Surviving mutants in this change: 1
  • Killed mutants in this change: 31
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.RedundantStringEscape 1 21
🎉tech.picnic.errorprone.utils.SourceCode 0 5
🎉tech.picnic.errorprone.bugpatterns.StringJoin 0 1
🎉tech.picnic.errorprone.bugpatterns.Slf4jLogStatement 0 2
🎉tech.picnic.errorprone.guidelines.bugpatterns.ErrorProneRuntimeClasspath 0 2

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Override
public Description matchLiteral(LiteralTree tree, VisitorState state) {
String constant = ASTHelpers.constValue(tree, String.class);
if (constant == null || constant.indexOf('\'') == -1) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Pitest is correct that the || constant.indexOf('\'') == -1 condition can be dropped. It's here for performance reasons.

@werli werli self-requested a review April 15, 2024 16:03
Comment on lines 46 to 61
/**
* Returns a Java string constant expression (i.e., a quoted string) representing the given input.
*
* @apiNote This method differs from {@link com.sun.tools.javac.util.Constants#format(Object)} in
* that it does not superfluously escape single quote characters.
* @param str The string of interest.
* @return A non-{@code null} string.
*/
public static String toStringConstantExpression(CharSequence str) {
StringBuilder result = new StringBuilder("\"");
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '\'') {
result.append('\'');
} else {
result.append(Convert.quote(c));
}
}
return result.append('"').toString();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Just noticed we could also use the upstream VisitorState#getConstantExpression method, though this variant is more efficient.

Copy link
Member Author

Choose a reason for hiding this comment

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

I filed google/error-prone#4586; will update this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Rebased and added a commit.

Copy link

sonarqubecloud bot commented Aug 4, 2024

Copy link

github-actions bot commented Aug 4, 2024

  • Surviving mutants in this change: 1
  • Killed mutants in this change: 31
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.RedundantStringEscape 1 21
🎉tech.picnic.errorprone.utils.SourceCode 0 5
🎉tech.picnic.errorprone.bugpatterns.StringJoin 0 1
🎉tech.picnic.errorprone.bugpatterns.Slf4jLogStatement 0 2
🎉tech.picnic.errorprone.guidelines.bugpatterns.ErrorProneRuntimeClasspath 0 2

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202 Stephan202 modified the milestones: 0.18.0, 0.19.0 Aug 11, 2024
@Stephan202 Stephan202 force-pushed the sschroevers/introduce-redundant-string-escape-check branch from 96c8208 to 8ee3ce8 Compare September 21, 2024 15:00
Copy link

Copy link

  • Surviving mutants in this change: 2
  • Killed mutants in this change: 31
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.RedundantStringEscape 2 22
🎉tech.picnic.errorprone.utils.SourceCode 0 3
🎉tech.picnic.errorprone.bugpatterns.StringJoin 0 1
🎉tech.picnic.errorprone.guidelines.bugpatterns.ExhaustiveRefasterTypeMigration 0 1
🎉tech.picnic.errorprone.bugpatterns.Slf4jLogStatement 0 2
🎉tech.picnic.errorprone.guidelines.bugpatterns.ErrorProneRuntimeClasspath 0 2

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie modified the milestones: 0.19.0, 0.20.0 Oct 23, 2024
Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

Sorry for taking a long time to check this, it looks good to go!

@rickie rickie force-pushed the sschroevers/introduce-redundant-string-escape-check branch from 8ee3ce8 to 4ce0955 Compare November 12, 2024 10:11
Copy link

  • Surviving mutants in this change: 2
  • Killed mutants in this change: 31
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.RedundantStringEscape 2 22
🎉tech.picnic.errorprone.utils.SourceCode 0 3
🎉tech.picnic.errorprone.bugpatterns.StringJoin 0 1
🎉tech.picnic.errorprone.guidelines.bugpatterns.ExhaustiveRefasterTypeMigration 0 1
🎉tech.picnic.errorprone.bugpatterns.Slf4jLogStatement 0 2
🎉tech.picnic.errorprone.guidelines.bugpatterns.ErrorProneRuntimeClasspath 0 2

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202
Copy link
Member Author

@werli are you up for being the second reviewer? Otherwise I'll ask @mohamedsamehsalah 😄

@werli
Copy link
Member

werli commented Nov 12, 2024

@Stephan202 Yes, I got pinged by Rick. ETA is EOD. 👍

@Stephan202
Copy link
Member Author

GitHub should support the :rocket-intensifies: emoji 😄

Copy link
Member

@werli werli left a comment

Choose a reason for hiding this comment

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

One suggestion to consider, but I don't consider it blocking. Nice job 💪

import com.sun.source.tree.LiteralTree;
import tech.picnic.errorprone.utils.SourceCode;

/** A {@link BugChecker} that flags string constants with extraneous escaping. */
Copy link
Member

Choose a reason for hiding this comment

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

IIUC, this covers all unnecessary escaping cases in "single-line strings", but not text blocks. With text blocks, escalating double quotes may also be unnecessary but only if it's not the last character (because the text block would end with """").

I'm happy not (yet) to cover it, but should we document and make a note of it?


Or am I missing something since text blocks are a Java 17 feature and not yet an EPS concern?

Copy link
Member Author

Choose a reason for hiding this comment

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

Great suggestion! I'll have a look whether this isn't too much trouble to add, and add an XXX comment otherwise 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

I started working on this, but there are quite some edge cases and performance aspects to consider. As such I pushed only a commit with a comment to follow up on this topic. Will push a draft branch once this PR is merged.

@Stephan202 Stephan202 force-pushed the sschroevers/introduce-redundant-string-escape-check branch from 4ce0955 to 3e30dc5 Compare November 18, 2024 06:37
Copy link

  • Surviving mutants in this change: 2
  • Killed mutants in this change: 31
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.RedundantStringEscape 2 22
🎉tech.picnic.errorprone.utils.SourceCode 0 3
🎉tech.picnic.errorprone.bugpatterns.StringJoin 0 1
🎉tech.picnic.errorprone.guidelines.bugpatterns.ExhaustiveRefasterTypeMigration 0 1
🎉tech.picnic.errorprone.bugpatterns.Slf4jLogStatement 0 2
🎉tech.picnic.errorprone.guidelines.bugpatterns.ErrorProneRuntimeClasspath 0 2

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie
Copy link
Member

rickie commented Nov 18, 2024

LGTM, let's merge 🚀 ?

This check aims to simplify string constants by dropping redundant
single quote escape sequences. The check is optimized for performance.

While there, update existing checks such that they do not introduce
violations of the type flagged by this new check.
@Stephan202 Stephan202 force-pushed the sschroevers/introduce-redundant-string-escape-check branch from 3e30dc5 to f200a92 Compare November 18, 2024 19:06
Copy link

  • Surviving mutants in this change: 2
  • Killed mutants in this change: 31
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.RedundantStringEscape 2 22
🎉tech.picnic.errorprone.utils.SourceCode 0 3
🎉tech.picnic.errorprone.bugpatterns.StringJoin 0 1
🎉tech.picnic.errorprone.guidelines.bugpatterns.ExhaustiveRefasterTypeMigration 0 1
🎉tech.picnic.errorprone.bugpatterns.Slf4jLogStatement 0 2
🎉tech.picnic.errorprone.guidelines.bugpatterns.ErrorProneRuntimeClasspath 0 2

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202 Stephan202 merged commit fc9c200 into master Nov 18, 2024
16 checks passed
@Stephan202 Stephan202 deleted the sschroevers/introduce-redundant-string-escape-check branch November 18, 2024 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants