Updates the global list of line error-suppressions. Parses any NOLINT comments on the current line, updating the global error_suppressions store. Reports an error if the NOLINT comment was malformed. Args: filename: str, the name of the input file. raw_line: str, the l
(filename, raw_line, linenum, error)
| 1092 | |
| 1093 | |
| 1094 | def ParseNolintSuppressions(filename, raw_line, linenum, error): |
| 1095 | """Updates the global list of line error-suppressions. |
| 1096 | |
| 1097 | Parses any NOLINT comments on the current line, updating the global |
| 1098 | error_suppressions store. Reports an error if the NOLINT comment |
| 1099 | was malformed. |
| 1100 | |
| 1101 | Args: |
| 1102 | filename: str, the name of the input file. |
| 1103 | raw_line: str, the line of input text, with comments. |
| 1104 | linenum: int, the number of the current line. |
| 1105 | error: function, an error handler. |
| 1106 | """ |
| 1107 | if matched := re.search(r"\bNOLINT(NEXTLINE|BEGIN|END)?\b(\([^)]+\))?", raw_line): |
| 1108 | no_lint_type = matched.group(1) |
| 1109 | if no_lint_type == "NEXTLINE": |
| 1110 | |
| 1111 | def ProcessCategory(category): |
| 1112 | _error_suppressions.AddLineSuppression(category, linenum + 1) |
| 1113 | elif no_lint_type == "BEGIN": |
| 1114 | if _error_suppressions.HasOpenBlock(): |
| 1115 | error( |
| 1116 | filename, |
| 1117 | linenum, |
| 1118 | "readability/nolint", |
| 1119 | 5, |
| 1120 | ( |
| 1121 | "NONLINT block already defined on line " |
| 1122 | f"{_error_suppressions.GetOpenBlockStart()}" |
| 1123 | ), |
| 1124 | ) |
| 1125 | |
| 1126 | def ProcessCategory(category): |
| 1127 | _error_suppressions.StartBlockSuppression(category, linenum) |
| 1128 | elif no_lint_type == "END": |
| 1129 | if not _error_suppressions.HasOpenBlock(): |
| 1130 | error(filename, linenum, "readability/nolint", 5, "Not in a NOLINT block") |
| 1131 | |
| 1132 | def ProcessCategory(category): |
| 1133 | if category is not None: |
| 1134 | error( |
| 1135 | filename, |
| 1136 | linenum, |
| 1137 | "readability/nolint", |
| 1138 | 5, |
| 1139 | f"NOLINT categories not supported in block END: {category}", |
| 1140 | ) |
| 1141 | _error_suppressions.EndBlockSuppression(linenum) |
| 1142 | else: |
| 1143 | |
| 1144 | def ProcessCategory(category): |
| 1145 | _error_suppressions.AddLineSuppression(category, linenum) |
| 1146 | |
| 1147 | categories = matched.group(2) |
| 1148 | if categories in (None, "(*)"): # => "suppress all" |
| 1149 | ProcessCategory(None) |
| 1150 | elif categories.startswith("(") and categories.endswith(")"): |
| 1151 | for category in {c.strip() for c in categories[1:-1].split(",")}: |
no test coverage detected