while_used.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. """Check for use of while loops."""
  5. from __future__ import annotations
  6. from typing import TYPE_CHECKING
  7. from astroid import nodes
  8. from pylint.checkers import BaseChecker
  9. from pylint.checkers.utils import only_required_for_messages
  10. if TYPE_CHECKING:
  11. from pylint.lint import PyLinter
  12. class WhileChecker(BaseChecker):
  13. name = "while_used"
  14. msgs = {
  15. "W0149": (
  16. "Used `while` loop",
  17. "while-used",
  18. "Unbounded `while` loops can often be rewritten as bounded `for` loops. "
  19. "Exceptions can be made for cases such as event loops, listeners, etc.",
  20. )
  21. }
  22. @only_required_for_messages("while-used")
  23. def visit_while(self, node: nodes.While) -> None:
  24. self.add_message("while-used", node=node)
  25. def register(linter: PyLinter) -> None:
  26. linter.register_checker(WhileChecker(linter))