<commit_before>/*
 * Copyright 2018 The Error Prone Authors.
 *
 * 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.google.errorprone.bugpatterns;

import com.google.errorprone.CompilationTestHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link TreeToString}. */
@RunWith(JUnit4.class)
public class TreeToStringTest {
  private final CompilationTestHelper testHelper =
      CompilationTestHelper.newInstance(TreeToString.class, getClass());

  @Test
  public void noMatch() {
    testHelper
        .addSourceLines(
            "ExampleChecker.java",
            "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.bugpatterns.BugChecker.ClassTreeMatcher;",
            "import com.google.errorprone.matchers.Description;",
            "import com.sun.source.tree.ClassTree;",
            "import com.sun.tools.javac.code.Types;",
            "@BugPattern(name = \"Example\", summary = \"\", severity = SeverityLevel.ERROR)",
            "public class ExampleChecker extends BugChecker implements ClassTreeMatcher {",
            "  @Override public Description matchClass(ClassTree t, VisitorState s) {",
            "    return Description.NO_MATCH;",
            "  }",
            "}")
        .addModules("jdk.compiler/com.sun.tools.javac.code")
        .doTest();
  }

  @Test
  public void matchInABugChecker() {
    testHelper
        .addSourceLines(
            "ExampleChecker.java",
            "import static com.google.errorprone.util.ASTHelpers.getSymbol;",
            "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.bugpatterns.BugChecker.ClassTreeMatcher;",
            "import com.google.errorprone.fixes.SuggestedFix;",
            "import com.google.errorprone.matchers.Description;",
            "import com.sun.source.tree.ClassTree;",
            "import com.sun.tools.javac.code.Symbol;",
            "import com.sun.tools.javac.code.Symbol.ClassSymbol;",
            "import com.sun.tools.javac.code.Type;",
            "import com.sun.tools.javac.code.Types;",
            "@BugPattern(name = \"Example\", summary = \"\", severity = SeverityLevel.ERROR)",
            "public class ExampleChecker extends BugChecker implements ClassTreeMatcher {",
            "  @Override public Description matchClass(ClassTree tree, VisitorState state) {",
            "    // BUG: Diagnostic contains: state.getSourceForNode(tree).contains",
            "    if (tree.toString().contains(\"match\")) {",
            "      return describeMatch(tree);",
            "    }",
            "    return Description.NO_MATCH;",
            "  }",
            "}")
        .addModules("jdk.compiler/com.sun.tools.javac.code")
        .doTest();
  }
}
<commit_msg>Add an example for TreeToString which gets flagged correctly but there is no suggested fix as of now.<commit_after>/*
 * Copyright 2018 The Error Prone Authors.
 *
 * 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.google.errorprone.bugpatterns;

import com.google.errorprone.CompilationTestHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link TreeToString}. */
@RunWith(JUnit4.class)
public class TreeToStringTest {
  private final CompilationTestHelper testHelper =
      CompilationTestHelper.newInstance(TreeToString.class, getClass());

  @Test
  public void noMatch() {
    testHelper
        .addSourceLines(
            "ExampleChecker.java",
            "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.bugpatterns.BugChecker.ClassTreeMatcher;",
            "import com.google.errorprone.matchers.Description;",
            "import com.sun.source.tree.ClassTree;",
            "import com.sun.tools.javac.code.Types;",
            "@BugPattern(name = \"Example\", summary = \"\", severity = SeverityLevel.ERROR)",
            "public class ExampleChecker extends BugChecker implements ClassTreeMatcher {",
            "  @Override public Description matchClass(ClassTree t, VisitorState s) {",
            "    return Description.NO_MATCH;",
            "  }",
            "}")
        .addModules("jdk.compiler/com.sun.tools.javac.code")
        .doTest();
  }

  @Test
  public void matchInABugChecker() {
    testHelper
        .addSourceLines(
            "ExampleChecker.java",
            "import static com.google.errorprone.util.ASTHelpers.getSymbol;",
            "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.bugpatterns.BugChecker.ClassTreeMatcher;",
            "import com.google.errorprone.fixes.SuggestedFix;",
            "import com.google.errorprone.matchers.Description;",
            "import com.google.errorprone.matchers.Matcher;",
            "import com.sun.source.tree.ClassTree;",
            "import com.sun.tools.javac.code.Symbol;",
            "import com.sun.tools.javac.code.Symbol.ClassSymbol;",
            "import com.sun.tools.javac.code.Type;",
            "import com.sun.tools.javac.code.Types;",
            "@BugPattern(name = \"Example\", summary = \"\", severity = SeverityLevel.ERROR)",
            "public class ExampleChecker extends BugChecker implements ClassTreeMatcher {",
            "  private static Matcher<ClassTree> matches(String name) {",
            "    // BUG: Diagnostic contains: TreeToString",
            "    return (Matcher<ClassTree>) (c, state) -> c.toString().equals(name);",
            "  }",
            "  @Override public Description matchClass(ClassTree tree, VisitorState state) {",
            "    // BUG: Diagnostic contains: state.getSourceForNode(tree).contains",
            "    if (tree.toString().contains(\"match\")) {",
            "      return describeMatch(tree);",
            "    }",
            "    return Description.NO_MATCH;",
            "  }",
            "}")
        .addModules("jdk.compiler/com.sun.tools.javac.code")
        .doTest();
  }
}

{'fraction_non_alphanumeric': 0.11817950025497195, 'fraction_numerical': 0.0025497195308516064, 'mean_word_length': 2.6052389705882355, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4557534', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2175, 'n_words': 475}