[Analyzers][CPP] Turn on rule 26497 (#23119)

* Turn on warning 26497
This function function-name could be marked constexpr if compile-time evaluation is desired.

* C++20 has constexpr swap

* as constexpr is not only for compile time,
make all functions that can be constexpr constexpr

* constexpr functions are implicity inline
This commit is contained in:
sosssego 2023-02-08 10:59:34 +00:00 committed by GitHub
parent 53f0b00328
commit 956eb98125
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 48 additions and 42 deletions

View File

@ -84,7 +84,7 @@
<Rule Id="C26494" Action="Hidden" />
<Rule Id="C26495" Action="Error" />
<Rule Id="C26496" Action="Hidden" />
<Rule Id="C26497" Action="Hidden" />
<Rule Id="C26497" Action="Error" />
<Rule Id="C26498" Action="Error" />
<Rule Id="C26800" Action="Hidden" />
<Rule Id="C26810" Action="Error" />

View File

@ -206,7 +206,7 @@ void notifications::show_toast(std::wstring message, std::wstring title, toast_p
show_toast_with_activations(std::move(message), std::move(title), {}, {}, std::move(params));
}
inline void xml_escape(std::wstring data)
constexpr inline void xml_escape(std::wstring data)
{
std::wstring buffer;
buffer.reserve(data.size());

View File

@ -4,8 +4,9 @@
#include <Windows.h>
// disable warning 26471 - Don't use reinterpret_cast. A cast from void* can use static_cast
// Disable 26497 for winrt - This function function-name could be marked constexpr if compile-time evaluation is desired.
#pragma warning(push)
#pragma warning(disable: 26471)
#pragma warning(disable : 26471 26497)
#include <wil/resource.h>
#pragma warning(pop)

View File

@ -115,12 +115,12 @@ namespace
return true;
}
bool isWin(int key)
constexpr bool isWin(int key)
{
return key == VK_LWIN || key == VK_RWIN;
}
bool isKeyDown(LowlevelKeyboardEvent event)
constexpr bool isKeyDown(LowlevelKeyboardEvent event)
{
return event.wParam == WM_KEYDOWN || event.wParam == WM_SYSKEYDOWN;
}

View File

@ -207,7 +207,7 @@ namespace MonitorUtils
return { .id = str.substr(0, dividerPos), .instanceId = str.substr(dividerPos + 1) };
}
inline bool not_digit(wchar_t ch)
constexpr inline bool not_digit(wchar_t ch)
{
return '0' <= ch && ch <= '9';
}

View File

@ -248,32 +248,6 @@ namespace FancyZonesUtils
return closestIdx;
}
RECT PrepareRectForCycling(RECT windowRect, RECT workAreaRect, DWORD vkCode) noexcept
{
LONG deltaX = 0, deltaY = 0;
switch (vkCode)
{
case VK_UP:
deltaY = workAreaRect.bottom - workAreaRect.top;
break;
case VK_DOWN:
deltaY = workAreaRect.top - workAreaRect.bottom;
break;
case VK_LEFT:
deltaX = workAreaRect.right - workAreaRect.left;
break;
case VK_RIGHT:
deltaX = workAreaRect.left - workAreaRect.right;
}
windowRect.left += deltaX;
windowRect.right += deltaX;
windowRect.top += deltaY;
windowRect.bottom += deltaY;
return windowRect;
}
void SwallowKey(const WORD key) noexcept
{
INPUT inputKey[1] = {};

View File

@ -93,7 +93,7 @@ namespace FancyZonesUtils
}
}
inline BYTE OpacitySettingToAlpha(int opacity)
constexpr inline BYTE OpacitySettingToAlpha(int opacity)
{
return static_cast<BYTE>(opacity * 2.55);
}
@ -168,6 +168,32 @@ namespace FancyZonesUtils
return result;
}
constexpr RECT PrepareRectForCycling(RECT windowRect, RECT workAreaRect, DWORD vkCode) noexcept
{
LONG deltaX = 0, deltaY = 0;
switch (vkCode)
{
case VK_UP:
deltaY = workAreaRect.bottom - workAreaRect.top;
break;
case VK_DOWN:
deltaY = workAreaRect.top - workAreaRect.bottom;
break;
case VK_LEFT:
deltaX = workAreaRect.right - workAreaRect.left;
break;
case VK_RIGHT:
deltaX = workAreaRect.left - workAreaRect.right;
}
windowRect.left += deltaX;
windowRect.right += deltaX;
windowRect.top += deltaY;
windowRect.bottom += deltaY;
return windowRect;
}
UINT GetDpiForMonitor(HMONITOR monitor) noexcept;
void OrderMonitors(std::vector<std::pair<HMONITOR, RECT>>& monitorInfo);
@ -175,7 +201,6 @@ namespace FancyZonesUtils
std::optional<GUID> GuidFromString(const std::wstring& str) noexcept;
std::optional<std::wstring> GuidToString(const GUID& guid) noexcept;
RECT PrepareRectForCycling(RECT windowRect, RECT workAreaRect, DWORD vkCode) noexcept;
size_t ChooseNextZoneByPosition(DWORD vkCode, RECT windowRect, const std::vector<RECT>& zoneRects) noexcept;
void SwallowKey(const WORD key) noexcept;

