<commit_before>package com.jetbrains.python.inspections;

import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiReference;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.actions.ReplaceFunctionWithSetLiteralQuickFix;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyBuiltinCache;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

/**
 * User: catherine
 *
 * Inspection to find set built-in function and replace it with set literal
 * available if the selected language level supports set literals.
 */
public class PySetFunctionToLiteralInspection extends PyInspection {

  @Nls
  @NotNull
  @Override
  public String getDisplayName() {
    return PyBundle.message("INSP.NAME.set.function.to.literal");
  }

  @NotNull
  @Override
  public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
    return new Visitor(holder);
  }

  private static class Visitor extends PyInspectionVisitor {
    public Visitor(final ProblemsHolder holder) {
      super(holder);
    }

    @Override
    public void visitPyCallExpression(final PyCallExpression node) {
      if (LanguageLevel.forElement(node).supportsSetLiterals()) {
        PyExpression callee = node.getCallee();
        if (node.isCalleeText(PyNames.SET) && isInBuiltins(callee)) {
          PyExpression[] arguments = node.getArguments();
          if (arguments.length == 1) {
            PyExpression argument= arguments[0];
            String string = "";
            PyElement[] elements = {};
            if (argument instanceof PyStringLiteralExpression) {
              string = ((PyStringLiteralExpression)argument).getStringValue();
            }
            if ((argument instanceof PySequenceExpression || (argument instanceof PyParenthesizedExpression &&
                          ((PyParenthesizedExpression)argument).getContainedExpression() instanceof PyTupleExpression))) {

              if (argument instanceof PySequenceExpression)
                elements = ((PySequenceExpression)argument).getElements();
              if (argument instanceof PyParenthesizedExpression) {
                PyExpression tuple = ((PyParenthesizedExpression)argument).getContainedExpression();
                if (tuple instanceof PyTupleExpression)
                  elements = ((PyTupleExpression)(tuple)).getElements();
              }
            }
            if (elements.length != 0 || !string.isEmpty())
                registerProblem(node, "Function call can be replaced with set literal",
                                                 new ReplaceFunctionWithSetLiteralQuickFix(elements, string));
          }
        }
      }
    }

    private static boolean isInBuiltins(PyExpression callee) {
      PsiReference reference = callee.getReference();
      if (reference != null) {
        PsiElement resolved = reference.resolve();
        if (resolved != null && PyBuiltinCache.getInstance(callee).hasInBuiltins(resolved)) {
          return true;
        }
      }
      return false;
    }
  }
}<commit_msg>qualified references are not going to resolve to built-in set<commit_after>package com.jetbrains.python.inspections;

import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiReference;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.actions.ReplaceFunctionWithSetLiteralQuickFix;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyBuiltinCache;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

/**
 * User: catherine
 *
 * Inspection to find set built-in function and replace it with set literal
 * available if the selected language level supports set literals.
 */
public class PySetFunctionToLiteralInspection extends PyInspection {

  @Nls
  @NotNull
  @Override
  public String getDisplayName() {
    return PyBundle.message("INSP.NAME.set.function.to.literal");
  }

  @NotNull
  @Override
  public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
    return new Visitor(holder);
  }

  private static class Visitor extends PyInspectionVisitor {
    public Visitor(final ProblemsHolder holder) {
      super(holder);
    }

    @Override
    public void visitPyCallExpression(final PyCallExpression node) {
      if (LanguageLevel.forElement(node).supportsSetLiterals()) {
        PyExpression callee = node.getCallee();
        if (node.isCalleeText(PyNames.SET) && isInBuiltins(callee)) {
          PyExpression[] arguments = node.getArguments();
          if (arguments.length == 1) {
            PyExpression argument= arguments[0];
            String string = "";
            PyElement[] elements = {};
            if (argument instanceof PyStringLiteralExpression) {
              string = ((PyStringLiteralExpression)argument).getStringValue();
            }
            if ((argument instanceof PySequenceExpression || (argument instanceof PyParenthesizedExpression &&
                          ((PyParenthesizedExpression)argument).getContainedExpression() instanceof PyTupleExpression))) {

              if (argument instanceof PySequenceExpression)
                elements = ((PySequenceExpression)argument).getElements();
              if (argument instanceof PyParenthesizedExpression) {
                PyExpression tuple = ((PyParenthesizedExpression)argument).getContainedExpression();
                if (tuple instanceof PyTupleExpression)
                  elements = ((PyTupleExpression)(tuple)).getElements();
              }
            }
            if (elements.length != 0 || !string.isEmpty())
                registerProblem(node, "Function call can be replaced with set literal",
                                                 new ReplaceFunctionWithSetLiteralQuickFix(elements, string));
          }
        }
      }
    }

    private static boolean isInBuiltins(PyExpression callee) {
      if (callee instanceof PyQualifiedExpression && (((PyQualifiedExpression)callee).getQualifier() != null)) {
        return false;
      }
      PsiReference reference = callee.getReference();
      if (reference != null) {
        PsiElement resolved = reference.resolve();
        if (resolved != null && PyBuiltinCache.getInstance(callee).hasInBuiltins(resolved)) {
          return true;
        }
      }
      return false;
    }
  }
}
{'fraction_non_alphanumeric': 0.09131939220701069, 'fraction_numerical': 0.0009026628554234993, 'mean_word_length': 2.961859356376639, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4157468', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1635, 'n_words': 383}