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

Fix PreferSafeLogger edge case that produced non-compiling code #1851

Merged
merged 2 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public Void visitMethodInvocation(MethodInvocationTree tree, Void _unused) {
if (sym.equals(ASTHelpers.getSymbol(receiver))) {
if (!isSafeSlf4jInteraction(tree, state)) {
foundUnknownUsage.set(true);
} else {
// Scan arguments for findings that may not compile
scan(tree.getArguments(), null);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,48 @@ void testPassedLoggerUnmodified() {
.expectUnchanged()
.doTest();
}

@Test
void testGetNameInArgUnmodified() {
// getName is not supported by SafeLogger, so we shouldn't make changes that won't compile.
fix().addInputLines(
"Test.java",
"import com.palantir.logsafe.*;",
"import org.slf4j.*;",
"class Test {",
" private static final Logger log = LoggerFactory.getLogger(Test.class);",
" void action() {",
" log.info(\"foo\", SafeArg.of(\"name\", log.getName()));",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously the log.getName() method invocation hid in the shadow of the parent log.info node, so the recommended fix wouldn't compile.

" }",
"}")
.expectUnchanged()
.doTest();
}

@Test
void testIsTraceEnabledInArg() {
fix().addInputLines(
"Test.java",
"import com.palantir.logsafe.*;",
"import org.slf4j.*;",
"class Test {",
" private static final Logger log = LoggerFactory.getLogger(Test.class);",
" void action() {",
" log.info(\"foo\", SafeArg.of(\"name\", log.isTraceEnabled()));",
" }",
"}")
.addOutputLines(
"Test.java",
"import com.palantir.logsafe.*;",
"import com.palantir.logsafe.logger.SafeLogger;",
"import com.palantir.logsafe.logger.SafeLoggerFactory;",
"import org.slf4j.*;",
"class Test {",
" private static final SafeLogger log = SafeLoggerFactory.get(Test.class);",
" void action() {",
" log.info(\"foo\", SafeArg.of(\"name\", log.isTraceEnabled()));",
" }",
"}")
.doTest();
}
}