From 785118c6d92b49ea12e9c0a1918ff51be8b57d97 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 18 Jul 2023 20:04:13 +0200 Subject: [PATCH] fix various clippy warnings to improve the code quality --- src/api/image.rs | 6 +++--- src/api/instances.rs | 3 +-- src/api/mod.rs | 8 ++++---- src/api/post.rs | 2 +- src/components/comment_row.rs | 6 +++--- src/components/community_page.rs | 15 ++++++--------- src/components/community_row.rs | 2 +- src/components/inbox_page.rs | 12 ++++-------- src/components/instance_row.rs | 2 +- src/components/mention_row.rs | 6 +++--- src/components/moderates_row.rs | 2 +- src/components/post_page.rs | 8 ++++---- src/components/post_row.rs | 6 +++--- src/components/voting_row.rs | 16 +++++----------- src/dialogs/editor.rs | 2 +- src/main.rs | 3 +-- src/settings.rs | 10 +++++----- src/util.rs | 6 +++--- 18 files changed, 50 insertions(+), 65 deletions(-) diff --git a/src/api/image.rs b/src/api/image.rs index e301aac..cd7f840 100644 --- a/src/api/image.rs +++ b/src/api/image.rs @@ -31,16 +31,16 @@ pub fn upload_image(image: std::path::PathBuf) -> Result let part = Part::bytes(data) .file_name(file_name) - .mime_str(&mime_type.unwrap().essence_str())?; + .mime_str(mime_type.unwrap().essence_str())?; let form = reqwest::blocking::multipart::Form::new().part("images[]", part); let account = settings::get_current_account(); let base_url = account.instance_url; let path = format!("{}/pictrs/image", base_url); let res: UploadImageResponse = CLIENT - .post(&path) + .post(path) .header( "cookie", - format!("jwt={}", account.jwt.unwrap().to_string()), + format!("jwt={}", account.jwt.unwrap().into_inner()), ) .multipart(form) .send()? diff --git a/src/api/instances.rs b/src/api/instances.rs index 07d7df2..f317101 100644 --- a/src/api/instances.rs +++ b/src/api/instances.rs @@ -25,8 +25,7 @@ pub fn fetch_instances(query_filter: &str) -> std::result::Result, .filter(|instance| { instance.software == Some("lemmy".to_owned()) && instance.domain.clone().contains(&lowercase_query_filter) - }) - .map(|instance| instance.clone()) + }).cloned() .collect::>()), None => Ok(vec![]), } diff --git a/src/api/mod.rs b/src/api/mod.rs index 7c616ee..d276b57 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -30,7 +30,7 @@ pub static CLIENT: Lazy = Lazy::new(|| { }); fn get_api_url() -> String { - format!("{}/api/{}", get_current_account().instance_url, API_VERSION).to_string() + format!("{}/api/{}", get_current_account().instance_url, API_VERSION) } fn get_url(path: &str) -> String { @@ -42,7 +42,7 @@ where T: DeserializeOwned, Params: Serialize + std::fmt::Debug, { - CLIENT.get(&get_url(path)).query(¶ms).send()?.json() + CLIENT.get(get_url(path)).query(¶ms).send()?.json() } fn post(path: &str, params: &Params) -> Result @@ -50,7 +50,7 @@ where T: DeserializeOwned, Params: Serialize + std::fmt::Debug, { - CLIENT.post(&get_url(path)).json(¶ms).send()?.json() + CLIENT.post(get_url(path)).json(¶ms).send()?.json() } fn put(path: &str, params: &Params) -> Result @@ -58,5 +58,5 @@ where T: DeserializeOwned, Params: Serialize + std::fmt::Debug, { - CLIENT.put(&get_url(path)).json(¶ms).send()?.json() + CLIENT.put(get_url(path)).json(¶ms).send()?.json() } diff --git a/src/api/post.rs b/src/api/post.rs index 847f6c1..4270835 100644 --- a/src/api/post.rs +++ b/src/api/post.rs @@ -43,7 +43,7 @@ pub fn get_comments(post_id: PostId) -> Result, reqwest::Error> let mut grouped_comments: Vec = vec![]; for (_, comments_group) in &comments .iter() - .group_by(|a| a.comment.path.split(".").collect::>()[1].to_owned()) + .group_by(|a| a.comment.path.split('.').collect::>()[1].to_owned()) { let mut group = comments_group.collect::>(); group.sort_by(|a, b| a.comment.path.partial_cmp(&b.comment.path).unwrap()); diff --git a/src/components/comment_row.rs b/src/components/comment_row.rs index a68e197..d25c552 100644 --- a/src/components/comment_row.rs +++ b/src/components/comment_row.rs @@ -168,7 +168,7 @@ impl FactoryComponent for CommentRow { fn update(&mut self, message: Self::Input, sender: FactorySender) { match message { CommentRowMsg::OpenPerson => { - sender.output(crate::AppMsg::OpenPerson(self.comment.creator.id.clone())); + sender.output(crate::AppMsg::OpenPerson(self.comment.creator.id)); } CommentRowMsg::DeleteComment => { let comment_id = self.comment.comment.id; @@ -201,7 +201,7 @@ impl FactoryComponent for CommentRow { let message = match api::comment::edit_comment(data.body, id) { Ok(comment) => Some(CommentRowMsg::UpdateComment(comment.comment_view)), Err(err) => { - println!("{}", err.to_string()); + println!("{}", err); None } }; @@ -219,7 +219,7 @@ impl FactoryComponent for CommentRow { // TODO sender.output_sender().emit(PostPageInput::CreatedComment(comment.comment_view)); } Err(err) => { - println!("{}", err.to_string()); + println!("{}", err); } }; }); diff --git a/src/components/community_page.rs b/src/components/community_page.rs index 4b5402d..3f8a2fb 100644 --- a/src/components/community_page.rs +++ b/src/components/community_page.rs @@ -203,12 +203,12 @@ impl SimpleComponent for CommunityPage { self.posts.guard().push_front(post); } CommunityInput::CreatePostRequest(post) => { - let id = self.info.community.id.0.clone(); + let id = self.info.community.id.0; std::thread::spawn(move || { let message = match api::post::create_post(post.name, post.body, post.url, id) { Ok(post) => Some(CommunityInput::CreatedPost(post.post_view)), Err(err) => { - println!("{}", err.to_string()); + println!("{}", err); None } }; @@ -219,22 +219,19 @@ impl SimpleComponent for CommunityPage { } CommunityInput::ToggleSubscription => { let community_id = self.info.community.id.0; - let new_state = match self.info.subscribed { - SubscribedType::NotSubscribed => true, - _ => false, - }; + let new_state = matches!(self.info.subscribed, SubscribedType::NotSubscribed); std::thread::spawn(move || { let message = match api::community::follow_community(community_id, new_state) { Ok(community) => Some(CommunityInput::UpdateSubscriptionState( community.community_view.subscribed, )), Err(err) => { - println!("{}", err.to_string()); + println!("{}", err); None } }; - if message.is_some() { - sender.input(message.unwrap()) + if let Some(message) = message { + sender.input(message) }; }); } diff --git a/src/components/community_row.rs b/src/components/community_row.rs index 61b5540..92c3ab8 100644 --- a/src/components/community_row.rs +++ b/src/components/community_row.rs @@ -96,7 +96,7 @@ impl FactoryComponent for CommunityRow { fn update(&mut self, message: Self::Input, sender: FactorySender) { match message { CommunityRowMsg::OpenCommunity => sender.output(crate::AppMsg::OpenCommunity( - self.community.community.id.clone(), + self.community.community.id, )), } } diff --git a/src/components/inbox_page.rs b/src/components/inbox_page.rs index ecd2924..b72d76d 100644 --- a/src/components/inbox_page.rs +++ b/src/components/inbox_page.rs @@ -126,8 +126,8 @@ impl SimpleComponent for InboxPage { match message { InboxInput::FetchInbox => { let type_ = self.type_.clone(); - let page = self.page.clone(); - let unread_only = self.unread_only.clone(); + let page = self.page; + let unread_only = self.unread_only; std::thread::spawn(move || { let message = match type_ { InboxType::Mentions => { @@ -135,11 +135,7 @@ impl SimpleComponent for InboxPage { // It's just a different object, but its contents are exactly the same let serialised = serde_json::to_string(&response.mentions).unwrap(); let mentions = serde_json::from_str(&serialised).ok(); - if let Some(mentions) = mentions { - Some(InboxInput::UpdateInbox(mentions)) - } else { - None - } + mentions.map(InboxInput::UpdateInbox) } else { None } @@ -187,7 +183,7 @@ impl SimpleComponent for InboxPage { } } InboxInput::MarkAllAsRead => { - let show_unread_only = self.unread_only.clone(); + let show_unread_only = self.unread_only; std::thread::spawn(move || { if api::user::mark_all_as_read().is_ok() && show_unread_only { sender.input(InboxInput::UpdateInbox(vec![])); diff --git a/src/components/instance_row.rs b/src/components/instance_row.rs index 1be4c86..e81ca6e 100644 --- a/src/components/instance_row.rs +++ b/src/components/instance_row.rs @@ -79,7 +79,7 @@ impl FactoryComponent for InstanceRow { match message { InstanceRowMsg::OpenInstance => { let instance_address = format!("https://{}", self.instance.domain); - sender.output(InstancesPageInput::SetInstance(instance_address.clone())) + sender.output(InstancesPageInput::SetInstance(instance_address)) } } } diff --git a/src/components/mention_row.rs b/src/components/mention_row.rs index 2bc419b..73ca660 100644 --- a/src/components/mention_row.rs +++ b/src/components/mention_row.rs @@ -154,14 +154,14 @@ impl FactoryComponent for MentionRow { fn update(&mut self, message: Self::Input, sender: FactorySender) { match message { MentionRowMsg::OpenPerson => { - sender.output(crate::AppMsg::OpenPerson(self.comment.creator.id.clone())); + sender.output(crate::AppMsg::OpenPerson(self.comment.creator.id)); } MentionRowMsg::OpenPost => { - sender.output(crate::AppMsg::OpenPost(self.comment.post.id.clone())); + sender.output(crate::AppMsg::OpenPost(self.comment.post.id)); } MentionRowMsg::OpenCommunity => { sender.output(crate::AppMsg::OpenCommunity( - self.comment.community.id.clone(), + self.comment.community.id, )); } } diff --git a/src/components/moderates_row.rs b/src/components/moderates_row.rs index f24ff88..8a46fff 100644 --- a/src/components/moderates_row.rs +++ b/src/components/moderates_row.rs @@ -107,7 +107,7 @@ impl FactoryComponent for ModeratesRow { fn update(&mut self, message: Self::Input, sender: FactorySender) { match message { ModeratesRowMsg::OpenCommunity => sender.output(crate::AppMsg::OpenCommunity( - self.community.community.id.clone(), + self.community.community.id, )), } } diff --git a/src/components/post_page.rs b/src/components/post_page.rs index fc7173d..5d8cb71 100644 --- a/src/components/post_page.rs +++ b/src/components/post_page.rs @@ -273,13 +273,13 @@ impl SimpleComponent for PostPage { } } PostPageInput::OpenPerson => { - let person_id = self.info.post_view.creator.id.clone(); + let person_id = self.info.post_view.creator.id; sender .output_sender() .emit(crate::AppMsg::OpenPerson(person_id)); } PostPageInput::OpenCommunity => { - let community_id = self.info.community_view.community.id.clone(); + let community_id = self.info.community_view.community.id; sender .output_sender() .emit(crate::AppMsg::OpenCommunity(community_id)); @@ -314,7 +314,7 @@ impl SimpleComponent for PostPage { let message = match api::comment::create_comment(id, post.body, None) { Ok(comment) => Some(PostPageInput::CreatedComment(comment.comment_view)), Err(err) => { - println!("{}", err.to_string()); + println!("{}", err); None } }; @@ -368,7 +368,7 @@ impl SimpleComponent for PostPage { let message = match api::post::edit_post(post.name, post.url, post.body, id) { Ok(post) => Some(PostPageInput::DoneEditPost(post.post_view)), Err(err) => { - println!("{}", err.to_string()); + println!("{}", err); None } }; diff --git a/src/components/post_row.rs b/src/components/post_row.rs index f27208f..c6c0e5f 100644 --- a/src/components/post_row.rs +++ b/src/components/post_row.rs @@ -189,13 +189,13 @@ impl FactoryComponent for PostRow { fn update(&mut self, message: Self::Input, sender: FactorySender) { match message { PostRowMsg::OpenCommunity => { - sender.output(crate::AppMsg::OpenCommunity(self.post.community.id.clone())) + sender.output(crate::AppMsg::OpenCommunity(self.post.community.id)) } PostRowMsg::OpenPerson => { - sender.output(crate::AppMsg::OpenPerson(self.post.creator.id.clone())) + sender.output(crate::AppMsg::OpenPerson(self.post.creator.id)) } PostRowMsg::OpenPost => { - sender.output(crate::AppMsg::OpenPost(self.post.post.id.clone())) + sender.output(crate::AppMsg::OpenPost(self.post.post.id)) } PostRowMsg::ToggleSaved => { let post_id = self.post.post.id; diff --git a/src/components/voting_row.rs b/src/components/voting_row.rs index 9a2a791..b7f745e 100644 --- a/src/components/voting_row.rs +++ b/src/components/voting_row.rs @@ -106,9 +106,7 @@ impl SimpleComponent for VotingRowModel { match message { VotingRowInput::Vote(vote) => { let mut score = self.stats.own_vote.unwrap_or(0) + vote; - if score < -1 || score > 1 { - score = 0 - }; + if !(-1..=1).contains(&score) { score = 0 }; if settings::get_current_account().jwt.is_none() { return; } @@ -116,9 +114,7 @@ impl SimpleComponent for VotingRowModel { std::thread::spawn(move || { let info = if stats.post_id.is_some() { let response = api::post::like_post( - PostId { - 0: stats.post_id.unwrap(), - }, + PostId(stats.post_id.unwrap()), score, ); match response { @@ -127,15 +123,13 @@ impl SimpleComponent for VotingRowModel { post.post_view.my_vote, )), Err(err) => { - println!("{}", err.to_string()); + println!("{}", err); None } } } else { let response = api::comment::like_comment( - CommentId { - 0: stats.comment_id.unwrap(), - }, + CommentId(stats.comment_id.unwrap()), score, ); match response { @@ -144,7 +138,7 @@ impl SimpleComponent for VotingRowModel { comment.comment_view.my_vote, )), Err(err) => { - println!("{}", err.to_string()); + println!("{}", err); None } } diff --git a/src/dialogs/editor.rs b/src/dialogs/editor.rs index c46675c..53a0f20 100644 --- a/src/dialogs/editor.rs +++ b/src/dialogs/editor.rs @@ -207,7 +207,7 @@ impl SimpleComponent for EditorDialog { if let Some(url) = data.url { self.url_buffer.set_text(url.to_string()); } - self.body_buffer.set_text(&data.body.clone()); + self.body_buffer.set_text(&data.body); } DialogMsg::ChooseImage => { let buttons = [ diff --git a/src/main.rs b/src/main.rs index 00c08a2..c1239da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -333,9 +333,8 @@ impl SimpleComponent for App { profile_sender.input(AppMsg::OpenPerson(PersonId(person.id))); } }); - let login_sender = sender.clone(); let login_action: RelmAction = RelmAction::new_stateless(move |_| { - login_sender.input(AppMsg::UpdateState(AppState::Login)); + sender.input(AppMsg::UpdateState(AppState::Login)); }); let about_action = { let sender = model.about_dialog.sender().clone(); diff --git a/src/settings.rs b/src/settings.rs index 48363a8..ed28c52 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -35,16 +35,16 @@ pub fn get_prefs() -> Preferences { if let Ok(file) = File::open(data_path()) { // Deserialize data from file to vector let prefs: Result = serde_json::from_reader(file); - if prefs.is_ok() { - return prefs.unwrap(); + if let Ok(prefs) = prefs { + return prefs; } } - return Preferences::default(); + Preferences::default() } pub fn get_current_account() -> Account { let mut prefs = get_prefs(); - if prefs.accounts.len() == 0 { + if prefs.accounts.is_empty() { prefs = create_account(true); } prefs.accounts[prefs.current_account_index as usize].clone() @@ -66,7 +66,7 @@ pub fn remove_account(index: usize) { settings.accounts.remove(index); // if the deleted account has been before the current one, the current index needs to decreased too if index < settings.current_account_index as usize { - settings.current_account_index = settings.current_account_index - 1; + settings.current_account_index -= 1; } save_prefs(&settings); } diff --git a/src/util.rs b/src/util.rs index 74d4376..e856817 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,11 +2,11 @@ use lemmy_api_common::lemmy_db_schema::newtypes::DbUrl; use relm4_components::web_image::WebImageMsg; pub fn get_web_image_msg(url: Option) -> WebImageMsg { - return if let Some(url) = url { + if let Some(url) = url { WebImageMsg::LoadImage(url.to_string()) } else { WebImageMsg::Unload - }; + } } pub fn get_web_image_url(url: Option) -> String { @@ -18,7 +18,7 @@ pub fn get_web_image_url(url: Option) -> String { } pub fn markdown_to_pango_markup(text: String) -> String { - return html2pango::markup_html(&markdown::to_html(&text)).unwrap_or(text); + html2pango::markup_html(&markdown::to_html(&text)).unwrap_or(text) } pub fn format_elapsed_time(time: chrono::NaiveDateTime) -> String {