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 CanonicalConstantFieldName bug checker #794

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

mohamedsamehsalah
Copy link
Contributor

@mohamedsamehsalah mohamedsamehsalah commented Sep 17, 2023

Suggested commit message:

Introduce `CanonicalConstantFieldName` check (#794)

@mohamedsamehsalah mohamedsamehsalah linked an issue Sep 17, 2023 that may be closed by this pull request
2 tasks
@github-actions
Copy link

  • Surviving mutants in this change: 5
  • Killed mutants in this change: 13
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 5 13

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

Copy link
Contributor Author

@mohamedsamehsalah mohamedsamehsalah left a comment

Choose a reason for hiding this comment

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

In addition to class variables within the same compilation unit, should we also match on constants imported from other classes ?
Ideally, the checker will suggest the fix in that class 🤔

Which raises the other question, do we really need to suggest a fix for the other classes referencing the constants in this compilation unit ?

@Stephan202 suggested the use of TreeScanner as in this example but I could not seem to make this work 😓

Help appreciated 🙏

pom.xml Outdated Show resolved Hide resolved
@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from 31a9e4d to 7fff49f Compare September 17, 2023 17:33
@github-actions
Copy link

  • Surviving mutants in this change: 5
  • Killed mutants in this change: 13
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 5 13

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

Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Tnx for working on this @mohamedsamehsalah; it'll allow for some nice cleanup I expect. Did leave some comments based on a quick skim :)

@Stephan202
Copy link
Member

Stephan202 commented Sep 17, 2023

(I didn't check the comments before reviewing; sorry.)

In addition to class variables within the same compilation unit, should we also match on constants imported from other classes ?
Ideally, the checker will suggest the fix in that class 🤔

Error Prone doesn't support this; for cases like that OpenRewrite would be better suited. (But for now, let's just focus on the private fields.)

@Stephan202 suggested the use of TreeScanner as in this example but I could not seem to make this work 😓

Anywhere we can see the code that doesn't work? Perhaps I can take a look.

@github-actions
Copy link

  • Surviving mutants in this change: 11
  • Killed mutants in this change: 14
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 11 14

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

@github-actions
Copy link

  • Surviving mutants in this change: 14
  • Killed mutants in this change: 28
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 14 28

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

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from 4acc2bc to ecdccc3 Compare September 23, 2023 20:14
@github-actions
Copy link

  • Surviving mutants in this change: 10
  • Killed mutants in this change: 56
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 8 34
🧟tech.picnic.errorprone.bugpatterns.RefasterMethodParameterOrder 1 20
🧟tech.picnic.errorprone.bugpatterns.RefasterMethodParameterOrder$1 1 2

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

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from ecdccc3 to 40a2a6f Compare September 23, 2023 20:21
@github-actions
Copy link

  • Surviving mutants in this change: 8
  • Killed mutants in this change: 34
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 8 34

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

@mohamedsamehsalah
Copy link
Contributor Author

@Stephan202 I covered your first pass comments, however, I still could not make use of the TreeScanner. Or in other words, I couldn't find what advantage does the TreeScanner give that the TreeMatcher does not.

AFAIU, the suggested fix builder used in this PR will execute what you are suggesting in this comment:

IIUC this change will suggest many distinct fixes. It might work in practice, but it'll be nicer if there's a single report against the field, which, when applied, renames all references to that Symbol. (That's why offline I suggested using a TreeScanner :)

Happy to hear your thoughts (again? 😅) 🙏

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from 40a2a6f to 0c94940 Compare September 23, 2023 20:29
@github-actions
Copy link

  • Surviving mutants in this change: 8
  • Killed mutants in this change: 34
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 8 34

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

Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

@Stephan202 I covered your first pass comments, however, I still could not make use of the TreeScanner. Or in other words, I couldn't find what advantage does the TreeScanner give that the TreeMatcher does not.

Actually, you are using a TreeScanner now, just through a method I didn't know existed: have a close look at SuggestedFixes.renameVariable 👀 😄 This should work!

Before I dive into a more thorough review: would you like to have a crack at resolving the surviving mutants?

if (!isVariableUpperSnakeCase(variableName) && !isVariableNameExcluded(variableName)) {
String replacement = toUpperSnakeCase(variableName);

if (!classVariables.contains(replacement)) {
Copy link
Member

Choose a reason for hiding this comment

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

IIUC, here we avoid renaming a variable if the target name is already declared in the exact same scope. Due to the global search and replace that we do, I suspect that any usage of the target name in this CompilationUnit could cause ambiguity, shadowing or confusion. So for this case, too, a TreeScanner may be employed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

would you like to have a crack at resolving the surviving mutants?

Yes, sir 🖖

IIUC, here we avoid renaming a variable if the target name is already declared in the exact same scope. Due to the global search and replace that we do, I suspect that any usage of the target name in this CompilationUnit could cause ambiguity, shadowing or confusion. So for this case, too, a TreeScanner may be employed.

Will look into this 👀

Thanks.

@github-actions
Copy link

github-actions bot commented Oct 1, 2023

  • Surviving mutants in this change: 4
  • Killed mutants in this change: 39
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 3 37
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName$1 1 2

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

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 36
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 2 34
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName$1 1 2

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

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch 2 times, most recently from 1c55636 to 12e51ab Compare October 2, 2023 06:53
@github-actions
Copy link

github-actions bot commented Oct 2, 2023

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 36
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 2 34
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName$1 1 2

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

@sonarcloud
Copy link

sonarcloud bot commented Oct 2, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 4 Code Smells

94.9% 94.9% Coverage
0.0% 0.0% Duplication

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 36
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 2 34
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName$1 1 2

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

Copy link
Contributor Author

@mohamedsamehsalah mohamedsamehsalah 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 the slow progress on this @Stephan202 , PTAL 🙏

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from 12e51ab to a7f652e Compare November 25, 2023 20:42
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.

As a result of dropping, we can simplify the code, pushing a suggestion!

Copy link

github-actions bot commented Apr 5, 2024

  • Surviving mutants in this change: 1
  • Killed mutants in this change: 25
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName$1 1 0
🎉tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 0 25

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

@rickie
Copy link
Member

rickie commented Apr 5, 2024

/integration-test

@mohamedsamehsalah
Copy link
Contributor Author

Thanks @rickie , changes LGMT 🚀

Copy link

github-actions bot commented Apr 8, 2024

  • Surviving mutants in this change: 1
  • Killed mutants in this change: 28
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantNaming$1 1 0
🎉tech.picnic.errorprone.bugpatterns.CanonicalConstantNaming 0 28

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

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.

Hey @mohamedsamehsalah , thanks a lot for pushing this! It looks really nice and am curious to see this getting rolled out on Picnic's codebase 🔥 !!!

linkType = CUSTOM,
severity = WARNING,
tags = LIKELY_ERROR)
public final class CanonicalConstantFieldName extends BugChecker implements VariableTreeMatcher {
Copy link
Member

Choose a reason for hiding this comment

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

I think we can omit the word "field" from most things in this class. As a constant is actually always a field right? 🤔

private static final Matcher<Tree> IS_CONSTANT =
allOf(
hasModifier(Modifier.STATIC), hasModifier(Modifier.PRIVATE), hasModifier(Modifier.FINAL));
private static final Pattern TO_SNAKE_CASE = Pattern.compile("([a-z])([A-Z])");
Copy link
Member

Choose a reason for hiding this comment

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

This is actually not the TO_ snake case, it's the pattern for SNAKE_CASE, right?

private static final Pattern TO_SNAKE_CASE = Pattern.compile("([a-z])([A-Z])");
private static final ImmutableSet<String> DEFAULT_EXCLUDED_CONSTANT_FIELD_NAMES =
ImmutableSet.of("serialVersionUID");
private static final String EXCLUDED_CONSTANT_FIELD_NAMES =
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private static final String EXCLUDED_CONSTANT_FIELD_NAMES =
private static final String ALLOWED_CONSTANT_NAMES_FLAG =

Let's add the FLAG suffix and maybe instead of excluded maybe we should use the word "allowed", WDYT?

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
String variableName = tree.getName().toString();
if (IS_CONSTANT.matches(tree, state)
Copy link
Member

Choose a reason for hiding this comment

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

If we flip the conditions here and do an early return, we have one level of indentation less. Additionally, we generally try to have early returns if possible :).

return Description.NO_MATCH;
}

private static ImmutableList<VariableTree> getVariablesInCompilationUnit(
Copy link
Member

Choose a reason for hiding this comment

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

Changed the order of static methods in the class based on which one is used first :).

ImmutableList<VariableTree> variablesInCompilationUnit =
getVariablesInCompilationUnit(state.getPath().getCompilationUnit());
String replacement = toUpperSnakeCase(variableName);
if (variablesInCompilationUnit.stream()
Copy link
Member

Choose a reason for hiding this comment

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

Just a suggestion but what if we extract this to a method, I think it reads nicely. Additionally if we use anyMatch instead of noneMatch it reads as "isVariableNameInUse`, instead of the negated variant, could be a bit easier to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

More readable, I agree 💯

void identification() {
CompilationTestHelper.newInstance(CanonicalConstantFieldName.class, getClass())
.addSourceLines(
"A.java",
Copy link
Member

Choose a reason for hiding this comment

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

Added a few more tests :).

Copy link

github-actions bot commented Apr 8, 2024

  • Surviving mutants in this change: 1
  • Killed mutants in this change: 28
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantNaming$1 1 0
🎉tech.picnic.errorprone.bugpatterns.CanonicalConstantNaming 0 28

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

@rickie rickie force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from 9605dfe to 0728480 Compare April 8, 2024 06:29
Copy link

github-actions bot commented Apr 8, 2024

  • Surviving mutants in this change: 1
  • Killed mutants in this change: 28
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantNaming$1 1 0
🎉tech.picnic.errorprone.bugpatterns.CanonicalConstantNaming 0 28

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

Copy link

sonarcloud bot commented Apr 8, 2024

Copy link

github-actions bot commented Apr 8, 2024

  • Surviving mutants in this change: 1
  • Killed mutants in this change: 28
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantNaming$1 1 0
🎉tech.picnic.errorprone.bugpatterns.CanonicalConstantNaming 0 28

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

@mohamedsamehsalah
Copy link
Contributor Author

Thanks for suggestions @rickie . LGTM 🚀

@rickie rickie added this to the 0.17.0 milestone Apr 8, 2024
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.

Canonical constant naming
3 participants