no-empty-array-as-parameter

Try in Playground
python-securitySafetyError

0

No tags

No CWE or CVE

We should never pass an empty array parameter to a function. Instead, use None and check the value if defined. This can cause unwanted behavior as the value of the argument is only evaluated once.

Read more

  • Avoid using empty list as default argument

Ast Rule: function definition


no-empty-array-as-parameter

How to write a rule
function visit(node) {
  if (node.parameters && node.parameters.values) {
    const parametersWithEmptyArray = node.parameters.values.filter(p => p && p.defaultValue && p.defaultValue.value === "[]");

    for(var i = 0 ; i < parametersWithEmptyArray.length ; i++) {
      const parameter = parametersWithEmptyArray[i];
      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);
    }
  }
}

fail

Expected test result: has error

print("bli")
def newFunction(arg1, arg2: int, arg3 = []):
  print("bla")
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.