while_used.py 859 B

1234567891011121314151617181920212223242526272829303132
  1. """Check for use of while loops."""
  2. from astroid import nodes
  3. from pylint.checkers import BaseChecker
  4. from pylint.checkers.utils import check_messages
  5. from pylint.interfaces import IAstroidChecker
  6. class WhileChecker(BaseChecker):
  7. __implements__ = (IAstroidChecker,)
  8. name = "while_used"
  9. msgs = {
  10. "W0149": (
  11. "Used `while` loop",
  12. "while-used",
  13. "Unbounded `while` loops can often be rewritten as bounded `for` loops.",
  14. )
  15. }
  16. @check_messages("while-used")
  17. def visit_while(self, node: nodes.While) -> None:
  18. self.add_message("while-used", node=node)
  19. def register(linter):
  20. """Required method to auto register this checker.
  21. :param linter: Main interface object for Pylint plugins
  22. :type linter: Pylint object
  23. """
  24. linter.register_checker(WhileChecker(linter))