diff --git a/coverage/parser.py b/coverage/parser.py index 8a64f6247..7842b36c9 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -115,7 +115,7 @@ def lines_matching(self, *regexes: str) -> set[TLineNo]: matches = set() for i, ltext in enumerate(self.lines, start=1): if regex_c.search(ltext): - matches.add(i) + matches.add(self._multiline.get(i, i)) return matches def _raw_parse(self) -> None: diff --git a/tests/test_parser.py b/tests/test_parser.py index 69de60411..eaaf4ae0a 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -425,6 +425,24 @@ def f(): ) assert parser.statements == {1,7} + def test_multiline_if_no_branch(self) -> None: + # From https://github.com/nedbat/coveragepy/issues/754 + parser = self.parse_text("""\ + if (this_is_a_verylong_boolean_expression == True # pragma: no branch + and another_long_expression and here_another_expression): + do_something() + """, + ) + parser2 = self.parse_text("""\ + if this_is_a_verylong_boolean_expression == True and another_long_expression \\ + and here_another_expression: # pragma: no branch + do_something() + """, + ) + assert parser.statements == parser2.statements == {1, 3} + pragma_re = ".*pragma: no branch.*" + assert parser.lines_matching(pragma_re) == parser2.lines_matching(pragma_re) + def test_excluding_function(self) -> None: parser = self.parse_text("""\ def fn(foo): # nocover