Skip to content

Commit

Permalink
Fix PreferSafeLogger edge case that produced non-compiling code (#1851)
Browse files Browse the repository at this point in the history
Fix PreferSafeLogger edge case that produced suggested fixes that didn't compile without human interaction.
  • Loading branch information
carterkozak authored Jul 23, 2021
1 parent e78e392 commit 2ea1274
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
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()));",
" }",
"}")
.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();
}
}
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1851.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
fix:
description: Fix PreferSafeLogger edge case that produced suggested fixes that didn't
compile without human interaction.
links:
- https://github.com/palantir/gradle-baseline/pull/1851

0 comments on commit 2ea1274

Please sign in to comment.