function-definition

Try in Playground
python-templatesUnknownInformational

0

No tags

No CWE or CVE

Ast Rule: function definition


function-definition

How to write a rule
function visit(node, filename, code) {
  
  // If filename starts or ends with test_ or _test, do not do anything
  if(filename.includes("_test.py") || filename.startsWith("test_")) {
    return;
  }
  
  // if the parameters of the function are defined
  if (node.parameters && node.parameters.values) {
    
    // Get the list of parameters with the default value []
    const parametersWithEmptyArray = node.parameters.values.filter(p => p && p.defaultValue && p.defaultValue.value === "[]");

    
    parametersWithEmptyArray.forEach(parameter => {
      const error = buildError(parameter.defaultValue.start.line, parameter.defaultValue.start.col, parameter.defaultValue.end.line, parameter.defaultValue.end.col, "cannot use default initializer [] in function", "CRITICAL", "SAFETY");
      addError(error);
    });
  }
}

empty-parameter

Expected test result: no error

def foo(bar=[]):
  pass
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Explore
  • Cookbooks
  • Playground
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.