From f5f5a3f79e4a3ac5c65edf04ebf48bc7e62f35da Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Thu, 23 Jan 2025 16:18:58 +0100 Subject: [PATCH] Fix some clippy issues on the Android build (#35147) This fixes a variety of clippy issues that show up on the Android build. Signed-off-by: Martin Robinson --- .../platform/freetype/android/font_list.rs | 47 ++++++---------- .../fonts/platform/freetype/android/xml.rs | 2 +- ports/servoshell/egl/android.rs | 54 ++++++++----------- ports/servoshell/egl/android/simpleservo.rs | 4 +- ports/servoshell/prefs.rs | 2 + 5 files changed, 43 insertions(+), 66 deletions(-) diff --git a/components/fonts/platform/freetype/android/font_list.rs b/components/fonts/platform/freetype/android/font_list.rs index b45cbf66946..557425bdb25 100644 --- a/components/fonts/platform/freetype/android/font_list.rs +++ b/components/fonts/platform/freetype/android/font_list.rs @@ -19,7 +19,7 @@ use crate::{ LocalFontIdentifier, LowercaseFontFamilyName, }; -static FONT_LIST: LazyLock = LazyLock::new(|| FontList::new()); +static FONT_LIST: LazyLock = LazyLock::new(FontList::new); // Android doesn't provide an API to query system fonts until Android O: // https://developer.android.com/reference/android/text/FontConfig.html @@ -141,7 +141,7 @@ impl FontList { let mut result = None; paths.iter().all(|path| { result = Self::from_path(path); - !result.is_some() + result.is_none() }); if result.is_none() { @@ -194,10 +194,7 @@ impl FontList { } } - Some(FontList { - families: families, - aliases: aliases, - }) + Some(FontList { families, aliases }) } // Fonts expected to exist in Android devices. @@ -277,24 +274,19 @@ impl FontList { let mut fonts = Vec::new(); // Parse font variants for node in familyset { - match node { - Node::Element { - name, - attributes, - children, - } => { - if name.local_name == "font" { - FontList::parse_font(&children, attributes, &mut fonts); - } - }, - _ => {}, + if let Node::Element { + name, + attributes, + children, + } = node + { + if name.local_name == "font" { + FontList::parse_font(children, attributes, &mut fonts); + } } } - out.push(FontFamily { - name: name, - fonts: fonts, - }); + out.push(FontFamily { name, fonts }); } // Parse family and font file names for Androi API < 21 @@ -339,10 +331,7 @@ impl FontList { .collect(); if !fonts.is_empty() { - out.push(FontFamily { - name: name, - fonts: fonts, - }) + out.push(FontFamily { name, fonts }) } } } @@ -393,11 +382,7 @@ impl FontList { // Parse optional weight filter let weight = Self::find_attrib("weight", attrs).and_then(|w| w.parse().ok()); - out.push(FontAlias { - from: from, - to: to, - weight: weight, - }) + out.push(FontAlias { from, to, weight }) } fn find_attrib(name: &str, attrs: &[Attribute]) -> Option { @@ -408,7 +393,7 @@ impl FontList { } fn text_content(nodes: &[Node]) -> Option { - nodes.get(0).and_then(|child| match child { + nodes.first().and_then(|child| match child { Node::Text(contents) => Some(contents.trim().into()), Node::Element { .. } => None, }) diff --git a/components/fonts/platform/freetype/android/xml.rs b/components/fonts/platform/freetype/android/xml.rs index 42a21a05829..c9f0dcc07af 100644 --- a/components/fonts/platform/freetype/android/xml.rs +++ b/components/fonts/platform/freetype/android/xml.rs @@ -17,7 +17,7 @@ pub(super) enum Node { pub(super) fn parse(bytes: &[u8]) -> xml::reader::Result> { let mut stack = Vec::new(); let mut nodes = Vec::new(); - for result in xml::EventReader::new(&*bytes) { + for result in xml::EventReader::new(bytes) { match result? { StartElement { name, attributes, .. diff --git a/ports/servoshell/egl/android.rs b/ports/servoshell/egl/android.rs index 8845e43af85..1a8cd9337db 100644 --- a/ports/servoshell/egl/android.rs +++ b/ports/servoshell/egl/android.rs @@ -273,9 +273,7 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_scrollStart<'local>( y: jint, ) { debug!("scrollStart"); - call(&mut env, |s| { - s.scroll_start(dx as f32, dy as f32, x as i32, y as i32) - }); + call(&mut env, |s| s.scroll_start(dx as f32, dy as f32, x, y)); } #[no_mangle] @@ -288,9 +286,7 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_scrollEnd<'local>( y: jint, ) { debug!("scrollEnd"); - call(&mut env, |s| { - s.scroll_end(dx as f32, dy as f32, x as i32, y as i32) - }); + call(&mut env, |s| s.scroll_end(dx as f32, dy as f32, x, y)); } #[no_mangle] @@ -303,9 +299,7 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_scroll<'local>( y: jint, ) { debug!("scroll"); - call(&mut env, |s| { - s.scroll(dx as f32, dy as f32, x as i32, y as i32) - }); + call(&mut env, |s| s.scroll(dx as f32, dy as f32, x, y)); } #[no_mangle] @@ -317,7 +311,7 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_touchDown<'local>( pointer_id: jint, ) { debug!("touchDown"); - call(&mut env, |s| s.touch_down(x, y, pointer_id as i32)); + call(&mut env, |s| s.touch_down(x, y, pointer_id)); } #[no_mangle] @@ -329,7 +323,7 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_touchUp<'local>( pointer_id: jint, ) { debug!("touchUp"); - call(&mut env, |s| s.touch_up(x, y, pointer_id as i32)); + call(&mut env, |s| s.touch_up(x, y, pointer_id)); } #[no_mangle] @@ -341,7 +335,7 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_touchMove<'local>( pointer_id: jint, ) { debug!("touchMove"); - call(&mut env, |s| s.touch_move(x, y, pointer_id as i32)); + call(&mut env, |s| s.touch_move(x, y, pointer_id)); } #[no_mangle] @@ -353,7 +347,7 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_touchCancel<'local>( pointer_id: jint, ) { debug!("touchCancel"); - call(&mut env, |s| s.touch_cancel(x, y, pointer_id as i32)); + call(&mut env, |s| s.touch_cancel(x, y, pointer_id)); } #[no_mangle] @@ -365,9 +359,7 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_pinchZoomStart<'local>( y: jint, ) { debug!("pinchZoomStart"); - call(&mut env, |s| { - s.pinchzoom_start(factor as f32, x as u32, y as u32) - }); + call(&mut env, |s| s.pinchzoom_start(factor, x as u32, y as u32)); } #[no_mangle] @@ -379,7 +371,7 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_pinchZoom<'local>( y: jint, ) { debug!("pinchZoom"); - call(&mut env, |s| s.pinchzoom(factor as f32, x as u32, y as u32)); + call(&mut env, |s| s.pinchzoom(factor, x as u32, y as u32)); } #[no_mangle] @@ -391,26 +383,24 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_pinchZoomEnd<'local>( y: jint, ) { debug!("pinchZoomEnd"); - call(&mut env, |s| { - s.pinchzoom_end(factor as f32, x as u32, y as u32) - }); + call(&mut env, |s| s.pinchzoom_end(factor, x as u32, y as u32)); } #[no_mangle] -pub extern "C" fn Java_org_servo_servoview_JNIServo_click<'local>( - mut env: JNIEnv<'local>, - _: JClass<'local>, +pub extern "C" fn Java_org_servo_servoview_JNIServo_click( + mut env: JNIEnv, + _: JClass, x: jfloat, y: jfloat, ) { debug!("click"); - call(&mut env, |s| s.click(x as f32, y as f32)); + call(&mut env, |s| s.click(x, y)); } #[no_mangle] -pub extern "C" fn Java_org_servo_servoview_JNIServo_pauseCompositor<'local>( +pub extern "C" fn Java_org_servo_servoview_JNIServo_pauseCompositor( mut env: JNIEnv, - _: JClass<'local>, + _: JClass<'_>, ) { debug!("pauseCompositor"); call(&mut env, |s| s.pause_compositor()); @@ -439,7 +429,7 @@ pub extern "C" fn Java_org_servo_servoview_JNIServo_mediaSessionAction<'local>( action: jint, ) { debug!("mediaSessionAction"); - call(&mut env, |s| s.media_session_action((action as i32).into())); + call(&mut env, |s| s.media_session_action((action).into())); } pub struct WakeupCallback { @@ -531,7 +521,7 @@ impl HostTrait for HostCallbacks { fn on_title_changed(&self, title: Option) { debug!("on_title_changed"); let mut env = self.jvm.get_env().unwrap(); - let title = title.unwrap_or_else(String::new); + let title = title.unwrap_or_default(); let Ok(title_string) = new_string_as_jvalue(&mut env, &title) else { return; }; @@ -557,8 +547,8 @@ impl HostTrait for HostCallbacks { &[(&url_string).into()], ); match allow { - Ok(allow) => return allow.z().unwrap(), - Err(_) => return true, + Ok(allow) => allow.z().unwrap(), + Err(_) => true, } } @@ -761,8 +751,8 @@ fn get_field<'local>( } env.get_field(obj, field, type_) - .map(|value| Some(value)) - .or_else(|_| Err(format!("Can't find `{}` field", field))) + .map(Some) + .map_err(|_| format!("Can't find `{}` field", field)) } fn get_non_null_field<'local>( diff --git a/ports/servoshell/egl/android/simpleservo.rs b/ports/servoshell/egl/android/simpleservo.rs index 2e0b4786a11..30a649c1756 100644 --- a/ports/servoshell/egl/android/simpleservo.rs +++ b/ports/servoshell/egl/android/simpleservo.rs @@ -30,7 +30,7 @@ use crate::egl::servo_glue::{ use crate::prefs::{parse_command_line_arguments, ArgumentParsingResult}; thread_local! { - pub static SERVO: RefCell> = RefCell::new(None); + pub static SERVO: RefCell> = const { RefCell::new(None) }; } pub struct InitOptions { @@ -61,7 +61,7 @@ pub fn init( resources::set(Box::new(ResourceReaderInstance::new())); // `parse_command_line_arguments` expects the first argument to be the binary name. - let mut args = mem::replace(&mut init_opts.args, vec![]); + let mut args = mem::take(&mut init_opts.args); args.insert(0, "servo".to_string()); let (opts, preferences, servoshell_preferences) = match parse_command_line_arguments(args) { diff --git a/ports/servoshell/prefs.rs b/ports/servoshell/prefs.rs index 4ba757bccc7..e732403fa54 100644 --- a/ports/servoshell/prefs.rs +++ b/ports/servoshell/prefs.rs @@ -17,6 +17,7 @@ use servo::config::prefs::{PrefValue, Preferences}; use servo::url::ServoUrl; use url::Url; +#[cfg_attr(any(target_os = "android", target_env = "ohos"), allow(dead_code))] pub(crate) struct ServoShellPreferences { /// The user agent to use for servoshell. pub user_agent: Option, @@ -138,6 +139,7 @@ pub fn read_prefs_map(txt: &str) -> HashMap { } #[allow(clippy::large_enum_variant)] +#[cfg_attr(any(target_os = "android", target_env = "ohos"), allow(dead_code))] pub(crate) enum ArgumentParsingResult { ChromeProcess(Opts, Preferences, ServoShellPreferences), ContentProcess(String),