From 24bbd6c9ece91a2ba89b1c9cc493e38ad91742c4 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Mon, 19 Aug 2019 09:36:18 -0400 Subject: [PATCH] fix #511: Remove redundant errorprone `LogSafePreconditionsConstantMessage` (#755) Remove errorprone `LogSafePreconditionsConstantMessage` --- .../LogSafePreconditionsConstantMessage.java | 71 ------------------- ...SafePreconditionsConstantMessageTests.java | 70 ------------------ changelog/@unreleased/pr-755.v2.yml | 5 ++ 3 files changed, 5 insertions(+), 141 deletions(-) delete mode 100644 baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LogSafePreconditionsConstantMessage.java delete mode 100644 baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LogSafePreconditionsConstantMessageTests.java create mode 100644 changelog/@unreleased/pr-755.v2.yml diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LogSafePreconditionsConstantMessage.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LogSafePreconditionsConstantMessage.java deleted file mode 100644 index 86bd8546e..000000000 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LogSafePreconditionsConstantMessage.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.palantir.baseline.errorprone; - -import com.google.auto.service.AutoService; -import com.google.errorprone.BugPattern; -import com.google.errorprone.BugPattern.SeverityLevel; -import com.google.errorprone.VisitorState; -import com.google.errorprone.bugpatterns.BugChecker; -import com.google.errorprone.matchers.CompileTimeConstantExpressionMatcher; -import com.google.errorprone.matchers.Description; -import com.google.errorprone.matchers.Matcher; -import com.google.errorprone.matchers.method.MethodMatchers; -import com.sun.source.tree.ExpressionTree; -import com.sun.source.tree.MethodInvocationTree; -import java.util.List; -import java.util.regex.Pattern; - -@AutoService(BugChecker.class) -@BugPattern( - name = "LogSafePreconditionsConstantMessage", - severity = SeverityLevel.ERROR, - summary = "Allow only constant messages to logsafe Preconditions.checkX() methods") -public final class LogSafePreconditionsConstantMessage - extends BugChecker implements BugChecker.MethodInvocationTreeMatcher { - - private static final long serialVersionUID = 1L; - - private static final Matcher PRECONDITIONS_METHOD = MethodMatchers.staticMethod() - .onClass("com.palantir.logsafe.Preconditions") - .withNameMatching(Pattern.compile("checkArgument|checkState|checkNotNull")); - - private final Matcher compileTimeConstExpressionMatcher = - new CompileTimeConstantExpressionMatcher(); - - @Override - public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { - if (!PRECONDITIONS_METHOD.matches(tree, state)) { - return Description.NO_MATCH; - } - - List args = tree.getArguments(); - if (args.size() <= 1) { - return Description.NO_MATCH; - } - - ExpressionTree messageArg = args.get(1); - - if (compileTimeConstExpressionMatcher.matches(messageArg, state)) { - return Description.NO_MATCH; - } - - return buildDescription(tree).setMessage( - "Preconditions.checkX() statement uses a non-constant message. " - + "Dynamic components are required to be wrapped with either SafeArg or UnsafeArg.").build(); - } -} diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LogSafePreconditionsConstantMessageTests.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LogSafePreconditionsConstantMessageTests.java deleted file mode 100644 index 258cff293..000000000 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LogSafePreconditionsConstantMessageTests.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.palantir.baseline.errorprone; - -import com.google.errorprone.CompilationTestHelper; -import org.junit.Before; -import org.junit.Test; - -public final class LogSafePreconditionsConstantMessageTests extends PreconditionsTests { - - private static final String DIAGNOSTIC = "non-constant message"; - - private CompilationTestHelper compilationHelper; - - @Before - public void before() { - compilationHelper = CompilationTestHelper.newInstance(LogSafePreconditionsConstantMessage.class, getClass()); - } - - @Override - public CompilationTestHelper compilationHelper() { - return compilationHelper; - } - - @Test - public void testCheckArgument_stringConcatenate() { - failLogSafe(DIAGNOSTIC, "Preconditions.checkArgument(param != \"string\", \"constant\" + param);"); - } - - @Test - public void testCheckArgument_stringFormat() { - failLogSafe(DIAGNOSTIC, - "Preconditions.checkArgument(param != \"string\", String.format(\"constant %s\", param));"); - } - - @Test - public void testCheckState_stringConcatenate() { - failLogSafe(DIAGNOSTIC, "Preconditions.checkState(param != \"string\", \"constant\" + param);"); - } - - @Test - public void testCheckState_stringFormat() { - failLogSafe(DIAGNOSTIC, - "Preconditions.checkState(param != \"string\", String.format(\"constant %s\", param));"); - } - - @Test - public void testCheckNotNull_stringConcatenate() { - failLogSafe(DIAGNOSTIC, "Preconditions.checkNotNull(param, \"constant\" + param);"); - } - - @Test - public void testCheckNotNull_stringFormat() { - failLogSafe(DIAGNOSTIC, "Preconditions.checkNotNull(param, String.format(\"constant %s\", param));"); - } -} diff --git a/changelog/@unreleased/pr-755.v2.yml b/changelog/@unreleased/pr-755.v2.yml new file mode 100644 index 000000000..265f21c5a --- /dev/null +++ b/changelog/@unreleased/pr-755.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Remove errorprone `LogSafePreconditionsConstantMessage` + links: + - https://github.com/palantir/gradle-baseline/pull/755