diff --git a/cxxheaderparser/parser.py b/cxxheaderparser/parser.py index a4217a2..65be148 100644 --- a/cxxheaderparser/parser.py +++ b/cxxheaderparser/parser.py @@ -1914,11 +1914,12 @@ def _parse_fn_end(self, fn: Function) -> None: fn_template = fn_template[0] fn_template.raw_requires_post = self._parse_requires(rtok) + if self.lex.token_if("ARROW"): + self._parse_trailing_return_type(fn) + if self.lex.token_if("{"): self._discard_contents("{", "}") fn.has_body = True - elif self.lex.token_if("ARROW"): - self._parse_trailing_return_type(fn) def _parse_method_end(self, method: Method) -> None: """ @@ -1963,6 +1964,9 @@ def _parse_method_end(self, method: Method) -> None: method.ref_qualifier = tok_value elif tok_value == "->": self._parse_trailing_return_type(method) + if self.lex.token_if("{"): + self._discard_contents("{", "}") + method.has_body = True break elif tok_value == "throw": tok = self._next_token_must_be("(") diff --git a/tests/test_fn.py b/tests/test_fn.py index 77facfb..1a4fb4e 100644 --- a/tests/test_fn.py +++ b/tests/test_fn.py @@ -1194,3 +1194,67 @@ class C { ] ) ) + + +def test_fn_trailing_return_with_body() -> None: + content = """ + auto test() -> void + { + } + """ + data = parse_string(content, cleandoc=True) + + assert data == ParsedData( + namespace=NamespaceScope( + functions=[ + Function( + return_type=Type( + typename=PQName(segments=[FundamentalSpecifier(name="void")]) + ), + name=PQName(segments=[NameSpecifier(name="test")]), + parameters=[], + has_body=True, + has_trailing_return=True, + ) + ] + ) + ) + + +def test_method_trailing_return_with_body() -> None: + content = """ + struct X { + auto test() -> void + { + } + }; + """ + data = parse_string(content, cleandoc=True) + + assert data == ParsedData( + namespace=NamespaceScope( + classes=[ + ClassScope( + class_decl=ClassDecl( + typename=PQName( + segments=[NameSpecifier(name="X")], classkey="struct" + ) + ), + methods=[ + Method( + return_type=Type( + typename=PQName( + segments=[FundamentalSpecifier(name="void")] + ) + ), + name=PQName(segments=[NameSpecifier(name="test")]), + parameters=[], + has_body=True, + has_trailing_return=True, + access="public", + ) + ], + ) + ] + ) + )