Report if too many lines in function body. Args: error: The function to call with any errors found. filename: The name of the current file. linenum: The number of the line to check.
(self, error, filename, linenum)
| 1658 | self.lines_in_function += 1 |
| 1659 | |
| 1660 | def Check(self, error, filename, linenum): |
| 1661 | """Report if too many lines in function body. |
| 1662 | |
| 1663 | Args: |
| 1664 | error: The function to call with any errors found. |
| 1665 | filename: The name of the current file. |
| 1666 | linenum: The number of the line to check. |
| 1667 | """ |
| 1668 | if not self.in_a_function: |
| 1669 | return |
| 1670 | |
| 1671 | if re.match(r"T(EST|est)", self.current_function): |
| 1672 | base_trigger = self._TEST_TRIGGER |
| 1673 | else: |
| 1674 | base_trigger = self._NORMAL_TRIGGER |
| 1675 | trigger = base_trigger * 2 ** _VerboseLevel() |
| 1676 | |
| 1677 | if self.lines_in_function > trigger: |
| 1678 | error_level = int(math.log2(self.lines_in_function / base_trigger)) |
| 1679 | # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... |
| 1680 | error_level = min(error_level, 5) |
| 1681 | error( |
| 1682 | filename, |
| 1683 | linenum, |
| 1684 | "readability/fn_size", |
| 1685 | error_level, |
| 1686 | "Small and focused functions are preferred:" |
| 1687 | f" {self.current_function} has {self.lines_in_function} non-comment lines" |
| 1688 | f" (error triggered by exceeding {trigger} lines).", |
| 1689 | ) |
| 1690 | |
| 1691 | def End(self): |
| 1692 | """Stop analyzing function body.""" |
no test coverage detected