open-add-flag

Try in Playground
python-best-practicesBest PracticeWarning

0

No tags

No CWE or CVE

If a flag is opened for read-only, no need to put a flag.

Pattern Rule: open("${file}", "r")


open-add-flag

How to write a rule
function visit(pattern) {
  const error = buildError(pattern.start.line, pattern.start.col, pattern.end.line, pattern.end.col, "file with read-only defined", "WARN", "bestpractices");
  const filename = pattern.variables.get("file").value;
  const edit = buildEdit(pattern.start.line, pattern.start.col, pattern.end.line, pattern.end.col, "replace", `open(\"${filename}\")`);
  const fix = buildFix("remove the read-only flag", [edit]);

  addError(error.addFix(fix));
}

open-without-flag.py

Expected test result: no error

def print_foo():
  with open("myfile.txt") as myfile:
    content = myfile.read()

open-with-flag.py

Expected test result: has error

def print_foo():
  with open("myfile.txt", "r") as myfile:
    content = myfile.read()
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.