-
Notifications
You must be signed in to change notification settings - Fork 49
Add bug pattern for unchecked enum creation with valueOf #2031
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
base: master
Are you sure you want to change the base?
Conversation
|
Suggested commit message: |
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
c1ce1f0 to
918a7b1
Compare
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
| "", | ||
| " void convertSafe(String raw, B b) {", | ||
| " A.valueOf(\"ONE\");", | ||
| " var a = switch(b) { case TWO -> A.valueOf(b.name()); default -> null;};", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to traverse back to switch case statement somehow to capture possible values of b.name(). If they are filtered by the switch case this should not be a bug.
Question: Should we do a similar check for possible if-else statements? I think scope will be too large to capture everything.
| } | ||
|
|
||
| // Match unchecked String values | ||
| if (nameArgument instanceof IdentifierTree) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is enough to check if it is just a passed String.
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/EnumValueOfUsage.java
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/EnumValueOfUsage.java
Outdated
Show resolved
Hide resolved
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
| .filter( | ||
| argument -> | ||
| Optional.ofNullable(ASTHelpers.getType(argument)) | ||
| .filter(type -> type.toString().equals(String.class.getCanonicalName())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[javac] reported by reviewdog 🐶
[TypeToString] TypeMirror#toString shouldn't be used as it is expensive and fragile.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
|
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
|
I will try to resolve mutation test reports when I have the time. |



Context
Enum.valueOfmethod usage may lead to runtime exceptions when the provided argument is not checked against all possible values of method owner.Example:
Scope
Of course it is not able to capture all possible checks on
bbefore supplying it intoA.valueOf. Therefore scope will be limited to following checks.Above checks should also consider the polymorphic variant of
valueOf: