diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 98345dd31f9..6bc6d56a929 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -10025,12 +10025,19 @@ void Tokenizer::simplifyOperatorName() } } + const bool returnsRef = Token::simpleMatch(par, "( & (") && tok->next()->isName(); if (par && !op.empty()) { - tok->str("operator" + op); - Token::eraseTokens(tok, par); + if (returnsRef) { + par->next()->insertToken("operator" + op)->isOperatorKeyword(true); + tok->deleteThis(); + } + else { + tok->str("operator" + op); + Token::eraseTokens(tok, par); + } } - if (!op.empty()) + if (!op.empty() && !returnsRef) tok->isOperatorKeyword(true); } diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index a63538f346b..72ef30579fc 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -2909,7 +2909,7 @@ class TestSimplifyTypedef : public TestFixture { " constexpr operator foo &() const noexcept { return _a; }\n" "};"; - const char actual[] = "class c { char _a [ 4 ] ; constexpr operatorchar ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; + const char actual[] = "class c { char _a [ 4 ] ; constexpr char ( & operatorchar ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; const char exp[] = "class c { char _a [ 4 ] ; const operator char ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; TODO_ASSERT_EQUALS(exp, actual, tok(code, false)); } @@ -2921,7 +2921,7 @@ class TestSimplifyTypedef : public TestFixture { " constexpr operator const foo &() const noexcept { return _a; }\n" "};"; - const char actual[] = "class c { char _a [ 4 ] ; constexpr operatorconstchar ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; + const char actual[] = "class c { char _a [ 4 ] ; constexpr const char ( & operatorconstchar ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; const char exp[] = "class c { char _a [ 4 ] ; const operator const char ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; TODO_ASSERT_EQUALS(exp, actual, tok(code, false)); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 6fa10173b07..ee3dd93f48d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5101,7 +5101,7 @@ class TestTokenizer : public TestFixture { " operator A& () { return x_; }\n" " A x_;\n" "};"; - ASSERT_EQUALS("template < typename T >\nstruct B {\n\noperatorT ( & ( ) ) [ 3 ] { return x_ ; }\nT x_ [ 3 ] ;\n} ;", tokenizeAndStringify(code)); + ASSERT_EQUALS("template < typename T >\nstruct B {\n\nT ( & operatorT ( ) ) [ 3 ] { return x_ ; }\nT x_ [ 3 ] ;\n} ;", tokenizeAndStringify(code)); ASSERT_EQUALS("", errout.str()); }