From 179598ebbcc23188d8485e59fc8d3da4d8680eb8 Mon Sep 17 00:00:00 2001 From: yvt Date: Fri, 29 May 2020 18:16:52 +0900 Subject: [PATCH] feat(tcw3): address clippy lints - `clone_on_copy` - `type_complexity` (threshold adjusted) - `collapsible_if` - `option_map_unit_fn` - `identity_conversion` - `iter_nth_zero` - `eval_order_dependence` (ignored) - `new_without_default` (adds `impl Default` for a public type) Unaddressed: - `len_zero` - This is a bug in clippy: --- .clippy.toml | 1 + tcw3/src/ui/editing/history.rs | 12 +++++++++--- tcw3/src/ui/scrolling/lineset.rs | 2 +- tcw3/src/ui/theming/stylesheet.rs | 5 ++++- tcw3/src/ui/theming/view.rs | 6 ++++-- tcw3/src/ui/views/entry.rs | 6 ++++-- tcw3/src/ui/views/slider.rs | 3 ++- tcw3/src/ui/views/split.rs | 2 +- tcw3/src/uicore/keybd.rs | 6 ++---- 9 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 .clippy.toml diff --git a/.clippy.toml b/.clippy.toml new file mode 100644 index 00000000..bf2ffdd0 --- /dev/null +++ b/.clippy.toml @@ -0,0 +1 @@ +type-complexity-threshold = 500 diff --git a/tcw3/src/ui/editing/history.rs b/tcw3/src/ui/editing/history.rs index 26f44c36..ff68a254 100644 --- a/tcw3/src/ui/editing/history.rs +++ b/tcw3/src/ui/editing/history.rs @@ -175,9 +175,8 @@ impl, Text> CoalescingCb for &'_ mut T { } } -impl CoalescingState { - /// Construct a `CoalescingState`. - pub fn new() -> Self { +impl Default for CoalescingState { + fn default() -> Self { Self { composition_active: false, has_edit: false, @@ -185,6 +184,13 @@ impl CoalescingState { _phantom: std::marker::PhantomData, } } +} + +impl CoalescingState { + /// Construct a `CoalescingState`. + pub fn new() -> Self { + Self::default() + } /// Resets the state of the algorithm. /// diff --git a/tcw3/src/ui/scrolling/lineset.rs b/tcw3/src/ui/scrolling/lineset.rs index 5538e8bb..904f67cc 100644 --- a/tcw3/src/ui/scrolling/lineset.rs +++ b/tcw3/src/ui/scrolling/lineset.rs @@ -273,7 +273,7 @@ impl Lineset { let (mut iter, range) = self .line_grs .range(range_by_key(LineOff::index, Floor(range.start)..)); - (*iter.nth(0).unwrap(), range.start) + (*iter.next().unwrap(), range.start) }; // Endpoints of the line group (pre-insertion) diff --git a/tcw3/src/ui/theming/stylesheet.rs b/tcw3/src/ui/theming/stylesheet.rs index 083f4dc1..7b22271b 100644 --- a/tcw3/src/ui/theming/stylesheet.rs +++ b/tcw3/src/ui/theming/stylesheet.rs @@ -411,7 +411,10 @@ macro_rules! rule { } ) => {{ let start = $i; - $i += $crate::prop_count! { $($props)* }; + // This lint misfires for `$i` + #[allow(clippy::eval_order_dependence)] { + $i += $crate::prop_count! { $($props)* }; + } let end = $i; $crate::ui::theming::Rule { diff --git a/tcw3/src/ui/theming/view.rs b/tcw3/src/ui/theming/view.rs index 99466a5a..0de1f5cb 100644 --- a/tcw3/src/ui/theming/view.rs +++ b/tcw3/src/ui/theming/view.rs @@ -215,6 +215,7 @@ impl StyledBox { let mut subelems = self.shared.subelems.borrow_mut(); if let Some(i) = subelems.iter().position(|&(r, _)| r == role) { + // There's already a subelement with `role`. Replace or remove it self.shared.style_elem.remove_child(subelems[i].1); if let Some(helem) = helem { subelems[i].1 = helem; @@ -222,6 +223,7 @@ impl StyledBox { subelems.swap_remove(i); } } else { + // There's no subelement with `role` if let Some(helem) = helem { subelems.push((role, helem)); } @@ -452,7 +454,7 @@ impl AbsInnerLayout { Self { subview_layout: subviews .clone() - .map(|&(role, _)| (role, (*props.subview_metrics(role)).clone())) + .map(|&(role, _)| (role, *props.subview_metrics(role))) .collect(), subviews: subviews.map(|x| x.1.clone()).collect(), overrider, @@ -522,7 +524,7 @@ impl Layout for AbsInnerLayout { fn arrange(&self, ctx: &mut LayoutCtx<'_>, size: Vector2) { for (&(role, ref metrics), sv) in self.subview_layout.iter().zip(self.subviews.iter()) { let sv_traits = ctx.subview_size_traits(sv.as_ref()); - let container = box2! {top_left: [0.0, 0.0].into(), size: size.into()}; + let container = box2! {top_left: [0.0, 0.0].into(), size: size}; let mut frame = metrics.arrange(container, sv_traits.preferred); diff --git a/tcw3/src/ui/views/entry.rs b/tcw3/src/ui/views/entry.rs index 5acb4b05..3112612b 100644 --- a/tcw3/src/ui/views/entry.rs +++ b/tcw3/src/ui/views/entry.rs @@ -1086,7 +1086,7 @@ impl ViewListener for EntryCoreListener { .update_layer(wm, view, ctx.hwnd(), visual_bounds, |draw_ctx| { let c = &mut draw_ctx.canvas; - let mut sel_range = sel_range.clone(); + let mut sel_range = *sel_range; let text_layout = &text_layout_info.text_layout; c.save(); @@ -1313,7 +1313,9 @@ impl pal::iface::TextInputCtxEdit for Edit<'_> { } fn set_composition_range(&mut self, range: Option>) { - range.as_ref().map(|r| self.check_range(r)); + if let Some(r) = &range { + self.check_range(r) + } let range = range.map(|r| [r.start, r.end]); if range == self.state.comp_range { diff --git a/tcw3/src/ui/views/slider.rs b/tcw3/src/ui/views/slider.rs index cda6c833..951f271b 100644 --- a/tcw3/src/ui/views/slider.rs +++ b/tcw3/src/ui/views/slider.rs @@ -318,6 +318,7 @@ impl SliderRaw { for &(role, label) in children.iter() { if let Some((new_value, new_widget)) = label { + // Assign a label let new_value = new_value as f32; let wrap_view = if let Some(label) = labels.iter_mut().find(|label| label.role == role) { @@ -339,8 +340,8 @@ impl SliderRaw { FillLayout::new(new_widget.view_ref().cloned()).with_margin(wrap_view_margin), ); } else { + // Remove a label if there's one for `role` if let Some(i) = labels.iter().position(|label| label.role == role) { - // Remove the label let label = labels.swap_remove(i); debug_assert_eq!(label.role, role); labels_wrapper.set_subview(role, None); diff --git a/tcw3/src/ui/views/split.rs b/tcw3/src/ui/views/split.rs index cdef574b..ad4a8431 100644 --- a/tcw3/src/ui/views/split.rs +++ b/tcw3/src/ui/views/split.rs @@ -432,7 +432,7 @@ impl Layout for SplitLayout { ); // Arrange the panels - let mut frame1 = box2! { top_left: [0.0, 0.0].into(), size: size.into() }; + let mut frame1 = box2! { top_left: [0.0, 0.0].into(), size: size }; let mut frame2 = frame1; let mut spl_frame = frame1; diff --git a/tcw3/src/uicore/keybd.rs b/tcw3/src/uicore/keybd.rs index 0c6e0017..76512352 100644 --- a/tcw3/src/uicore/keybd.rs +++ b/tcw3/src/uicore/keybd.rs @@ -182,10 +182,8 @@ impl HWndRef<'_> { // Does this window recognize the action? let listener = self.wnd.listener.borrow(); let status = listener.validate_action(wm, self, action); - if status.contains(ActionStatus::VALID) { - if perform && status.contains(ActionStatus::ENABLED) { - listener.perform_action(wm, self, action); - } + if perform && status.contains(ActionStatus::VALID | ActionStatus::ENABLED) { + listener.perform_action(wm, self, action); } status }