-
Notifications
You must be signed in to change notification settings - Fork 134
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
[Safe-logging] Verify member reference type parameters when cast to functional interface #3052
Conversation
51556ee
to
5c9dc78
Compare
Generate changelog in
|
...e-error-prone/src/main/java/com/palantir/baseline/errorprone/IllegalSafeLoggingArgument.java
Show resolved
Hide resolved
// If e.g. the cast type says it will return SAFE, but the reference returns UNSAFE, that's a problem | ||
// On the other hand, if the cast returns UNSAFE and the reference returns SAFE, that's fine | ||
if (!castTypeReturnSafety.allowsValueWith(referenceReturnSafety)) { | ||
return buildDescription(tree) |
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.
If we use state.reportMatch(
, we can report problems with the return type as well as parameters in the same evaluation. It's not really the standard way we like to use error-prone, but I do find it helpful to describe all the issues at once for the safelogging check specifically
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.
Great idea indeed!
...e-error-prone/src/main/java/com/palantir/baseline/errorprone/IllegalSafeLoggingArgument.java
Show resolved
Hide resolved
@Override | ||
public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) { | ||
// This is the type the reference gets "cast" into, whether through assignment, return type, argument, etc | ||
Type castType = state.getTypes().findDescriptorType(ASTHelpers.getType(tree)); |
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.
Should we check if ASTHelpers.getType(tree)
returns null or findDescriptorType
returns null and exit early?
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.
Good call!
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.
Fantastic work, I especially appreciate the inline comments!
Feel free to add "merge-when-ready" if you're ready to release this |
Released 6.16.0 |
Before this PR
Passing a member reference as a lambda will currently not catch cases where the type it gets passed to has different requirements around safety of the return or parameter types.
After this PR
==COMMIT_MSG==
[Safe-logging] Verify member reference type parameters when cast to functional interface
==COMMIT_MSG==
This is done by comparing the safety between the method reference and the functional interface it gets cast into, and verifying that:
Supplier<@Unsafe String> f = this::fun
whenfun
returns@Safe
, but not@Supplier<@Safe String> f = this::fun
whenfun
returns@Unsafe
, as in the latter case, you would then start using the unsafe result fromfun
as safe when callingf
Consumer<@Safe String> f = this::fun
whenfun
takes an@Unsafe
, but notConsumer<@Unsafe String> f = this::fun
whenfun
takes a@Safe
, as otherwise callingf.accept(s)
would let you pass an unsafe string tofun
To summarize:
Allowed
Forbidden
Possible downsides?