2022-03-01 15:04:49 +00:00
|
|
|
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
|
|
|
|
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
|
|
|
|
|
2021-08-18 15:28:49 +00:00
|
|
|
"""Check for use of while loops."""
|
2021-12-27 20:30:34 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2021-09-03 11:47:23 +00:00
|
|
|
from astroid import nodes
|
|
|
|
|
2021-08-18 15:28:49 +00:00
|
|
|
from pylint.checkers import BaseChecker
|
|
|
|
from pylint.checkers.utils import check_messages
|
|
|
|
from pylint.interfaces import IAstroidChecker
|
|
|
|
|
2021-12-27 20:30:34 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from pylint.lint import PyLinter
|
|
|
|
|
2021-08-18 15:28:49 +00:00
|
|
|
|
|
|
|
class WhileChecker(BaseChecker):
|
|
|
|
|
|
|
|
__implements__ = (IAstroidChecker,)
|
|
|
|
name = "while_used"
|
|
|
|
msgs = {
|
|
|
|
"W0149": (
|
|
|
|
"Used `while` loop",
|
|
|
|
"while-used",
|
|
|
|
"Unbounded `while` loops can often be rewritten as bounded `for` loops.",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@check_messages("while-used")
|
2021-09-03 11:47:23 +00:00
|
|
|
def visit_while(self, node: nodes.While) -> None:
|
2021-08-18 15:28:49 +00:00
|
|
|
self.add_message("while-used", node=node)
|
|
|
|
|
|
|
|
|
2021-12-27 20:30:34 +00:00
|
|
|
def register(linter: "PyLinter") -> None:
|
2021-08-18 15:28:49 +00:00
|
|
|
linter.register_checker(WhileChecker(linter))
|