no-if-true

Try in Playground
python-best-practicesCode StyleInformational

0

No tags

No CWE or CVE

Do not use variable == True, just use variable.

Ast Rule: if condition


no-if-true

How to write a rule
function visit(node) {
  if(node.condition && node.condition.rightSide && node.condition.rightSide.expression && node.condition.rightSide.expression.atom && node.condition.rightSide.expression.atom.str === "True"){
    const error = buildError(node.condition.start.line, node.condition.start.col, node.condition.end.line, node.condition.end.col, "do not make equal with true", "INFO", "BEST_PRACTICE");

    const editReplaceCondition = buildEditUpdate(node.condition.start.line, node.condition.start.col, 
                                                 node.condition.rightSide.expression.atom.end.line, node.condition.rightSide.expression.atom.end.col, node.condition.leftSide.getText())


    const fix = buildFix("remove True", [editReplaceCondition]);
    addError(error.addFix(fix));
  }
}

if-without-true.py

Expected test result: no error

if foo:
  print("bar")

if-true.py

Expected test result: has error

if foo == True:
  print("bar")
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.