Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -26,7 +26,6 @@
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.CompileTimeConstantExpressionMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
Expand All @@ -49,8 +48,6 @@ public final class LogsafeArgName extends BugChecker implements MethodInvocation

private static final Matcher<ExpressionTree> SAFE_ARG_OF =
Matchers.staticMethod().onClass("com.palantir.logsafe.SafeArg").named("of");
private final Matcher<ExpressionTree> compileTimeConstExpressionMatcher =
new CompileTimeConstantExpressionMatcher();

private final Set<String> unsafeParamNames;

Expand All @@ -72,7 +69,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState

List<? extends ExpressionTree> args = tree.getArguments();
ExpressionTree argNameExpression = args.get(0);
if (compileTimeConstExpressionMatcher.matches(argNameExpression, state)) {
if (argNameExpression instanceof JCTree.JCLiteral) {
String argName = (String) ((JCTree.JCLiteral) argNameExpression).getValue();
if (unsafeParamNames.stream().anyMatch(unsafeArgName -> unsafeArgName.equalsIgnoreCase(argName))) {
SuggestedFix.Builder builder = SuggestedFix.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ public void ignores_safe_arg_names() {
.doTest();
}

@Test
public void ignores_identifier_arg_names() {
getCompilationHelper()
.addSourceLines(
"Test.java",
"import com.palantir.logsafe.SafeArg;",
"import java.lang.String;",
"class Test {",
" static final String NAME = \"name\";",
" void f() {",
" SafeArg.of(NAME, 1);",
" }",
"",
"}")
.doTest();
}

private static RefactoringValidator getRefactoringHelper() {
return RefactoringValidator.of(
new LogsafeArgName(ErrorProneFlags.builder()
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1465.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: The `LogsafeArgName` now ignores arg names that are not literals.
links:
- https://github.com/palantir/gradle-baseline/pull/1465