From 18df9aafe7daaa752a1c31041e13944a6c7dd048 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Thu, 3 Aug 2023 21:20:26 +0700 Subject: [PATCH] Some clippy fixes. (#624) --- cocoa-foundation/src/foundation.rs | 33 ++++++++++----------------- cocoa-foundation/tests/foundation.rs | 4 ++-- cocoa/examples/color.rs | 2 +- cocoa/examples/tab_view.rs | 2 +- cocoa/src/appkit.rs | 2 +- core-foundation-sys/src/base.rs | 11 ++++----- core-foundation/src/array.rs | 12 +++++++--- core-foundation/src/data.rs | 8 ++++++- core-foundation/src/date.rs | 2 +- core-foundation/src/dictionary.rs | 10 ++++---- core-foundation/src/set.rs | 7 +++++- core-foundation/src/url.rs | 2 +- core-foundation/src/uuid.rs | 6 ++--- core-graphics-types/src/geometry.rs | 7 ++---- core-graphics/src/context.rs | 2 +- core-graphics/src/display.rs | 9 ++++---- core-graphics/src/path.rs | 2 +- core-graphics/src/private.rs | 10 ++++---- core-text/src/font.rs | 34 +++++++++++++--------------- io-surface/src/lib.rs | 2 +- 20 files changed, 83 insertions(+), 84 deletions(-) diff --git a/cocoa-foundation/src/foundation.rs b/cocoa-foundation/src/foundation.rs index 26da47e..60eaa4f 100644 --- a/cocoa-foundation/src/foundation.rs +++ b/cocoa-foundation/src/foundation.rs @@ -48,7 +48,7 @@ mod macos { impl NSPoint { #[inline] pub fn new(x: CGFloat, y: CGFloat) -> NSPoint { - NSPoint { x: x, y: y } + NSPoint { x, y } } } @@ -73,10 +73,7 @@ mod macos { impl NSSize { #[inline] pub fn new(width: CGFloat, height: CGFloat) -> NSSize { - NSSize { - width: width, - height: height, - } + NSSize { width, height } } } @@ -101,10 +98,7 @@ mod macos { impl NSRect { #[inline] pub fn new(origin: NSPoint, size: NSSize) -> NSRect { - NSRect { - origin: origin, - size: size, - } + NSRect { origin, size } } #[inline] @@ -169,10 +163,7 @@ pub struct NSRange { impl NSRange { #[inline] pub fn new(location: NSUInteger, length: NSUInteger) -> NSRange { - NSRange { - location: location, - length: length, - } + NSRange { location, length } } } @@ -216,9 +207,9 @@ impl NSOperatingSystemVersion { patchVersion: NSUInteger, ) -> NSOperatingSystemVersion { NSOperatingSystemVersion { - majorVersion: majorVersion, - minorVersion: minorVersion, - patchVersion: patchVersion, + majorVersion, + minorVersion, + patchVersion, } } } @@ -663,10 +654,10 @@ impl NSString for id { } unsafe fn init_str(self, string: &str) -> id { - return msg_send![self, - initWithBytes:string.as_ptr() - length:string.len() - encoding:UTF8_ENCODING as id]; + msg_send![self, + initWithBytes:string.as_ptr() + length:string.len() + encoding:UTF8_ENCODING as id] } unsafe fn len(self) -> usize { @@ -734,7 +725,7 @@ impl Iterator for NSFastIterator { } if self.idx < self.len { - let object = unsafe { *self.state.items_ptr.offset(self.idx as isize) }; + let object = unsafe { *self.state.items_ptr.add(self.idx) }; self.mut_val = Some(new_mut); self.idx += 1; Some(object) diff --git a/cocoa-foundation/tests/foundation.rs b/cocoa-foundation/tests/foundation.rs index 19d7b15..f139b07 100644 --- a/cocoa-foundation/tests/foundation.rs +++ b/cocoa-foundation/tests/foundation.rs @@ -116,8 +116,8 @@ mod foundation { #[test] fn test_get() { - const KEY: &'static str = "The key"; - const VALUE: &'static str = "Some value"; + const KEY: &str = "The key"; + const VALUE: &str = "Some value"; unsafe { let key = NSString::alloc(nil).init_str(KEY); let value = NSString::alloc(nil).init_str(VALUE); diff --git a/cocoa/examples/color.rs b/cocoa/examples/color.rs index dbafd1e..d440426 100644 --- a/cocoa/examples/color.rs +++ b/cocoa/examples/color.rs @@ -133,5 +133,5 @@ unsafe fn create_app() -> id { let current_app = NSRunningApplication::currentApplication(nil); current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps); - return app; + app } diff --git a/cocoa/examples/tab_view.rs b/cocoa/examples/tab_view.rs index 73519b3..1b0a6fe 100644 --- a/cocoa/examples/tab_view.rs +++ b/cocoa/examples/tab_view.rs @@ -83,5 +83,5 @@ unsafe fn create_app(title: id, content: id) -> id { let current_app = NSRunningApplication::currentApplication(nil); current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps); - return app; + app } diff --git a/cocoa/src/appkit.rs b/cocoa/src/appkit.rs index c73839f..7fe1f0b 100644 --- a/cocoa/src/appkit.rs +++ b/cocoa/src/appkit.rs @@ -583,7 +583,7 @@ impl NSApplication for id { unsafe fn presentationOptions_(self) -> NSApplicationPresentationOptions { let options = msg_send![self, presentationOptions]; - return NSApplicationPresentationOptions::from_bits(options).unwrap(); + NSApplicationPresentationOptions::from_bits(options).unwrap() } unsafe fn setMainMenu_(self, menu: id) { diff --git a/core-foundation-sys/src/base.rs b/core-foundation-sys/src/base.rs index fa6eb8d..0bbd402 100644 --- a/core-foundation-sys/src/base.rs +++ b/core-foundation-sys/src/base.rs @@ -49,9 +49,9 @@ pub type CFComparatorFunction = extern "C" fn( context: *mut c_void, ) -> CFComparisonResult; -impl Into for CFComparisonResult { - fn into(self) -> Ordering { - match self { +impl From for Ordering { + fn from(val: CFComparisonResult) -> Self { + match val { CFComparisonResult::LessThan => Ordering::Less, CFComparisonResult::EqualTo => Ordering::Equal, CFComparisonResult::GreaterThan => Ordering::Greater, @@ -69,10 +69,7 @@ pub struct CFRange { // for back-compat impl CFRange { pub fn init(location: CFIndex, length: CFIndex) -> CFRange { - CFRange { - location: location, - length: length, - } + CFRange { location, length } } } diff --git a/core-foundation/src/array.rs b/core-foundation/src/array.rs index 361af30..8f17173 100644 --- a/core-foundation/src/array.rs +++ b/core-foundation/src/array.rs @@ -115,7 +115,7 @@ impl CFArray { /// Core Foundation objects (not always true), they need to be wrapped with /// `TCFType::wrap_under_get_rule()`. #[inline] - pub fn iter<'a>(&'a self) -> CFArrayIterator<'a, T> { + pub fn iter(&self) -> CFArrayIterator<'_, T> { CFArrayIterator { array: self, index: 0, @@ -128,8 +128,14 @@ impl CFArray { unsafe { CFArrayGetCount(self.0) } } + /// Returns `true` if the array contains no elements. #[inline] - pub unsafe fn get_unchecked<'a>(&'a self, index: CFIndex) -> ItemRef<'a, T> + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + #[inline] + pub unsafe fn get_unchecked(&self, index: CFIndex) -> ItemRef<'_, T> where T: FromVoid, { @@ -137,7 +143,7 @@ impl CFArray { } #[inline] - pub fn get<'a>(&'a self, index: CFIndex) -> Option> + pub fn get(&self, index: CFIndex) -> Option> where T: FromVoid, { diff --git a/core-foundation/src/data.rs b/core-foundation/src/data.rs index 15cbbe0..fa4106b 100644 --- a/core-foundation/src/data.rs +++ b/core-foundation/src/data.rs @@ -80,7 +80,7 @@ impl CFData { /// Returns a pointer to the underlying bytes in this data. Note that this byte buffer is /// read-only. #[inline] - pub fn bytes<'a>(&'a self) -> &'a [u8] { + pub fn bytes(&self) -> &[u8] { unsafe { slice::from_raw_parts(CFDataGetBytePtr(self.0), self.len() as usize) } } @@ -89,6 +89,12 @@ impl CFData { pub fn len(&self) -> CFIndex { unsafe { CFDataGetLength(self.0) } } + + /// Returns `true` if this byte buffer is empty. + #[inline] + pub fn is_empty(&self) -> bool { + self.len() == 0 + } } impl Deref for CFData { diff --git a/core-foundation/src/date.rs b/core-foundation/src/date.rs index 5dc7bdb..49c07cc 100644 --- a/core-foundation/src/date.rs +++ b/core-foundation/src/date.rs @@ -79,7 +79,7 @@ mod test { let same_sign = a.is_sign_positive() == b.is_sign_positive(); let equal = ((a - b).abs() / f64::min(a.abs() + b.abs(), f64::MAX)) < f64::EPSILON; - (same_sign && equal) + same_sign && equal } #[test] diff --git a/core-foundation/src/dictionary.rs b/core-foundation/src/dictionary.rs index ac798f3..90cd4a7 100644 --- a/core-foundation/src/dictionary.rs +++ b/core-foundation/src/dictionary.rs @@ -47,7 +47,7 @@ impl CFDictionary { { let (keys, values): (Vec, Vec) = pairs .iter() - .map(|&(ref key, ref value)| (key.as_CFTypeRef(), value.as_CFTypeRef())) + .map(|(key, value)| (key.as_CFTypeRef(), value.as_CFTypeRef())) .unzip(); unsafe { @@ -103,7 +103,7 @@ impl CFDictionary { } #[inline] - pub fn find<'a, T: ToVoid>(&'a self, key: T) -> Option> + pub fn find>(&self, key: T) -> Option> where V: FromVoid, K: ToVoid, @@ -123,7 +123,7 @@ impl CFDictionary { /// Panics if the key is not present in the dictionary. Use `find` to get an `Option` instead /// of panicking. #[inline] - pub fn get<'a, T: ToVoid>(&'a self, key: T) -> ItemRef<'a, V> + pub fn get>(&self, key: T) -> ItemRef<'_, V> where V: FromVoid, K: ToVoid, @@ -195,7 +195,7 @@ impl CFMutableDictionary { V: ToVoid, { let mut result = Self::with_capacity(pairs.len() as _); - for &(ref key, ref value) in pairs { + for (key, value) in pairs { result.add(key, value); } result @@ -265,7 +265,7 @@ impl CFMutableDictionary { K: ToVoid, { let ptr = key.to_void(); - self.find(&key) + self.find(key) .unwrap_or_else(|| panic!("No entry found for key {:p}", ptr)) } diff --git a/core-foundation/src/set.rs b/core-foundation/src/set.rs index 6896ba1..83511ef 100644 --- a/core-foundation/src/set.rs +++ b/core-foundation/src/set.rs @@ -49,8 +49,13 @@ impl CFSet { } impl CFSet { - /// Get the number of elements in the CFSet + /// Get the number of elements in the `CFSet`. pub fn len(&self) -> usize { unsafe { CFSetGetCount(self.0) as usize } } + + /// Returns `true` if the set contains no elements. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } } diff --git a/core-foundation/src/url.rs b/core-foundation/src/url.rs index 9077061..236d8dc 100644 --- a/core-foundation/src/url.rs +++ b/core-foundation/src/url.rs @@ -34,7 +34,7 @@ impl fmt::Debug for CFURL { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { unsafe { let string: CFString = TCFType::wrap_under_get_rule(CFURLGetString(self.0)); - write!(f, "{}", string.to_string()) + write!(f, "{}", string) } } } diff --git a/core-foundation/src/uuid.rs b/core-foundation/src/uuid.rs index 5c3aefc..b70a414 100644 --- a/core-foundation/src/uuid.rs +++ b/core-foundation/src/uuid.rs @@ -44,9 +44,9 @@ impl Default for CFUUID { } #[cfg(feature = "with-uuid")] -impl Into for CFUUID { - fn into(self) -> Uuid { - let b = unsafe { CFUUIDGetUUIDBytes(self.0) }; +impl From for Uuid { + fn from(val: CFUUID) -> Self { + let b = unsafe { CFUUIDGetUUIDBytes(val.0) }; let bytes = [ b.byte0, b.byte1, b.byte2, b.byte3, b.byte4, b.byte5, b.byte6, b.byte7, b.byte8, b.byte9, b.byte10, b.byte11, b.byte12, b.byte13, b.byte14, b.byte15, diff --git a/core-graphics-types/src/geometry.rs b/core-graphics-types/src/geometry.rs index de94b0d..7d37bd7 100644 --- a/core-graphics-types/src/geometry.rs +++ b/core-graphics-types/src/geometry.rs @@ -42,10 +42,7 @@ pub struct CGSize { impl CGSize { #[inline] pub fn new(width: CGFloat, height: CGFloat) -> CGSize { - CGSize { - width: width, - height: height, - } + CGSize { width, height } } #[inline] @@ -64,7 +61,7 @@ pub struct CGPoint { impl CGPoint { #[inline] pub fn new(x: CGFloat, y: CGFloat) -> CGPoint { - CGPoint { x: x, y: y } + CGPoint { x, y } } #[inline] diff --git a/core-graphics/src/context.rs b/core-graphics/src/context.rs index 570a234..c8cd8d3 100644 --- a/core-graphics/src/context.rs +++ b/core-graphics/src/context.rs @@ -165,7 +165,7 @@ impl CGContext { unsafe { slice::from_raw_parts_mut( CGBitmapContextGetData(self.as_ptr()) as *mut u8, - (self.height() * self.bytes_per_row()) as usize, + self.height() * self.bytes_per_row(), ) } } diff --git a/core-graphics/src/display.rs b/core-graphics/src/display.rs index a07af07..5d02c3a 100644 --- a/core-graphics/src/display.rs +++ b/core-graphics/src/display.rs @@ -127,7 +127,7 @@ foreign_type! { impl CGDisplay { #[inline] pub fn new(id: CGDirectDisplayID) -> CGDisplay { - CGDisplay { id: id } + CGDisplay { id } } /// Returns the the main display. @@ -462,8 +462,7 @@ impl CGDisplay { pub fn active_displays() -> Result, CGError> { let count = CGDisplay::active_display_count()?; let mut buf: Vec = vec![0; count as usize]; - let result = - unsafe { CGGetActiveDisplayList(count as u32, buf.as_mut_ptr(), ptr::null_mut()) }; + let result = unsafe { CGGetActiveDisplayList(count, buf.as_mut_ptr(), ptr::null_mut()) }; if result == 0 { Ok(buf) } else { @@ -477,7 +476,7 @@ impl CGDisplay { let mut count: u32 = 0; let result = unsafe { CGGetActiveDisplayList(0, ptr::null_mut(), &mut count) }; if result == 0 { - Ok(count as u32) + Ok(count) } else { Err(result) } @@ -603,7 +602,7 @@ impl CGDisplayMode { /// Returns the I/O Kit flags of the specified display mode. #[inline] pub fn io_flags(&self) -> u32 { - unsafe { CGDisplayModeGetIOFlags(self.as_ptr()) as u32 } + unsafe { CGDisplayModeGetIOFlags(self.as_ptr()) } } /// Returns the pixel encoding of the specified display mode. diff --git a/core-graphics/src/path.rs b/core-graphics/src/path.rs index a15949c..4aad221 100644 --- a/core-graphics/src/path.rs +++ b/core-graphics/src/path.rs @@ -83,7 +83,7 @@ pub struct CGPathElementRef<'a> { impl<'a> CGPathElementRef<'a> { fn new<'b>(element: *const CGPathElement) -> CGPathElementRef<'b> { CGPathElementRef { - element: element, + element, phantom: PhantomData, } } diff --git a/core-graphics/src/private.rs b/core-graphics/src/private.rs index d28d452..e324c1a 100644 --- a/core-graphics/src/private.rs +++ b/core-graphics/src/private.rs @@ -31,7 +31,7 @@ impl CGSRegion { unsafe { let mut region = ptr::null_mut(); assert!(ffi::CGSNewRegionWithRect(rect, &mut region) == 0); - CGSRegion { region: region } + CGSRegion { region } } } @@ -43,7 +43,7 @@ impl CGSRegion { ffi::CGSNewRegionWithRectList(rects.as_ptr(), rects.len() as c_uint, &mut region) == 0 ); - CGSRegion { region: region } + CGSRegion { region } } } } @@ -59,9 +59,9 @@ impl CGSSurface { #[inline] pub fn from_ids(context_id: c_uint, window_number: c_int, surface_id: c_uint) -> CGSSurface { CGSSurface { - context_id: context_id, - window_number: window_number, - surface_id: surface_id, + context_id, + window_number, + surface_id, } } diff --git a/core-text/src/font.rs b/core-text/src/font.rs index 6df9750..b483c16 100644 --- a/core-text/src/font.rs +++ b/core-text/src/font.rs @@ -99,10 +99,10 @@ pub enum CTFontNameSpecifier { PostScriptCID, } -impl Into for CTFontNameSpecifier { - fn into(self) -> CFStringRef { +impl From for CFStringRef { + fn from(val: CTFontNameSpecifier) -> Self { unsafe { - match self { + match val { CTFontNameSpecifier::Copyright => kCTFontCopyrightNameKey, CTFontNameSpecifier::Family => kCTFontFamilyNameKey, CTFontNameSpecifier::SubFamily => kCTFontSubFamilyNameKey, @@ -803,7 +803,7 @@ fn macos_version() -> (i32, i32, i32) { .to_string(); match version - .split(".") + .split('.') .map(|x| x.parse().unwrap()) .collect::>()[..] { @@ -1007,21 +1007,19 @@ fn equal_descriptor_different_font() { from_font_desc.attributes().to_untyped(), resized_font_desc.attributes().to_untyped() ); + } else if macos_version() >= (10, 13, 0) { + // this is unsurprising + assert_ne!(from_font_desc, resized_font_desc); + assert_ne!( + from_font_desc.attributes().to_untyped(), + resized_font_desc.attributes().to_untyped() + ); } else { - if macos_version() >= (10, 13, 0) { - // this is unsurprising - assert_ne!(from_font_desc, resized_font_desc); - assert_ne!( - from_font_desc.attributes().to_untyped(), - resized_font_desc.attributes().to_untyped() - ); - } else { - assert_ne!(from_font_desc, resized_font_desc); - assert_eq!( - from_font_desc.attributes().to_untyped(), - resized_font_desc.attributes().to_untyped() - ); - } + assert_ne!(from_font_desc, resized_font_desc); + assert_eq!( + from_font_desc.attributes().to_untyped(), + resized_font_desc.attributes().to_untyped() + ); } } diff --git a/io-surface/src/lib.rs b/io-surface/src/lib.rs index a032843..a6d9a78 100644 --- a/io-surface/src/lib.rs +++ b/io-surface/src/lib.rs @@ -79,7 +79,7 @@ impl TCFType for IOSurface { #[inline] unsafe fn wrap_under_create_rule(obj: IOSurfaceRef) -> IOSurface { assert!(!obj.is_null(), "Attempted to create a NULL object."); - IOSurface { obj: obj } + IOSurface { obj } } #[inline]