From af68646258fa6e5cba5907602a45339e989e3bb2 Mon Sep 17 00:00:00 2001 From: Tomoaki Kawada Date: Thu, 25 Jul 2024 12:39:38 +0900 Subject: [PATCH] Fix CI (#3679) * refactor(yew-macro): ignore `dead_code` for `Lint::lint` if not `cfg(yew_lints)` `dead_code` has become more precise in recent toolchains. * refactor(yew): stop using `static mut` hack to get static reference to an empty `Vec` The old code triggered `static_mut_refs` lint in the latest stable toolchain. * refactor(yew): ignore `clippy::to_string_trait_impl` for `impl ToString for Classes` * perf(yew,yew-router): use `const {}` syntax for `thread_local!` Addresses `clippy::thread_local_initializer_can_be_made_const`. May provide performance benefits. * refactor(yew): remove `yew::html::component::lifecycle::Stateful::as_any_mut` Addresses `dead_code` lint. * refactor(yew): ignore `clippy::incompatible_msrv` for `TopologicalQueue::pop_topmost` if Rust version >= 1.66 The use of `BTreeMap::pop_first` (stabilized in 1.66) is already gated by `#[rustversion(since(1.66))]` hence we can ignore this warning. * refactor(yew): gate `yew::html::component::lifecycle::Stateful::{rendered,props_changed}` by `cfg(feature = "csr")` Addresses `dead_code` lint. * test(website-test): ignore `clippy::needless_doctest_main` * doc(examples): replace `clone` + assignment with `clone_from` Addresses `clippy::assigning_clones` lint. --- examples/todomvc/src/main.rs | 2 +- packages/yew-macro/src/html_tree/lint/mod.rs | 1 + packages/yew-router/src/utils.rs | 2 +- packages/yew/src/html/classes.rs | 1 + packages/yew/src/html/component/lifecycle.rs | 9 ++++----- packages/yew/src/renderer.rs | 2 +- packages/yew/src/scheduler.rs | 1 + packages/yew/src/virtual_dom/vlist.rs | 7 +++---- tools/website-test/src/lib.rs | 1 + 9 files changed, 14 insertions(+), 12 deletions(-) diff --git a/examples/todomvc/src/main.rs b/examples/todomvc/src/main.rs index 202480529..4cd1aed36 100644 --- a/examples/todomvc/src/main.rs +++ b/examples/todomvc/src/main.rs @@ -73,7 +73,7 @@ impl Component for App { .filter(|e| self.state.filter.fits(e)) .nth(idx) .unwrap(); - self.state.edit_value = entry.description.clone(); + self.state.edit_value.clone_from(&entry.description); self.state.clear_all_edit(); self.state.toggle_edit(idx); } diff --git a/packages/yew-macro/src/html_tree/lint/mod.rs b/packages/yew-macro/src/html_tree/lint/mod.rs index bbb34f246..82b0c351f 100644 --- a/packages/yew-macro/src/html_tree/lint/mod.rs +++ b/packages/yew-macro/src/html_tree/lint/mod.rs @@ -12,6 +12,7 @@ use crate::props::{ElementProps, Prop}; /// use `proc-macro-error` (and the `emit_warning!` macro) to produce a warning. At present, these /// are only emitted on nightly. pub trait Lint { + #[cfg_attr(not(yew_lints), allow(dead_code))] fn lint(element: &HtmlElement); } diff --git a/packages/yew-router/src/utils.rs b/packages/yew-router/src/utils.rs index ba3567017..588e7d5ba 100644 --- a/packages/yew-router/src/utils.rs +++ b/packages/yew-router/src/utils.rs @@ -8,7 +8,7 @@ pub(crate) fn strip_slash_suffix(path: &str) -> &str { static BASE_URL_LOADED: std::sync::Once = std::sync::Once::new(); thread_local! { - static BASE_URL: RefCell> = RefCell::new(None); + static BASE_URL: RefCell> = const { RefCell::new(None) }; } // This exists so we can cache the base url. It costs us a `to_string` call instead of a DOM API diff --git a/packages/yew/src/html/classes.rs b/packages/yew/src/html/classes.rs index 941ec617b..d2305a129 100644 --- a/packages/yew/src/html/classes.rs +++ b/packages/yew/src/html/classes.rs @@ -165,6 +165,7 @@ impl IntoIterator for &Classes { } } +#[allow(clippy::to_string_trait_impl)] impl ToString for Classes { fn to_string(&self) -> String { let mut iter = self.set.iter().cloned(); diff --git a/packages/yew/src/html/component/lifecycle.rs b/packages/yew/src/html/component/lifecycle.rs index c86e4349e..5bf523ee0 100644 --- a/packages/yew/src/html/component/lifecycle.rs +++ b/packages/yew/src/html/component/lifecycle.rs @@ -148,16 +148,17 @@ where /// methods. pub(crate) trait Stateful { fn view(&self) -> HtmlResult; + #[cfg(feature = "csr")] fn rendered(&mut self, first_render: bool); fn destroy(&mut self); fn any_scope(&self) -> AnyScope; fn flush_messages(&mut self) -> bool; + #[cfg(feature = "csr")] fn props_changed(&mut self, props: Rc) -> bool; fn as_any(&self) -> &dyn Any; - fn as_any_mut(&mut self) -> &mut dyn Any; #[cfg(feature = "hydration")] fn creation_mode(&self) -> RenderMode; @@ -171,6 +172,7 @@ where self.component.view(&self.context) } + #[cfg(feature = "csr")] fn rendered(&mut self, first_render: bool) { self.component.rendered(&self.context, first_render) } @@ -199,6 +201,7 @@ where }) } + #[cfg(feature = "csr")] fn props_changed(&mut self, props: Rc) -> bool { let props = match Rc::downcast::(props) { Ok(m) => m, @@ -216,10 +219,6 @@ where fn as_any(&self) -> &dyn Any { self } - - fn as_any_mut(&mut self) -> &mut dyn Any { - self - } } pub(crate) struct ComponentState { diff --git a/packages/yew/src/renderer.rs b/packages/yew/src/renderer.rs index 442268a28..7c7e20e63 100644 --- a/packages/yew/src/renderer.rs +++ b/packages/yew/src/renderer.rs @@ -8,7 +8,7 @@ use crate::app_handle::AppHandle; use crate::html::BaseComponent; thread_local! { - static PANIC_HOOK_IS_SET: Cell = Cell::new(false); + static PANIC_HOOK_IS_SET: Cell = const { Cell::new(false) }; } /// Set a custom panic hook. diff --git a/packages/yew/src/scheduler.rs b/packages/yew/src/scheduler.rs index 6f9113bf3..31bd57938 100644 --- a/packages/yew/src/scheduler.rs +++ b/packages/yew/src/scheduler.rs @@ -55,6 +55,7 @@ impl TopologicalQueue { /// Take a single entry, preferring parents over children #[rustversion::since(1.66)] + #[allow(clippy::incompatible_msrv)] #[inline] fn pop_topmost(&mut self) -> Option { self.inner.pop_first().map(|(_, v)| v) diff --git a/packages/yew/src/virtual_dom/vlist.rs b/packages/yew/src/virtual_dom/vlist.rs index 62dc5b14e..b5d34f947 100644 --- a/packages/yew/src/virtual_dom/vlist.rs +++ b/packages/yew/src/virtual_dom/vlist.rs @@ -45,10 +45,9 @@ impl Deref for VList { match self.children { Some(ref m) => m, None => { - // This is mutable because the Vec is not Sync - static mut EMPTY: Vec = Vec::new(); - // SAFETY: The EMPTY value is always read-only - unsafe { &EMPTY } + // This can be replaced with `const { &Vec::new() }` in Rust 1.79. + const EMPTY: &Vec = &Vec::new(); + EMPTY } } } diff --git a/tools/website-test/src/lib.rs b/tools/website-test/src/lib.rs index 310f42032..f814b3148 100644 --- a/tools/website-test/src/lib.rs +++ b/tools/website-test/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(clippy::needless_doctest_main)] pub mod tutorial; include!(concat!(env!("OUT_DIR"), "/website_tests.rs"));