From c92eece842cfdc8caaf9769a2b801b0b655b4f27 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sat, 8 May 2021 00:06:01 +0200 Subject: [PATCH] Apply clippy suggestions --- clippy.toml | 1 + musicus/src/editors/performance.rs | 2 +- musicus/src/editors/recording.rs | 2 +- musicus/src/editors/work.rs | 16 ++++++---------- musicus/src/import/medium_editor.rs | 2 +- musicus/src/import/medium_preview.rs | 2 +- musicus/src/import/source_selector.rs | 3 +-- musicus/src/import/track_editor.rs | 6 ++---- musicus/src/import/track_selector.rs | 6 ++---- musicus/src/preferences/register.rs | 2 +- musicus/src/preferences/server.rs | 4 ++-- musicus/src/selectors/ensemble.rs | 4 ++-- musicus/src/selectors/instrument.rs | 4 ++-- musicus/src/selectors/person.rs | 4 ++-- musicus/src/selectors/selector.rs | 8 ++++---- musicus/src/widgets/indexed_list_model.rs | 19 ++++++++++--------- musicus/src/widgets/list.rs | 2 +- musicus/src/widgets/player_bar.rs | 10 +++++----- musicus/src/widgets/upload_section.rs | 2 +- 19 files changed, 46 insertions(+), 53 deletions(-) create mode 100644 clippy.toml diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..2b47d11 --- /dev/null +++ b/clippy.toml @@ -0,0 +1 @@ +type-complexity-threshold = 500 \ No newline at end of file diff --git a/musicus/src/editors/performance.rs b/musicus/src/editors/performance.rs index 05a7c8a..5ff60ab 100644 --- a/musicus/src/editors/performance.rs +++ b/musicus/src/editors/performance.rs @@ -97,7 +97,7 @@ impl Screen, Performance> for PerformanceEditor { spawn!(@clone this, async move { if let Some(person) = push!(this.handle, PersonSelector).await { this.show_person(Some(&person)); - this.person.replace(Some(person.clone())); + this.person.replace(Some(person)); this.show_ensemble(None); this.ensemble.replace(None); } diff --git a/musicus/src/editors/recording.rs b/musicus/src/editors/recording.rs index 0604b35..8c4d881 100644 --- a/musicus/src/editors/recording.rs +++ b/musicus/src/editors/recording.rs @@ -203,7 +203,7 @@ impl RecordingEditor { self.handle .backend .db() - .update_recording(recording.clone().into()) + .update_recording(recording.clone()) .await .unwrap(); diff --git a/musicus/src/editors/work.rs b/musicus/src/editors/work.rs index cea397e..d5a51dd 100644 --- a/musicus/src/editors/work.rs +++ b/musicus/src/editors/work.rs @@ -11,7 +11,6 @@ use gtk_macros::get_widget; use libadwaita::prelude::*; use musicus_backend::db::{generate_id, Instrument, Person, Work, WorkPart, WorkSection}; use std::cell::RefCell; -use std::convert::TryInto; use std::rc::Rc; /// Either a work part or a work section. @@ -86,10 +85,7 @@ impl Screen, Work> for WorkEditor { } for section in work.sections { - structure.insert( - section.before_index.try_into().unwrap(), - PartOrSection::Section(section), - ); + structure.insert(section.before_index, PartOrSection::Section(section)); } (work.id, Some(work.composer), work.instruments, structure) @@ -141,7 +137,7 @@ impl Screen, Work> for WorkEditor { spawn!(@clone this, async move { if let Some(person) = push!(this.handle, PersonSelector).await { this.show_composer(&person); - this.composer.replace(Some(person.to_owned())); + this.composer.replace(Some(person)); } }); })); @@ -179,7 +175,7 @@ impl Screen, Work> for WorkEditor { if let Some(instrument) = push!(this.handle, InstrumentSelector).await { let length = { let mut instruments = this.instruments.borrow_mut(); - instruments.push(instrument.clone()); + instruments.push(instrument); instruments.len() }; @@ -344,8 +340,8 @@ impl WorkEditor { .clone() .expect("Tried to create work without composer!"), instruments: self.instruments.borrow().clone(), - parts: parts, - sections: sections, + parts, + sections, }; let upload = self.upload_switch.state(); @@ -356,7 +352,7 @@ impl WorkEditor { self.handle .backend .db() - .update_work(work.clone().into()) + .update_work(work.clone()) .await .unwrap(); diff --git a/musicus/src/import/medium_editor.rs b/musicus/src/import/medium_editor.rs index 163386f..2199a99 100644 --- a/musicus/src/import/medium_editor.rs +++ b/musicus/src/import/medium_editor.rs @@ -211,7 +211,7 @@ impl MediumEditor { id: generate_id(), name: self.name_entry.text().to_string(), discid: Some(self.session.source_id().to_owned()), - tracks: tracks, + tracks, }; let upload = self.publish_switch.state(); diff --git a/musicus/src/import/medium_preview.rs b/musicus/src/import/medium_preview.rs index 211038d..0954bad 100644 --- a/musicus/src/import/medium_preview.rs +++ b/musicus/src/import/medium_preview.rs @@ -210,7 +210,7 @@ impl MediumPreview { /// Copy the tracks to the music library and add the medium to the database. async fn import(&self) -> Result<()> { let medium = self.medium.borrow(); - let medium = medium.as_ref().ok_or(anyhow!("No medium set!"))?; + let medium = medium.as_ref().ok_or_else(|| anyhow!("No medium set!"))?; // Create a new directory in the music library path for the imported medium. diff --git a/musicus/src/import/source_selector.rs b/musicus/src/import/source_selector.rs index 6b3c97a..f9c473e 100644 --- a/musicus/src/import/source_selector.rs +++ b/musicus/src/import/source_selector.rs @@ -6,7 +6,6 @@ use glib::clone; use gtk::prelude::*; use gtk_macros::get_widget; use musicus_backend::import::ImportSession; -use std::path::PathBuf; use std::rc::Rc; /// A dialog for starting to import music. @@ -63,7 +62,7 @@ impl Screen<(), ()> for SourceSelector { this.widget.set_visible_child_name("loading"); spawn!(@clone this, async move { - match ImportSession::folder(PathBuf::from(path)).await { + match ImportSession::folder(path).await { Ok(session) => { let result = push!(this.handle, ImportScreen, session).await; this.handle.pop(result); diff --git a/musicus/src/import/track_editor.rs b/musicus/src/import/track_editor.rs index 0f0887b..b6a595a 100644 --- a/musicus/src/import/track_editor.rs +++ b/musicus/src/import/track_editor.rs @@ -61,10 +61,8 @@ impl Screen<(Recording, Vec), Vec> for TrackEditor { let mut selection = this.selection.borrow_mut(); if check.is_active() { selection.push(index); - } else { - if let Some(pos) = selection.iter().position(|part| *part == index) { - selection.remove(pos); - } + } else if let Some(pos) = selection.iter().position(|part| *part == index) { + selection.remove(pos); } })); diff --git a/musicus/src/import/track_selector.rs b/musicus/src/import/track_selector.rs index 63bd623..e32481e 100644 --- a/musicus/src/import/track_selector.rs +++ b/musicus/src/import/track_selector.rs @@ -65,10 +65,8 @@ impl Screen, Vec> for TrackSelector { let mut selection = this.selection.borrow_mut(); if check.is_active() { selection.push(index); - } else { - if let Some(pos) = selection.iter().position(|part| *part == index) { - selection.remove(pos); - } + } else if let Some(pos) = selection.iter().position(|part| *part == index) { + selection.remove(pos); } if selection.is_empty() { diff --git a/musicus/src/preferences/register.rs b/musicus/src/preferences/register.rs index 0a81e01..0c4b69b 100644 --- a/musicus/src/preferences/register.rs +++ b/musicus/src/preferences/register.rs @@ -70,7 +70,7 @@ impl Screen<(), LoginData> for RegisterDialog { let captcha_id = this.captcha_id.borrow().clone().unwrap(); let answer = this.captcha_entry.text().to_string(); - let email = if email.len() == 0 { + let email = if email.is_empty() { None } else { Some(email) diff --git a/musicus/src/preferences/server.rs b/musicus/src/preferences/server.rs index 58fd386..c422a0d 100644 --- a/musicus/src/preferences/server.rs +++ b/musicus/src/preferences/server.rs @@ -10,7 +10,7 @@ pub struct ServerDialog { backend: Rc, window: libadwaita::Window, url_entry: gtk::Entry, - selected_cb: RefCell ()>>>, + selected_cb: RefCell>>, } impl ServerDialog { @@ -54,7 +54,7 @@ impl ServerDialog { } /// The closure to call when the server was set. - pub fn set_selected_cb () + 'static>(&self, cb: F) { + pub fn set_selected_cb(&self, cb: F) { self.selected_cb.replace(Some(Box::new(cb))); } diff --git a/musicus/src/selectors/ensemble.rs b/musicus/src/selectors/ensemble.rs index e644c77..e063673 100644 --- a/musicus/src/selectors/ensemble.rs +++ b/musicus/src/selectors/ensemble.rs @@ -41,13 +41,13 @@ impl Screen<(), Ensemble> for EnsembleSelector { this.selector .set_load_online(clone!(@weak this => @default-panic, move || { - let clone = this.clone(); + let clone = this; async move { Ok(clone.handle.backend.cl().get_ensembles().await?) } })); this.selector .set_load_local(clone!(@weak this => @default-panic, move || { - let clone = this.clone(); + let clone = this; async move { clone.handle.backend.db().get_ensembles().await.unwrap() } })); diff --git a/musicus/src/selectors/instrument.rs b/musicus/src/selectors/instrument.rs index 996a89d..cfea819 100644 --- a/musicus/src/selectors/instrument.rs +++ b/musicus/src/selectors/instrument.rs @@ -41,13 +41,13 @@ impl Screen<(), Instrument> for InstrumentSelector { this.selector .set_load_online(clone!(@weak this => @default-panic, move || { - let clone = this.clone(); + let clone = this; async move { Ok(clone.handle.backend.cl().get_instruments().await?) } })); this.selector .set_load_local(clone!(@weak this => @default-panic, move || { - let clone = this.clone(); + let clone = this; async move { clone.handle.backend.db().get_instruments().await.unwrap() } })); diff --git a/musicus/src/selectors/person.rs b/musicus/src/selectors/person.rs index 184e17f..2a04ab4 100644 --- a/musicus/src/selectors/person.rs +++ b/musicus/src/selectors/person.rs @@ -41,13 +41,13 @@ impl Screen<(), Person> for PersonSelector { this.selector .set_load_online(clone!(@weak this => @default-panic, move || { - let clone = this.clone(); + let clone = this; async move { Ok(clone.handle.backend.cl().get_persons().await?) } })); this.selector .set_load_local(clone!(@weak this => @default-panic, move || { - let clone = this.clone(); + let clone = this; async move { clone.handle.backend.db().get_persons().await.unwrap() } })); diff --git a/musicus/src/selectors/selector.rs b/musicus/src/selectors/selector.rs index 9d3c078..07cb268 100644 --- a/musicus/src/selectors/selector.rs +++ b/musicus/src/selectors/selector.rs @@ -20,8 +20,8 @@ pub struct Selector { stack: gtk::Stack, list: Rc, items: RefCell>, - back_cb: RefCell ()>>>, - add_cb: RefCell ()>>>, + back_cb: RefCell>>, + add_cb: RefCell>>, make_widget: RefCell gtk::Widget>>>, load_online: RefCell Box>>>>>>, load_local: RefCell Box>>>>>, @@ -147,12 +147,12 @@ impl Selector { } /// Set the closure to be called when the user wants to go back. - pub fn set_back_cb () + 'static>(&self, cb: F) { + pub fn set_back_cb(&self, cb: F) { self.back_cb.replace(Some(Box::new(cb))); } /// Set the closure to be called when the user wants to add an item. - pub fn set_add_cb () + 'static>(&self, cb: F) { + pub fn set_add_cb(&self, cb: F) { self.add_cb.replace(Some(Box::new(cb))); } diff --git a/musicus/src/widgets/indexed_list_model.rs b/musicus/src/widgets/indexed_list_model.rs index cf952a5..fe576b7 100644 --- a/musicus/src/widgets/indexed_list_model.rs +++ b/musicus/src/widgets/indexed_list_model.rs @@ -5,16 +5,11 @@ use std::cell::Cell; glib::wrapper! { /// A thin list model managing only indices to an external data source. - pub struct IndexedListModel(ObjectSubclass) + pub struct IndexedListModel(ObjectSubclass) @implements gio::ListModel; } impl IndexedListModel { - /// Create a new indexed list model, which will be empty initially. - pub fn new() -> Self { - glib::Object::new(&[]).unwrap() - } - /// Set the length of the list model. pub fn set_length(&self, length: u32) { let old_length = self.property("length").unwrap().get::().unwrap(); @@ -23,7 +18,13 @@ impl IndexedListModel { } } -mod indexed_list_model { +impl Default for IndexedListModel { + fn default() -> Self { + glib::Object::new(&[]).unwrap() + } +} + +mod indexed_list_model_imp { use super::*; #[derive(Debug, Default)] @@ -97,7 +98,7 @@ mod indexed_list_model { glib::wrapper! { /// A simple GObject holding just one integer. - pub struct ItemIndex(ObjectSubclass); + pub struct ItemIndex(ObjectSubclass); } impl ItemIndex { @@ -112,7 +113,7 @@ impl ItemIndex { } } -mod item_index { +mod item_index_imp { use super::*; #[derive(Debug, Default)] diff --git a/musicus/src/widgets/list.rs b/musicus/src/widgets/list.rs index 7550894..841cec0 100644 --- a/musicus/src/widgets/list.rs +++ b/musicus/src/widgets/list.rs @@ -18,7 +18,7 @@ pub struct List { impl List { /// Create a new list. The list will be empty initially. pub fn new() -> Rc { - let model = IndexedListModel::new(); + let model = IndexedListModel::default(); let filter = gtk::CustomFilter::new(|_| true); let filter_model = gtk::FilterListModel::new(Some(&model), Some(&filter)); diff --git a/musicus/src/widgets/player_bar.rs b/musicus/src/widgets/player_bar.rs index 93077fa..e25b4fe 100644 --- a/musicus/src/widgets/player_bar.rs +++ b/musicus/src/widgets/player_bar.rs @@ -17,7 +17,7 @@ pub struct PlayerBar { play_image: gtk::Image, pause_image: gtk::Image, player: Rc>>>, - playlist_cb: Rc ()>>>>, + playlist_cb: Rc>>>, } impl PlayerBar { @@ -37,7 +37,7 @@ impl PlayerBar { get_widget!(builder, gtk::Image, pause_image); let player = Rc::new(RefCell::new(None::>)); - let playlist_cb = Rc::new(RefCell::new(None:: ()>>)); + let playlist_cb = Rc::new(RefCell::new(None::>)); previous_button.connect_clicked(clone!(@strong player => move |_| { if let Some(player) = &*player.borrow() { @@ -74,8 +74,8 @@ impl PlayerBar { duration_label, play_image, pause_image, - player: player, - playlist_cb: playlist_cb, + player, + playlist_cb, } } @@ -164,7 +164,7 @@ impl PlayerBar { } } - pub fn set_playlist_cb () + 'static>(&self, cb: F) { + pub fn set_playlist_cb(&self, cb: F) { self.playlist_cb.replace(Some(Box::new(cb))); } } diff --git a/musicus/src/widgets/upload_section.rs b/musicus/src/widgets/upload_section.rs index 2dc60b6..bfd563a 100644 --- a/musicus/src/widgets/upload_section.rs +++ b/musicus/src/widgets/upload_section.rs @@ -40,7 +40,7 @@ impl UploadSection { let section = Section::new(&gettext("Upload"), &list); let this = Rc::new(Self { - widget: section.widget.clone(), + widget: section.widget, backend, switch, });