View File

@ -421,8 +421,12 @@ void KeyDropDownControl::AddShortcutToControl(Shortcut shortcut, StackPanel tabl
}
}
// Disable 26497 this function should be evaluated at compile time
#pragma warning(push)
#pragma warning(disable : 26497)
// Get number of selected keys. Do not count -1 and 0 values as they stand for Not selected and None
int KeyDropDownControl::GetNumberOfSelectedKeys(std::vector<int32_t> keyCodes)
{
return (int)std::count_if(keyCodes.begin(), keyCodes.end(), [](int32_t a) { return a != -1 && a != 0; });
}
}
#pragma warning(pop)

View File

@ -165,7 +165,7 @@ int XamlBridge::MessageLoop()
static const WPARAM invalidKey = (WPARAM)-1;
WPARAM GetKeyFromReason(winrt::Windows::UI::Xaml::Hosting::XamlSourceFocusNavigationReason reason)
constexpr WPARAM GetKeyFromReason(winrt::Windows::UI::Xaml::Hosting::XamlSourceFocusNavigationReason reason)
{
auto key = invalidKey;
if (reason == winrt::Windows::UI::Xaml::Hosting::XamlSourceFocusNavigationReason::Last || reason == winrt::Windows::UI::Xaml::Hosting::XamlSourceFocusNavigationReason::First)

View File

@ -592,13 +592,13 @@ bool Shortcut::CheckModifiersKeyboardState(KeyboardManagerInput::InputInterface&
}
// Helper method for checking if a key is in a range for cleaner code
bool in_range(DWORD key, DWORD a, DWORD b)
constexpr bool in_range(DWORD key, DWORD a, DWORD b)
{
return (key >= a && key <= b);
}
// Helper method for checking if a key is equal to a value for cleaner code
bool equals(DWORD key, DWORD a)
constexpr bool equals(DWORD key, DWORD a)
{
return (key == a);
}

View File

@ -41,12 +41,12 @@ std::string toMediaTypeString(GUID subtype);
#define LOG(str) LogToFile(str, false);
#endif
inline bool failed(HRESULT hr)
constexpr inline bool failed(HRESULT hr)
{
return hr != S_OK;
}
inline bool failed(bool val)
constexpr inline bool failed(bool val)
{
return val == false;
}

View File

@ -6,8 +6,9 @@
#include <dshow.h>
// disable warning 26471 - Don't use reinterpret_cast. A cast from void* can use static_cast
// Disable 26497 for winrt - This function function-name could be marked constexpr if compile-time evaluation is desired.
#pragma warning(push)
#pragma warning(disable: 26471)
#pragma warning(disable : 26471 26497)
#include <wil/com.h>
#pragma warning(push)

View File

@ -6,8 +6,9 @@
#include <cguid.h>
// disable warning 26471 - Don't use reinterpret_cast. A cast from void* can use static_cast
// Disable 26497 for winrt - This function function-name could be marked constexpr if compile-time evaluation is desired.
#pragma warning(push)
#pragma warning(disable: 26471)
#pragma warning(disable : 26471 26497)
#include <wil/com.h>
#pragma warning(push)