Skip to content

Commit dbdb1ea

Browse files
authored
Put else-return logic into tighter scope (#5643)
1 parent a3678cc commit dbdb1ea

File tree

3 files changed

+164
-162
lines changed

3 files changed

+164
-162
lines changed

stl/inc/chrono

Lines changed: 162 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -5037,69 +5037,14 @@ namespace chrono {
50375037

50385038
template <class _Ty>
50395039
_NODISCARD tm _Fill_tm(const _Ty& _Val) {
5040-
unsigned int _Day = 0;
5041-
unsigned int _Month = 0;
5042-
int _Year = 0;
5043-
int _Yearday = 0;
5044-
int _Weekday = 0;
5045-
int _Hours = 0;
5046-
int _Minutes = 0;
5047-
int _Seconds = 0;
5048-
50495040
if constexpr (_Is_specialization_v<_Ty, duration>) {
50505041
return _Fill_tm(hh_mm_ss{_Val});
50515042
} else if constexpr (_Is_specialization_v<_Ty, _Local_time_format_t>) {
50525043
return _Fill_tm(_Val._Time);
5053-
} else if constexpr (is_same_v<_Ty, day>) {
5054-
_Day = static_cast<unsigned int>(_Val);
5055-
} else if constexpr (is_same_v<_Ty, month>) {
5056-
_Month = static_cast<unsigned int>(_Val);
5057-
} else if constexpr (is_same_v<_Ty, year>) {
5058-
_Year = static_cast<int>(_Val);
5059-
} else if constexpr (is_same_v<_Ty, weekday>) {
5060-
_Weekday = static_cast<int>(_Val.c_encoding());
5061-
} else if constexpr (_Is_any_of_v<_Ty, weekday_indexed, weekday_last>) {
5062-
_Weekday = static_cast<int>(_Val.weekday().c_encoding());
5063-
} else if constexpr (is_same_v<_Ty, month_day>) {
5064-
_Day = static_cast<unsigned int>(_Val.day());
5065-
_Month = static_cast<unsigned int>(_Val.month());
5066-
if (_Val.month() == January) {
5067-
_Yearday = static_cast<int>(_Day) - 1;
5068-
} else if (_Val.month() == February) {
5069-
_Yearday = 31 + static_cast<int>(_Day) - 1;
5070-
}
5071-
} else if constexpr (is_same_v<_Ty, month_day_last>) {
5072-
_Month = static_cast<unsigned int>(_Val.month());
5073-
_Day = static_cast<unsigned int>(_Last_day_table[(_Month - 1) & 0xF]);
5074-
if (_Val.month() == January) {
5075-
_Yearday = 30;
5076-
}
5077-
} else if constexpr (is_same_v<_Ty, month_weekday>) {
5078-
_Month = static_cast<unsigned int>(_Val.month());
5079-
_Weekday = static_cast<int>(_Val.weekday_indexed().weekday().c_encoding());
5080-
} else if constexpr (is_same_v<_Ty, month_weekday_last>) {
5081-
_Month = static_cast<unsigned int>(_Val.month());
5082-
_Weekday = static_cast<int>(_Val.weekday_last().weekday().c_encoding());
5083-
} else if constexpr (is_same_v<_Ty, year_month>) {
5084-
_Month = static_cast<unsigned int>(_Val.month());
5085-
_Year = static_cast<int>(_Val.year());
5086-
} else if constexpr (_Is_any_of_v<_Ty, year_month_day, year_month_day_last>) {
5087-
_Day = static_cast<unsigned int>(_Val.day());
5088-
_Month = static_cast<unsigned int>(_Val.month());
5089-
_Year = static_cast<int>(_Val.year());
5090-
if (_Val.ok()) {
5091-
const year_month_day& _Ymd = _Val;
5092-
_Weekday = _Ymd._Calculate_weekday();
5093-
_Yearday = (static_cast<sys_days>(_Val) - static_cast<sys_days>(_Val.year() / January / 1)).count();
5094-
}
50955044
} else if constexpr (_Is_any_of_v<_Ty, year_month_weekday, year_month_weekday_last>) {
50965045
auto _Tm = _Fill_tm(year_month_day{_Val});
50975046
_Tm.tm_wday = static_cast<int>(_Val.weekday().c_encoding());
50985047
return _Tm;
5099-
} else if constexpr (_Is_specialization_v<_Ty, hh_mm_ss>) {
5100-
_Hours = _Val.hours().count();
5101-
_Minutes = _Val.minutes().count();
5102-
_Seconds = static_cast<int>(_Val.seconds().count());
51035048
} else if constexpr (_Is_any_of_v<_Ty, sys_info, local_info>) {
51045049
return {}; // none of the valid conversion specifiers need tm fields
51055050
} else if constexpr (_Is_specialization_v<_Ty, time_point>) {
@@ -5112,18 +5057,75 @@ namespace chrono {
51125057
_Tm.tm_min = _Hms.tm_min;
51135058
_Tm.tm_hour = _Hms.tm_hour;
51145059
return _Tm;
5115-
}
5060+
} else {
5061+
unsigned int _Day = 0;
5062+
unsigned int _Month = 0;
5063+
int _Year = 0;
5064+
int _Yearday = 0;
5065+
int _Weekday = 0;
5066+
int _Hours = 0;
5067+
int _Minutes = 0;
5068+
int _Seconds = 0;
5069+
5070+
if constexpr (is_same_v<_Ty, day>) {
5071+
_Day = static_cast<unsigned int>(_Val);
5072+
} else if constexpr (is_same_v<_Ty, month>) {
5073+
_Month = static_cast<unsigned int>(_Val);
5074+
} else if constexpr (is_same_v<_Ty, year>) {
5075+
_Year = static_cast<int>(_Val);
5076+
} else if constexpr (is_same_v<_Ty, weekday>) {
5077+
_Weekday = static_cast<int>(_Val.c_encoding());
5078+
} else if constexpr (_Is_any_of_v<_Ty, weekday_indexed, weekday_last>) {
5079+
_Weekday = static_cast<int>(_Val.weekday().c_encoding());
5080+
} else if constexpr (is_same_v<_Ty, month_day>) {
5081+
_Day = static_cast<unsigned int>(_Val.day());
5082+
_Month = static_cast<unsigned int>(_Val.month());
5083+
if (_Val.month() == January) {
5084+
_Yearday = static_cast<int>(_Day) - 1;
5085+
} else if (_Val.month() == February) {
5086+
_Yearday = 31 + static_cast<int>(_Day) - 1;
5087+
}
5088+
} else if constexpr (is_same_v<_Ty, month_day_last>) {
5089+
_Month = static_cast<unsigned int>(_Val.month());
5090+
_Day = static_cast<unsigned int>(_Last_day_table[(_Month - 1) & 0xF]);
5091+
if (_Val.month() == January) {
5092+
_Yearday = 30;
5093+
}
5094+
} else if constexpr (is_same_v<_Ty, month_weekday>) {
5095+
_Month = static_cast<unsigned int>(_Val.month());
5096+
_Weekday = static_cast<int>(_Val.weekday_indexed().weekday().c_encoding());
5097+
} else if constexpr (is_same_v<_Ty, month_weekday_last>) {
5098+
_Month = static_cast<unsigned int>(_Val.month());
5099+
_Weekday = static_cast<int>(_Val.weekday_last().weekday().c_encoding());
5100+
} else if constexpr (is_same_v<_Ty, year_month>) {
5101+
_Month = static_cast<unsigned int>(_Val.month());
5102+
_Year = static_cast<int>(_Val.year());
5103+
} else if constexpr (_Is_any_of_v<_Ty, year_month_day, year_month_day_last>) {
5104+
_Day = static_cast<unsigned int>(_Val.day());
5105+
_Month = static_cast<unsigned int>(_Val.month());
5106+
_Year = static_cast<int>(_Val.year());
5107+
if (_Val.ok()) {
5108+
const year_month_day& _Ymd = _Val;
5109+
_Weekday = _Ymd._Calculate_weekday();
5110+
_Yearday = (static_cast<sys_days>(_Val) - static_cast<sys_days>(_Val.year() / January / 1)).count();
5111+
}
5112+
} else if constexpr (_Is_specialization_v<_Ty, hh_mm_ss>) {
5113+
_Hours = _Val.hours().count();
5114+
_Minutes = _Val.minutes().count();
5115+
_Seconds = static_cast<int>(_Val.seconds().count());
5116+
}
51165117

5117-
tm _Time;
5118-
_Time.tm_sec = _Seconds;
5119-
_Time.tm_min = _Minutes;
5120-
_Time.tm_hour = _Hours;
5121-
_Time.tm_mday = static_cast<int>(_Day);
5122-
_Time.tm_mon = static_cast<int>(_Month) - 1;
5123-
_Time.tm_year = _Year - 1900;
5124-
_Time.tm_yday = _Yearday;
5125-
_Time.tm_wday = _Weekday;
5126-
return _Time;
5118+
tm _Time;
5119+
_Time.tm_sec = _Seconds;
5120+
_Time.tm_min = _Minutes;
5121+
_Time.tm_hour = _Hours;
5122+
_Time.tm_mday = static_cast<int>(_Day);
5123+
_Time.tm_mon = static_cast<int>(_Month) - 1;
5124+
_Time.tm_year = _Year - 1900;
5125+
_Time.tm_yday = _Yearday;
5126+
_Time.tm_wday = _Weekday;
5127+
return _Time;
5128+
}
51275129
}
51285130

51295131
_EXPORT_STD template <class _CharT, class _Traits>
@@ -5731,110 +5733,111 @@ namespace chrono {
57315733
|| _Is_specialization_v<_Ty, time_point> || _Is_specialization_v<_Ty, _Local_time_format_t>
57325734
|| _Is_specialization_v<_Ty, hh_mm_ss>) {
57335735
return;
5734-
}
5736+
} else {
5737+
const auto _Validate = [&] {
5738+
constexpr bool _Is_ymd = _Is_any_of_v<_Ty, year_month_day, year_month_day_last, year_month_weekday,
5739+
year_month_weekday_last>;
5740+
5741+
switch (_Spec._Type) {
5742+
case 'a':
5743+
case 'A':
5744+
case 'u':
5745+
case 'w':
5746+
if constexpr (_Is_any_of_v<_Ty, weekday, weekday_last>) {
5747+
return _Val.ok();
5748+
} else if constexpr (_Is_any_of_v<_Ty, weekday_indexed, year_month_weekday,
5749+
year_month_weekday_last>) {
5750+
return _Val.weekday().ok();
5751+
} else if constexpr (is_same_v<_Ty, month_weekday>) {
5752+
return _Val.weekday_indexed().weekday().ok();
5753+
} else if constexpr (is_same_v<_Ty, month_weekday_last>) {
5754+
return _Val.weekday_last().ok();
5755+
} else if constexpr (_Is_any_of_v<_Ty, year_month_day, year_month_day_last>) {
5756+
return _Val.ok();
5757+
}
5758+
break;
57355759

5736-
const auto _Validate = [&] {
5737-
constexpr bool _Is_ymd =
5738-
_Is_any_of_v<_Ty, year_month_day, year_month_day_last, year_month_weekday, year_month_weekday_last>;
5739-
5740-
switch (_Spec._Type) {
5741-
case 'a':
5742-
case 'A':
5743-
case 'u':
5744-
case 'w':
5745-
if constexpr (_Is_any_of_v<_Ty, weekday, weekday_last>) {
5746-
return _Val.ok();
5747-
} else if constexpr (_Is_any_of_v<_Ty, weekday_indexed, year_month_weekday,
5748-
year_month_weekday_last>) {
5749-
return _Val.weekday().ok();
5750-
} else if constexpr (is_same_v<_Ty, month_weekday>) {
5751-
return _Val.weekday_indexed().weekday().ok();
5752-
} else if constexpr (is_same_v<_Ty, month_weekday_last>) {
5753-
return _Val.weekday_last().ok();
5754-
} else if constexpr (_Is_any_of_v<_Ty, year_month_day, year_month_day_last>) {
5755-
return _Val.ok();
5756-
}
5757-
break;
5760+
case 'b':
5761+
case 'B':
5762+
case 'h':
5763+
case 'm':
5764+
if constexpr (is_same_v<_Ty, month>) {
5765+
return _Val.ok();
5766+
} else if constexpr (_Is_any_of_v<_Ty, month_day, month_day_last, month_weekday,
5767+
month_weekday_last, year_month>
5768+
|| _Is_ymd) {
5769+
return _Val.month().ok();
5770+
}
5771+
break;
57585772

5759-
case 'b':
5760-
case 'B':
5761-
case 'h':
5762-
case 'm':
5763-
if constexpr (is_same_v<_Ty, month>) {
5764-
return _Val.ok();
5765-
} else if constexpr (_Is_any_of_v<_Ty, month_day, month_day_last, month_weekday, month_weekday_last,
5766-
year_month>
5767-
|| _Is_ymd) {
5768-
return _Val.month().ok();
5769-
}
5770-
break;
5773+
case 'C':
5774+
case 'y':
5775+
case 'Y':
5776+
if constexpr (is_same_v<_Ty, year>) {
5777+
return _Val.ok();
5778+
} else if constexpr (is_same_v<_Ty, year_month> || _Is_ymd) {
5779+
return _Val.year().ok();
5780+
}
5781+
break;
57715782

5772-
case 'C':
5773-
case 'y':
5774-
case 'Y':
5775-
if constexpr (is_same_v<_Ty, year>) {
5776-
return _Val.ok();
5777-
} else if constexpr (is_same_v<_Ty, year_month> || _Is_ymd) {
5778-
return _Val.year().ok();
5779-
}
5780-
break;
5783+
case 'd':
5784+
case 'e':
5785+
if constexpr (_Is_any_of_v<_Ty, day, month_day_last>) {
5786+
return _Val.ok();
5787+
} else if constexpr (is_same_v<_Ty, month_day>) {
5788+
return _Val.day().ok();
5789+
} else if constexpr (_Is_ymd) {
5790+
const year_month_day& _Ymd{_Val};
5791+
return _Ymd.day().ok();
5792+
}
5793+
break;
57815794

5782-
case 'd':
5783-
case 'e':
5784-
if constexpr (_Is_any_of_v<_Ty, day, month_day_last>) {
5785-
return _Val.ok();
5786-
} else if constexpr (is_same_v<_Ty, month_day>) {
5787-
return _Val.day().ok();
5788-
} else if constexpr (_Is_ymd) {
5789-
const year_month_day& _Ymd{_Val};
5790-
return _Ymd.day().ok();
5791-
}
5792-
break;
5795+
case 'D':
5796+
case 'F':
5797+
if constexpr (_Has_ok<_Ty>) {
5798+
return _Val.ok();
5799+
}
5800+
break;
57935801

5794-
case 'D':
5795-
case 'F':
5796-
if constexpr (_Has_ok<_Ty>) {
5797-
return _Val.ok();
5798-
}
5799-
break;
5802+
case 'j':
5803+
if constexpr (is_same_v<_Ty, month_day>) {
5804+
if (_Val.month() > February) {
5805+
_Throw_format_error("The day of year for a month_day past February is ambiguous.");
5806+
}
5807+
return true;
5808+
} else if constexpr (is_same_v<_Ty, month_day_last>) {
5809+
if (_Val.month() >= February) {
5810+
_Throw_format_error(
5811+
"The day of year for a month_day_last other than January is ambiguous");
5812+
}
5813+
return true;
5814+
} else if constexpr (_Is_ymd) {
5815+
return _Val.ok();
5816+
}
5817+
break;
58005818

5801-
case 'j':
5802-
if constexpr (is_same_v<_Ty, month_day>) {
5803-
if (_Val.month() > February) {
5804-
_Throw_format_error("The day of year for a month_day past February is ambiguous.");
5819+
case 'g':
5820+
case 'G':
5821+
case 'U':
5822+
case 'V':
5823+
case 'W':
5824+
if constexpr (_Is_ymd) {
5825+
return _Val.ok();
58055826
}
5806-
return true;
5807-
} else if constexpr (is_same_v<_Ty, month_day_last>) {
5808-
if (_Val.month() >= February) {
5809-
_Throw_format_error("The day of year for a month_day_last other than January is ambiguous");
5827+
break;
5828+
5829+
default:
5830+
if constexpr (_Has_ok<_Ty>) {
5831+
return _Val.ok();
58105832
}
58115833
return true;
5812-
} else if constexpr (_Is_ymd) {
5813-
return _Val.ok();
58145834
}
5815-
break;
5816-
5817-
case 'g':
5818-
case 'G':
5819-
case 'U':
5820-
case 'V':
5821-
case 'W':
5822-
if constexpr (_Is_ymd) {
5823-
return _Val.ok();
5824-
}
5825-
break;
5826-
5827-
default:
5828-
if constexpr (_Has_ok<_Ty>) {
5829-
return _Val.ok();
5830-
}
5831-
return true;
5835+
_STL_INTERNAL_CHECK(false);
5836+
return false;
5837+
};
5838+
if (!_Validate()) {
5839+
_Throw_format_error("Cannot localize out-of-bounds time point.");
58325840
}
5833-
_STL_INTERNAL_CHECK(false);
5834-
return false;
5835-
};
5836-
if (!_Validate()) {
5837-
_Throw_format_error("Cannot localize out-of-bounds time point.");
58385841
}
58395842
}
58405843

stl/inc/memory

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3938,7 +3938,7 @@ _CRTIMP2_PURE void __cdecl _Unlock_shared_ptr_spin_lock() noexcept;
39383938
} // extern "C"
39393939

39403940
struct _Shared_ptr_spin_lock { // class to manage a spin lock for shared_ptr atomic operations
3941-
_Shared_ptr_spin_lock() { // lock the spin lock
3941+
_Shared_ptr_spin_lock() noexcept { // lock the spin lock
39423942
_Lock_shared_ptr_spin_lock();
39433943
}
39443944

stl/inc/xhash

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,9 +2048,8 @@ _NODISCARD bool _Hash_equal(const _Hash<_Traits>& _Left, const _Hash<_Traits>& _
20482048
return false;
20492049
}
20502050
}
2051+
return true;
20512052
}
2052-
2053-
return true;
20542053
}
20552054
_STD_END
20562055

0 commit comments

Comments
 (0)