Skip to content

Commit 5e8ff8c

Browse files
autoantwortclaude
andcommitted
Keep requires-expressions after a member access on a cast
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7a05e80 commit 5e8ff8c

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎lib/templatesimplifier.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ static bool isTrailingReturnTypeEnd(const Token *tok)
9999
// introducer, the '(' of an object as in '(*p)->x' follows an operator or a keyword like 'return'.
100100
// A call result as in 'f()->x' is not told apart.
101101
const Token *beforeParen = prev->link()->previous();
102-
return Token::Match(beforeParen, "%name%|>|]") && !Token::Match(beforeParen, "return|throw|co_return|co_yield|co_await");
102+
if (Token::simpleMatch(beforeParen, ">")) {
103+
// the '>' closes template parameters as in '[]<class U>(U u)', or a cast as in 'static_cast<P*>(q)->x'
104+
const Token *opening = beforeParen->findOpeningBracket();
105+
return opening && !Token::Match(opening->previous(), "static_cast|dynamic_cast|reinterpret_cast|const_cast");
106+
}
107+
return Token::Match(beforeParen, "%name%|]") && !Token::Match(beforeParen, "return|throw|co_return|co_yield|co_await");
103108
}
104109

105110
namespace {

‎test/testsimplifytemplate.cpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6769,15 +6769,17 @@ class TestSimplifyTemplate : public TestFixture {
67696769
ASSERT_EQUALS(expected, tok(code));
67706770

67716771
// requires-expression after a member access
6772-
const char code2[] = "template <class T> bool f(P* p, P** a) {\n"
6772+
const char code2[] = "template <class T> bool f(P* p, P** a, void* q) {\n"
67736773
" return p->x && requires (T t) { t.foo(); } &&\n"
67746774
" a[0]->x && requires (T t) { t.foo(); } &&\n"
6775-
" (*a)->x && requires (T t) { t.foo(); };\n"
6775+
" (*a)->x && requires (T t) { t.foo(); } &&\n"
6776+
" static_cast<P*>(q)->x && requires (T t) { t.foo(); };\n"
67766777
"}";
6777-
const char expected2[] = "template < class T > bool f ( P * p , P * * a ) { "
6778+
const char expected2[] = "template < class T > bool f ( P * p , P * * a , void * q ) { "
67786779
"return p . x && requires ( T t ) { t . foo ( ) ; } && "
67796780
"a [ 0 ] . x && requires ( T t ) { t . foo ( ) ; } && "
6780-
"( * a ) . x && requires ( T t ) { t . foo ( ) ; } ; "
6781+
"( * a ) . x && requires ( T t ) { t . foo ( ) ; } && "
6782+
"static_cast < P * > ( q ) . x && requires ( T t ) { t . foo ( ) ; } ; "
67816783
"}";
67826784
ASSERT_EQUALS(expected2, tok(code2));
67836785

0 commit comments

Comments
 (0)