Skip to content

Commit d946b6b

Browse files
authored
Merge pull request #12793 from ethereum/hasDefaultHelper
Add helper to see if a switch has a default case.
2 parents 2858f70 + 2859383 commit d946b6b

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

libsolidity/analysis/ControlFlowBuilder.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,7 @@ void ControlFlowBuilder::operator()(yul::Switch const& _switch)
457457
}
458458
mergeFlow(nodes);
459459

460-
bool hasDefault = util::contains_if(_switch.cases, [](yul::Case const& _case) { return !_case.value; });
461-
if (!hasDefault)
460+
if (!hasDefaultCase(_switch))
462461
connect(beforeSwitch, m_currentNode);
463462
}
464463

libyul/AST.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,13 @@ template <class... Args> inline std::shared_ptr<DebugData const> debugDataOf(std
144144
return std::visit([](auto const& _arg) { return debugDataOf(_arg); }, _node);
145145
}
146146

147+
inline bool hasDefaultCase(Switch const& _switch)
148+
{
149+
return std::any_of(
150+
_switch.cases.begin(),
151+
_switch.cases.end(),
152+
[](Case const& _case) { return !_case.value; }
153+
);
154+
}
155+
147156
}

libyul/optimiser/ControlFlowSimplifier.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,7 @@ void removeEmptyDefaultFromSwitch(Switch& _switchStmt)
6060

6161
void removeEmptyCasesFromSwitch(Switch& _switchStmt)
6262
{
63-
bool hasDefault = std::any_of(
64-
_switchStmt.cases.begin(),
65-
_switchStmt.cases.end(),
66-
[](Case const& _case) { return !_case.value; }
67-
);
68-
69-
if (hasDefault)
63+
if (hasDefaultCase(_switchStmt))
7064
return;
7165

7266
ranges::actions::remove_if(

0 commit comments

Comments
 (0)