Checks for a C-style cast by looking for the pattern. Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. cast_type: The string for the C++ cast to recommend. This is either reint
(filename, clean_lines, linenum, cast_type, pattern, error)
| 5810 | |
| 5811 | |
| 5812 | def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error): |
| 5813 | """Checks for a C-style cast by looking for the pattern. |
| 5814 | |
| 5815 | Args: |
| 5816 | filename: The name of the current file. |
| 5817 | clean_lines: A CleansedLines instance containing the file. |
| 5818 | linenum: The number of the line to check. |
| 5819 | cast_type: The string for the C++ cast to recommend. This is either |
| 5820 | reinterpret_cast, static_cast, or const_cast, depending. |
| 5821 | pattern: The regular expression used to find C-style casts. |
| 5822 | error: The function to call with any errors found. |
| 5823 | |
| 5824 | Returns: |
| 5825 | True if an error was emitted. |
| 5826 | False otherwise. |
| 5827 | """ |
| 5828 | line = clean_lines.elided[linenum] |
| 5829 | match = Search(pattern, line) |
| 5830 | if not match: |
| 5831 | return False |
| 5832 | |
| 5833 | # Exclude lines with keywords that tend to look like casts |
| 5834 | context = line[0:match.start(1) - 1] |
| 5835 | if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context): |
| 5836 | return False |
| 5837 | |
| 5838 | # Try expanding current context to see if we one level of |
| 5839 | # parentheses inside a macro. |
| 5840 | if linenum > 0: |
| 5841 | for i in xrange(linenum - 1, max(0, linenum - 5), -1): |
| 5842 | context = clean_lines.elided[i] + context |
| 5843 | if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context): |
| 5844 | return False |
| 5845 | |
| 5846 | # operator++(int) and operator--(int) |
| 5847 | if context.endswith(' operator++') or context.endswith(' operator--'): |
| 5848 | return False |
| 5849 | |
| 5850 | # A single unnamed argument for a function tends to look like old style cast. |
| 5851 | # If we see those, don't issue warnings for deprecated casts. |
| 5852 | remainder = line[match.end(0):] |
| 5853 | if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)', |
| 5854 | remainder): |
| 5855 | return False |
| 5856 | |
| 5857 | # At this point, all that should be left is actual casts. |
| 5858 | error(filename, linenum, 'readability/casting', 4, |
| 5859 | 'Using C-style cast. Use %s<%s>(...) instead' % |
| 5860 | (cast_type, match.group(1))) |
| 5861 | |
| 5862 | return True |
| 5863 | |
| 5864 | |
| 5865 | def ExpectingFunctionArgs(clean_lines, linenum): |