From 41aa0622720e94a932fef8e747ee57fa96153744 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Tue, 14 Feb 2023 14:50:04 -0700 Subject: [PATCH] Add `modernize-use-auto` clang-tidy check --- .clang-tidy | 1 - src/SFML/Audio/SoundFileReaderMp3.cpp | 6 +++--- src/SFML/Graphics/Image.cpp | 2 +- src/SFML/Network/Ftp.cpp | 4 ++-- src/SFML/Network/Packet.cpp | 2 +- src/SFML/Window/DRM/DRMContext.cpp | 8 ++++---- src/SFML/Window/OSX/HIDInputManager.mm | 20 ++++++++++---------- src/SFML/Window/OSX/HIDJoystickManager.cpp | 4 ++-- src/SFML/Window/OSX/JoystickImpl.cpp | 17 ++++++++--------- src/SFML/Window/OSX/SFWindowController.mm | 6 +++--- src/SFML/Window/OSX/VideoModeImpl.cpp | 2 +- src/SFML/Window/OSX/cpp_objc_conversion.mm | 4 ++-- src/SFML/Window/Unix/KeyboardImpl.cpp | 5 ++--- src/SFML/Window/Win32/InputImpl.cpp | 6 +++--- test/Graphics/Color.test.cpp | 2 +- 15 files changed, 43 insertions(+), 46 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 4af5a31f..0eab94b8 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -19,7 +19,6 @@ Checks: > -modernize-macro-to-enum, -modernize-pass-by-value, -modernize-return-braced-init-list, - -modernize-use-auto, -modernize-use-nodiscard, -modernize-use-trailing-return-type, -readability-braces-around-statements, diff --git a/src/SFML/Audio/SoundFileReaderMp3.cpp b/src/SFML/Audio/SoundFileReaderMp3.cpp index ce51eeeb..47f9ac74 100644 --- a/src/SFML/Audio/SoundFileReaderMp3.cpp +++ b/src/SFML/Audio/SoundFileReaderMp3.cpp @@ -56,14 +56,14 @@ namespace { std::size_t readCallback(void* ptr, std::size_t size, void* data) { - sf::InputStream* stream = static_cast(data); + auto* stream = static_cast(data); return static_cast(stream->read(ptr, static_cast(size))); } int seekCallback(std::uint64_t offset, void* data) { - sf::InputStream* stream = static_cast(data); - std::int64_t position = stream->seek(static_cast(offset)); + auto* stream = static_cast(data); + std::int64_t position = stream->seek(static_cast(offset)); return position < 0 ? -1 : 0; } diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index 99e0d112..f34fb07f 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -227,7 +227,7 @@ void Image::createMaskFromColor(const Color& color, std::uint8_t alpha) // Interpolate RGBA components using the alpha values of the destination and source pixels std::uint8_t srcAlpha = src[3]; std::uint8_t dstAlpha = dst[3]; - std::uint8_t outAlpha = static_cast(srcAlpha + dstAlpha - srcAlpha * dstAlpha / 255); + auto outAlpha = static_cast(srcAlpha + dstAlpha - srcAlpha * dstAlpha / 255); dst[3] = outAlpha; diff --git a/src/SFML/Network/Ftp.cpp b/src/SFML/Network/Ftp.cpp index e2ed1536..92498314 100644 --- a/src/SFML/Network/Ftp.cpp +++ b/src/SFML/Network/Ftp.cpp @@ -555,8 +555,8 @@ Ftp::Response Ftp::DataChannel::open(Ftp::TransferMode mode) } // Reconstruct connection port and address - unsigned short port = static_cast(data[4] * 256 + data[5]); - IpAddress address(data[0], data[1], data[2], data[3]); + auto port = static_cast(data[4] * 256 + data[5]); + IpAddress address(data[0], data[1], data[2], data[3]); // Connect the data channel to the server if (m_dataSocket.connect(address, port) == Socket::Status::Done) diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp index c2e49094..1fc05ca1 100644 --- a/src/SFML/Network/Packet.cpp +++ b/src/SFML/Network/Packet.cpp @@ -434,7 +434,7 @@ Packet& Packet::operator<<(std::uint16_t data) //////////////////////////////////////////////////////////// Packet& Packet::operator<<(std::int32_t data) { - std::int32_t toWrite = static_cast(htonl(static_cast(data))); + auto toWrite = static_cast(htonl(static_cast(data))); append(&toWrite, sizeof(toWrite)); return *this; } diff --git a/src/SFML/Window/DRM/DRMContext.cpp b/src/SFML/Window/DRM/DRMContext.cpp index 52f965b5..5cfeb874 100644 --- a/src/SFML/Window/DRM/DRMContext.cpp +++ b/src/SFML/Window/DRM/DRMContext.cpp @@ -134,8 +134,8 @@ void cleanup() void drmFbDestroyCallback(gbm_bo* bo, void* data) { - int drmFd = gbm_device_get_fd(gbm_bo_get_device(bo)); - DrmFb* fb = static_cast(data); + int drmFd = gbm_device_get_fd(gbm_bo_get_device(bo)); + auto* fb = static_cast(data); if (fb->fbId) drmModeRmFB(drmFd, fb->fbId); @@ -145,8 +145,8 @@ void drmFbDestroyCallback(gbm_bo* bo, void* data) DrmFb* drmFbGetFromBo(gbm_bo& bo) { - int drmFd = gbm_device_get_fd(gbm_bo_get_device(&bo)); - DrmFb* fb = static_cast(gbm_bo_get_user_data(&bo)); + int drmFd = gbm_device_get_fd(gbm_bo_get_device(&bo)); + auto* fb = static_cast(gbm_bo_get_user_data(&bo)); if (fb) return fb; diff --git a/src/SFML/Window/OSX/HIDInputManager.mm b/src/SFML/Window/OSX/HIDInputManager.mm index 34286413..d9c41af6 100644 --- a/src/SFML/Window/OSX/HIDInputManager.mm +++ b/src/SFML/Window/OSX/HIDInputManager.mm @@ -58,7 +58,7 @@ long HIDInputManager::getLocationID(IOHIDDeviceRef device) if (!typeRef || (CFGetTypeID(typeRef) != CFNumberGetTypeID())) return 0; - CFNumberRef locRef = static_cast(typeRef); + const auto* locRef = static_cast(typeRef); if (!CFNumberGetValue(locRef, kCFNumberLongType, &loc)) return 0; @@ -753,7 +753,7 @@ void HIDInputManager::initializeKeyboard() return; } - NSSet* keyboards = static_cast(underlying); // Toll-Free Bridge + auto* keyboards = static_cast(underlying); // Toll-Free Bridge for (id keyboard in keyboards) loadKeyboard(static_cast(keyboard)); @@ -774,10 +774,10 @@ void HIDInputManager::loadKeyboard(IOHIDDeviceRef keyboard) return; } - NSArray* keys = static_cast(underlying); // Toll-Free Bridge + auto* keys = static_cast(underlying); // Toll-Free Bridge for (id key in keys) { - IOHIDElementRef elem = static_cast(key); + auto* elem = static_cast(key); if (IOHIDElementGetUsagePage(elem) == kHIDPage_KeyboardOrKeypad) loadKey(elem); } @@ -809,8 +809,8 @@ void HIDInputManager::buildMappings() key = Keyboard::Unknown; // Get the current keyboard layout - TISInputSourceRef tis = TISCopyCurrentKeyboardLayoutInputSource(); - CFDataRef layoutData = static_cast(TISGetInputSourceProperty(tis, kTISPropertyUnicodeKeyLayoutData)); + TISInputSourceRef tis = TISCopyCurrentKeyboardLayoutInputSource(); + const auto* layoutData = static_cast(TISGetInputSourceProperty(tis, kTISPropertyUnicodeKeyLayoutData)); if (layoutData == nullptr) { @@ -819,14 +819,14 @@ void HIDInputManager::buildMappings() return; } - UCKeyboardLayout* layout = reinterpret_cast(const_cast(CFDataGetBytePtr(layoutData))); + auto* layout = reinterpret_cast(const_cast(CFDataGetBytePtr(layoutData))); // For each scancode having a IOHIDElement, we translate the corresponding // virtual code to a localized Key. for (int i = 0; i < static_cast(Keyboard::Scan::ScancodeCount); ++i) { - Keyboard::Scancode scan = static_cast(i); - std::uint8_t virtualCode = scanToVirtualCode(scan); + auto scan = static_cast(i); + std::uint8_t virtualCode = scanToVirtualCode(scan); if (virtualCode == unknownVirtualCode) continue; @@ -912,7 +912,7 @@ void HIDInputManager::keyboardChanged(CFNotificationCenterRef /* center */, const void* /* object */, CFDictionaryRef /* userInfo */) { - HIDInputManager* manager = static_cast(observer); + auto* manager = static_cast(observer); manager->buildMappings(); } diff --git a/src/SFML/Window/OSX/HIDJoystickManager.cpp b/src/SFML/Window/OSX/HIDJoystickManager.cpp index 57ebeccc..38782add 100644 --- a/src/SFML/Window/OSX/HIDJoystickManager.cpp +++ b/src/SFML/Window/OSX/HIDJoystickManager.cpp @@ -123,7 +123,7 @@ void HIDJoystickManager::update() //////////////////////////////////////////////////////////// void HIDJoystickManager::pluggedIn(void* context, IOReturn, void*, IOHIDDeviceRef) { - HIDJoystickManager* manager = static_cast(context); + auto* manager = static_cast(context); ++manager->m_joystickCount; } @@ -131,7 +131,7 @@ void HIDJoystickManager::pluggedIn(void* context, IOReturn, void*, IOHIDDeviceRe //////////////////////////////////////////////////////////// void HIDJoystickManager::pluggedOut(void* context, IOReturn, void*, IOHIDDeviceRef) { - HIDJoystickManager* manager = static_cast(context); + auto* manager = static_cast(context); --manager->m_joystickCount; } diff --git a/src/SFML/Window/OSX/JoystickImpl.cpp b/src/SFML/Window/OSX/JoystickImpl.cpp index f2c5cdc2..1a2b6807 100644 --- a/src/SFML/Window/OSX/JoystickImpl.cpp +++ b/src/SFML/Window/OSX/JoystickImpl.cpp @@ -58,7 +58,7 @@ std::string getDeviceString(IOHIDDeviceRef ref, CFStringRef prop, unsigned int i CFTypeRef typeRef = IOHIDDeviceGetProperty(ref, prop); if (typeRef && (CFGetTypeID(typeRef) == CFStringGetTypeID())) { - CFStringRef str = static_cast(typeRef); + const auto* str = static_cast(typeRef); return stringFromCFString(str); } @@ -153,8 +153,7 @@ bool JoystickImpl::isConnected(unsigned int index) for (CFIndex didx(0); !state && didx < size; ++didx) { - IOHIDDeviceRef d = static_cast( - const_cast(array[static_cast(didx)])); + auto* d = static_cast(const_cast(array[static_cast(didx)])); Location dloc = HIDInputManager::getLocationID(d); bool foundJ = false; @@ -205,7 +204,7 @@ bool JoystickImpl::open(unsigned int index) IOHIDDeviceRef self = nil; for (CFIndex i(0); self == nil && i < joysticksCount; ++i) { - IOHIDDeviceRef d = static_cast(const_cast(devicesArray[static_cast(i)])); + auto* d = static_cast(const_cast(devicesArray[static_cast(i)])); if (deviceLoc == HIDInputManager::getLocationID(d)) self = d; } @@ -233,7 +232,7 @@ bool JoystickImpl::open(unsigned int index) CFIndex elementsCount = CFArrayGetCount(elements); for (int i = 0; i < elementsCount; ++i) { - IOHIDElementRef element = static_cast(const_cast(CFArrayGetValueAtIndex(elements, i))); + auto* element = static_cast(const_cast(CFArrayGetValueAtIndex(elements, i))); switch (IOHIDElementGetUsagePage(element)) { case kHIDPage_GenericDesktop: @@ -424,7 +423,7 @@ JoystickState JoystickImpl::update() bool found = false; for (CFIndex i(0); !found && i < joysticksCount; ++i) { - IOHIDDeviceRef d = static_cast(const_cast(devicesArray[static_cast(i)])); + auto* d = static_cast(const_cast(devicesArray[static_cast(i)])); if (selfLoc == HIDInputManager::getLocationID(d)) found = true; } @@ -475,12 +474,12 @@ JoystickState JoystickImpl::update() // This method might not be very accurate (the "0 position" can be // slightly shift with some device) but we don't care because most // of devices are so sensitive that this is not relevant. - double physicalMax = static_cast(IOHIDElementGetPhysicalMax(iohidElementRef)); - double physicalMin = static_cast(IOHIDElementGetPhysicalMin(iohidElementRef)); + auto physicalMax = static_cast(IOHIDElementGetPhysicalMax(iohidElementRef)); + auto physicalMin = static_cast(IOHIDElementGetPhysicalMin(iohidElementRef)); double scaledMin = -100; double scaledMax = 100; double physicalValue = IOHIDValueGetScaledValue(value, kIOHIDValueScaleTypePhysical); - float scaledValue = static_cast( + auto scaledValue = static_cast( (((physicalValue - physicalMin) * (scaledMax - scaledMin)) / (physicalMax - physicalMin)) + scaledMin); state.axes[axis] = scaledValue; } diff --git a/src/SFML/Window/OSX/SFWindowController.mm b/src/SFML/Window/OSX/SFWindowController.mm index bed6aa73..5403982e 100644 --- a/src/SFML/Window/OSX/SFWindowController.mm +++ b/src/SFML/Window/OSX/SFWindowController.mm @@ -606,9 +606,9 @@ //////////////////////////////////////////////////////// - (float)screenHeight { - NSDictionary* deviceDescription = [[m_window screen] deviceDescription]; - NSNumber* screenNumber = [deviceDescription valueForKey:@"NSScreenNumber"]; - CGDirectDisplayID screenID = static_cast([screenNumber intValue]); + NSDictionary* deviceDescription = [[m_window screen] deviceDescription]; + NSNumber* screenNumber = [deviceDescription valueForKey:@"NSScreenNumber"]; + auto screenID = static_cast([screenNumber intValue]); return static_cast(CGDisplayPixelsHigh(screenID)); } diff --git a/src/SFML/Window/OSX/VideoModeImpl.cpp b/src/SFML/Window/OSX/VideoModeImpl.cpp index e56b0b3f..f92e1dc9 100644 --- a/src/SFML/Window/OSX/VideoModeImpl.cpp +++ b/src/SFML/Window/OSX/VideoModeImpl.cpp @@ -56,7 +56,7 @@ std::vector VideoModeImpl::getFullscreenModes() const CFIndex modesCount = CFArrayGetCount(cgmodes); for (CFIndex i = 0; i < modesCount; ++i) { - CGDisplayModeRef cgmode = static_cast(const_cast(CFArrayGetValueAtIndex(cgmodes, i))); + auto* cgmode = static_cast(const_cast(CFArrayGetValueAtIndex(cgmodes, i))); VideoMode mode = convertCGModeToSFMode(cgmode); diff --git a/src/SFML/Window/OSX/cpp_objc_conversion.mm b/src/SFML/Window/OSX/cpp_objc_conversion.mm index 153c8440..fa805377 100644 --- a/src/SFML/Window/OSX/cpp_objc_conversion.mm +++ b/src/SFML/Window/OSX/cpp_objc_conversion.mm @@ -45,8 +45,8 @@ NSString* stringToNSString(const std::string& string) //////////////////////////////////////////////////////////// NSString* sfStringToNSString(const sf::String& string) { - std::uint32_t length = static_cast(string.getSize() * sizeof(std::uint32_t)); - const void* data = reinterpret_cast(string.getData()); + auto length = static_cast(string.getSize() * sizeof(std::uint32_t)); + const void* data = reinterpret_cast(string.getData()); NSStringEncoding encoding; if (NSHostByteOrder() == NS_LittleEndian) diff --git a/src/SFML/Window/Unix/KeyboardImpl.cpp b/src/SFML/Window/Unix/KeyboardImpl.cpp index aaac56e4..c4dc9dd0 100644 --- a/src/SFML/Window/Unix/KeyboardImpl.cpp +++ b/src/SFML/Window/Unix/KeyboardImpl.cpp @@ -484,9 +484,8 @@ void ensureMapping() std::memcpy(name, descriptor->names->keys[keycode].name, XkbKeyNameLength); name[XkbKeyNameLength] = '\0'; - std::unordered_map::iterator mappedScancode = nameScancodeMap.find( - std::string(name)); - scancode = sf::Keyboard::Scan::Unknown; + auto mappedScancode = nameScancodeMap.find(std::string(name)); + scancode = sf::Keyboard::Scan::Unknown; if (mappedScancode != nameScancodeMap.end()) scancode = mappedScancode->second; diff --git a/src/SFML/Window/Win32/InputImpl.cpp b/src/SFML/Window/Win32/InputImpl.cpp index 67e9e2b7..5ce16e28 100644 --- a/src/SFML/Window/Win32/InputImpl.cpp +++ b/src/SFML/Window/Win32/InputImpl.cpp @@ -543,9 +543,9 @@ void InputImpl::ensureMappings() // Phase 2: Translate scancode to virtual code to key names for (int i = 0; i < static_cast(Keyboard::Scan::ScancodeCount); ++i) { - Keyboard::Scancode scan = static_cast(i); - UINT virtualKey = sfScanToVirtualKey(scan); - Keyboard::Key key = virtualKeyToSfKey(virtualKey); + auto scan = static_cast(i); + UINT virtualKey = sfScanToVirtualKey(scan); + Keyboard::Key key = virtualKeyToSfKey(virtualKey); if (key != Keyboard::Unknown && m_keyToScancodeMapping[key] == Keyboard::Scan::Unknown) m_keyToScancodeMapping[key] = scan; m_scancodeToKeyMapping[static_cast(scan)] = key; diff --git a/test/Graphics/Color.test.cpp b/test/Graphics/Color.test.cpp index 0ed31eb3..f7ac4aa8 100644 --- a/test/Graphics/Color.test.cpp +++ b/test/Graphics/Color.test.cpp @@ -166,7 +166,7 @@ TEST_CASE("[Graphics] sf::Color") static_assert(alignof(sf::Color) == 1); const std::vector pixels = {{10, 11, 12, 13}, {14, 15, 16, 17}, {18, 19, 20, 21}}; - const std::uint8_t* begin = reinterpret_cast(pixels.data()); + const auto* begin = reinterpret_cast(pixels.data()); CHECK(begin[0] == pixels[0].r); CHECK(begin[1] == pixels[0].g); CHECK(begin[2] == pixels[0].b);