From 4cae2c93cb36d19d1757a497ccc1dd72f9cd082e Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 29 Dec 2022 03:33:34 +0400 Subject: [PATCH] Use new `format!` syntax where possible (fix clippy) --- crates/teloxide-core/src/bot.rs | 2 +- crates/teloxide-core/src/codegen/patch.rs | 16 ++++++++-------- crates/teloxide-core/src/codegen/schema.rs | 4 ++-- crates/teloxide-core/src/local_macros.rs | 4 ++-- crates/teloxide-core/src/net.rs | 6 ++---- crates/teloxide-core/src/requests/requester.rs | 4 ++-- .../teloxide-core/src/serde_multipart/error.rs | 8 ++++---- .../src/serde_multipart/serializers.rs | 8 ++++---- crates/teloxide-core/src/types/chat_id.rs | 2 +- .../src/types/inline_keyboard_markup.rs | 2 +- crates/teloxide-core/src/types/recipient.rs | 4 ++-- crates/teloxide-core/src/types/user_id.rs | 2 +- .../dialogue/storage/sqlite_storage.rs | 2 +- .../dialogue/storage/trace_storage.rs | 2 +- crates/teloxide/src/utils/command.rs | 14 ++++++-------- crates/teloxide/src/utils/html.rs | 10 +++++----- crates/teloxide/src/utils/markdown.rs | 12 ++++++------ 17 files changed, 49 insertions(+), 53 deletions(-) diff --git a/crates/teloxide-core/src/bot.rs b/crates/teloxide-core/src/bot.rs index b8f9d67e..deea3566 100644 --- a/crates/teloxide-core/src/bot.rs +++ b/crates/teloxide-core/src/bot.rs @@ -293,5 +293,5 @@ impl Bot { } fn get_env(env: &'static str) -> String { - std::env::var(env).unwrap_or_else(|_| panic!("Cannot get the {} env variable", env)) + std::env::var(env).unwrap_or_else(|_| panic!("Cannot get the {env} env variable")) } diff --git a/crates/teloxide-core/src/codegen/patch.rs b/crates/teloxide-core/src/codegen/patch.rs index 60ebc820..deb8037c 100644 --- a/crates/teloxide-core/src/codegen/patch.rs +++ b/crates/teloxide-core/src/codegen/patch.rs @@ -188,7 +188,7 @@ impl Doc { link.clear(); *link += *value; } else if key.is_exact() { - panic!("Patch error: {:?} doesn't have link {}", key, name); + panic!("Patch error: {key:?} doesn't have link {name}"); } } Patch::AddLink { name, value } => { @@ -228,7 +228,7 @@ fn intra_links(doc: &mut Doc) { } _ => { repls_t.push(k.clone()); - *v = format!("crate::types::{}", k); + *v = format!("crate::types::{k}"); } } } @@ -236,23 +236,23 @@ fn intra_links(doc: &mut Doc) { for repl in repls_t { if let Some(value) = doc.md_links.remove(repl.as_str()) { - doc.md = doc.md.replace(format!("[{}]", repl).as_str(), &format!("[`{}`]", repl)); - doc.md_links.insert(format!("`{}`", repl), value); + doc.md = doc.md.replace(format!("[{repl}]").as_str(), &format!("[`{repl}`]")); + doc.md_links.insert(format!("`{repl}`"), value); } } for repl in repls_m { if let Some(value) = doc.md_links.remove(repl.as_str()) { let repln = to_uppercase(&repl); - doc.md = doc.md.replace(format!("[{}]", repl).as_str(), &format!("[`{}`]", repln)); - doc.md_links.insert(format!("`{}`", repln), value); + doc.md = doc.md.replace(format!("[{repl}]").as_str(), &format!("[`{repln}`]")); + doc.md_links.insert(format!("`{repln}`"), value); } } } fn escape_kw(s: &mut String) { if ["type"].contains(&s.as_str()) { - *s = format!("{}_", s); + *s = format!("{s}_"); } } @@ -309,7 +309,7 @@ fn patch_types(schema: &mut Schema, from: Type, to: Type, list: &[(&str, &str)]) .find(|p| p.name == param) .expect("Couldn't find parameter for patching"); - assert_eq!(p.ty, from, "{}::{}", method, param); + assert_eq!(p.ty, from, "{method}::{param}"); p.ty = to.clone(); } } diff --git a/crates/teloxide-core/src/codegen/schema.rs b/crates/teloxide-core/src/codegen/schema.rs index 6f3eaa14..1c3794b8 100644 --- a/crates/teloxide-core/src/codegen/schema.rs +++ b/crates/teloxide-core/src/codegen/schema.rs @@ -97,8 +97,8 @@ impl std::fmt::Display for Type { Type::f64 => write!(f, "f64"), Type::bool => write!(f, "bool"), Type::String => write!(f, "String"), - Type::Option(inner) => write!(f, "Option<{}>", inner), - Type::ArrayOf(inner) => write!(f, "Vec<{}>", inner), + Type::Option(inner) => write!(f, "Option<{inner}>"), + Type::ArrayOf(inner) => write!(f, "Vec<{inner}>"), Type::RawTy(raw) => f.write_str(raw), Type::Url => write!(f, "Url"), Type::DateTime => write!(f, "DateTime"), diff --git a/crates/teloxide-core/src/local_macros.rs b/crates/teloxide-core/src/local_macros.rs index de69f36d..d3e30e02 100644 --- a/crates/teloxide-core/src/local_macros.rs +++ b/crates/teloxide-core/src/local_macros.rs @@ -1305,12 +1305,12 @@ fn codegen_requester_forward() { .join(",\n "); let generics = - if generics.is_empty() { String::from("") } else { format!("<{}>", generics) }; + if generics.is_empty() { String::from("") } else { format!("<{generics}>") }; let where_clause = if where_clause.is_empty() { String::from("") } else { - format!(" where {}", where_clause) + format!(" where {where_clause}") }; format!( diff --git a/crates/teloxide-core/src/net.rs b/crates/teloxide-core/src/net.rs index 5bd12342..1d686df6 100644 --- a/crates/teloxide-core/src/net.rs +++ b/crates/teloxide-core/src/net.rs @@ -80,16 +80,14 @@ pub fn default_reqwest_settings() -> reqwest::ClientBuilder { /// /// [Telegram documentation]: https://core.telegram.org/bots/api#making-requests fn method_url(base: reqwest::Url, token: &str, method_name: &str) -> reqwest::Url { - base.join(&format!("/bot{token}/{method}", token = token, method = method_name)) - .expect("failed to format url") + base.join(&format!("/bot{token}/{method_name}")).expect("failed to format url") } /// Creates URL for downloading a file. See the [Telegram documentation]. /// /// [Telegram documentation]: https://core.telegram.org/bots/api#file fn file_url(base: reqwest::Url, token: &str, file_path: &str) -> reqwest::Url { - base.join(&format!("file/bot{token}/{file}", token = token, file = file_path)) - .expect("failed to format url") + base.join(&format!("file/bot{token}/{file_path}")).expect("failed to format url") } #[cfg(test)] diff --git a/crates/teloxide-core/src/requests/requester.rs b/crates/teloxide-core/src/requests/requester.rs index f4a29663..f3f6397f 100644 --- a/crates/teloxide-core/src/requests/requester.rs +++ b/crates/teloxide-core/src/requests/requester.rs @@ -1294,12 +1294,12 @@ fn codegen_requester_methods() { .join(",\n "); let generics = - if generics.is_empty() { String::from("") } else { format!("<{}>", generics) }; + if generics.is_empty() { String::from("") } else { format!("<{generics}>") }; let where_clause = if where_clause.is_empty() { String::from("") } else { - format!(" where {}", where_clause) + format!(" where {where_clause}") }; format!( diff --git a/crates/teloxide-core/src/serde_multipart/error.rs b/crates/teloxide-core/src/serde_multipart/error.rs index 427f2da9..9602a541 100644 --- a/crates/teloxide-core/src/serde_multipart/error.rs +++ b/crates/teloxide-core/src/serde_multipart/error.rs @@ -25,11 +25,11 @@ impl ser::Error for Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::Custom(s) => write!(f, "Custom serde error: {}", s), + Self::Custom(s) => write!(f, "Custom serde error: {s}"), Self::TopLevelNotStruct => write!(f, "Multipart supports only structs at top level"), - Self::Fmt(inner) => write!(f, "Formatting error: {}", inner), - Self::Io(inner) => write!(f, "Io error: {}", inner), - Self::Json(inner) => write!(f, "Json (de)serialization error: {}", inner), + Self::Fmt(inner) => write!(f, "Formatting error: {inner}"), + Self::Io(inner) => write!(f, "Io error: {inner}"), + Self::Json(inner) => write!(f, "Json (de)serialization error: {inner}"), } } } diff --git a/crates/teloxide-core/src/serde_multipart/serializers.rs b/crates/teloxide-core/src/serde_multipart/serializers.rs index 0d0f33be..d92b919b 100644 --- a/crates/teloxide-core/src/serde_multipart/serializers.rs +++ b/crates/teloxide-core/src/serde_multipart/serializers.rs @@ -472,9 +472,9 @@ impl SerializeStruct for JsonPartSerializer { Empty => { self.state = Rest; - write!(&mut self.buf, "{{\"{}\":{}", key, value)? + write!(&mut self.buf, "{{\"{key}\":{value}")? } - Rest => write!(&mut self.buf, ",\"{}\":{}", key, value)?, + Rest => write!(&mut self.buf, ",\"{key}\":{value}")?, } Ok(()) @@ -511,9 +511,9 @@ impl SerializeSeq for JsonPartSerializer { Empty => { self.state = Rest; - write!(&mut self.buf, "[{}", value)? + write!(&mut self.buf, "[{value}")? } - Rest => write!(&mut self.buf, ",{}", value)?, + Rest => write!(&mut self.buf, ",{value}")?, } Ok(()) diff --git a/crates/teloxide-core/src/types/chat_id.rs b/crates/teloxide-core/src/types/chat_id.rs index 21adf49b..cccbc4a3 100644 --- a/crates/teloxide-core/src/types/chat_id.rs +++ b/crates/teloxide-core/src/types/chat_id.rs @@ -62,7 +62,7 @@ impl ChatId { Channel((MAX_MARKED_CHANNEL_ID - id) as _) } id @ MIN_USER_ID..=MAX_USER_ID => User(UserId(id as _)), - id => panic!("malformed chat id: {}", id), + id => panic!("malformed chat id: {id}"), } } } diff --git a/crates/teloxide-core/src/types/inline_keyboard_markup.rs b/crates/teloxide-core/src/types/inline_keyboard_markup.rs index 8b7747d3..db92ae4b 100644 --- a/crates/teloxide-core/src/types/inline_keyboard_markup.rs +++ b/crates/teloxide-core/src/types/inline_keyboard_markup.rs @@ -77,7 +77,7 @@ mod tests { use super::*; fn url(n: u32) -> reqwest::Url { - reqwest::Url::parse(&format!("https://example.com/{n}", n = n)).unwrap() + reqwest::Url::parse(&format!("https://example.com/{n}")).unwrap() } #[test] diff --git a/crates/teloxide-core/src/types/recipient.rs b/crates/teloxide-core/src/types/recipient.rs index 469aac97..8818f559 100644 --- a/crates/teloxide-core/src/types/recipient.rs +++ b/crates/teloxide-core/src/types/recipient.rs @@ -11,11 +11,11 @@ use crate::types::{ChatId, UserId}; #[serde(untagged)] pub enum Recipient { /// A chat identifier. - #[display(fmt = "{}", _0)] + #[display(fmt = "{_0}")] Id(ChatId), /// A channel username (in the format @channelusername). - #[display(fmt = "{}", _0)] + #[display(fmt = "{_0}")] ChannelUsername(String), } diff --git a/crates/teloxide-core/src/types/user_id.rs b/crates/teloxide-core/src/types/user_id.rs index 71d41984..57c95d13 100644 --- a/crates/teloxide-core/src/types/user_id.rs +++ b/crates/teloxide-core/src/types/user_id.rs @@ -13,7 +13,7 @@ impl UserId { /// `tg://user/?id=<...>`. #[must_use] pub fn url(self) -> reqwest::Url { - reqwest::Url::parse(&format!("tg://user/?id={}", self)).unwrap() + reqwest::Url::parse(&format!("tg://user/?id={self}")).unwrap() } /// Returns `true` if this is the id of the special user used by telegram diff --git a/crates/teloxide/src/dispatching/dialogue/storage/sqlite_storage.rs b/crates/teloxide/src/dispatching/dialogue/storage/sqlite_storage.rs index 982fefc1..32425b24 100644 --- a/crates/teloxide/src/dispatching/dialogue/storage/sqlite_storage.rs +++ b/crates/teloxide/src/dispatching/dialogue/storage/sqlite_storage.rs @@ -39,7 +39,7 @@ impl SqliteStorage { path: &str, serializer: S, ) -> Result, SqliteStorageError> { - let pool = SqlitePool::connect(format!("sqlite:{}?mode=rwc", path).as_str()).await?; + let pool = SqlitePool::connect(format!("sqlite:{path}?mode=rwc").as_str()).await?; let mut conn = pool.acquire().await?; sqlx::query( r#" diff --git a/crates/teloxide/src/dispatching/dialogue/storage/trace_storage.rs b/crates/teloxide/src/dispatching/dialogue/storage/trace_storage.rs index 0fe61065..ef3d06e1 100644 --- a/crates/teloxide/src/dispatching/dialogue/storage/trace_storage.rs +++ b/crates/teloxide/src/dispatching/dialogue/storage/trace_storage.rs @@ -56,7 +56,7 @@ where D: Send + 'static, { Box::pin(async move { - let to = format!("{:#?}", dialogue); + let to = format!("{dialogue:#?}"); >::update_dialogue(self.inner.clone(), chat_id, dialogue).await?; log::trace!("Updated a dialogue #{}: {:#?}", chat_id, to); Ok(()) diff --git a/crates/teloxide/src/utils/command.rs b/crates/teloxide/src/utils/command.rs index 95ef0000..2626d28c 100644 --- a/crates/teloxide/src/utils/command.rs +++ b/crates/teloxide/src/utils/command.rs @@ -429,18 +429,16 @@ impl Display for ParseError { match self { ParseError::TooFewArguments { expected, found, message } => write!( f, - "Too few arguments (expected {}, found {}, message = '{}')", - expected, found, message + "Too few arguments (expected {expected}, found {found}, message = '{message}')" ), ParseError::TooManyArguments { expected, found, message } => write!( f, - "Too many arguments (expected {}, found {}, message = '{}')", - expected, found, message + "Too many arguments (expected {expected}, found {found}, message = '{message}')" ), - ParseError::IncorrectFormat(e) => write!(f, "Incorrect format of command args: {}", e), - ParseError::UnknownCommand(e) => write!(f, "Unknown command: {}", e), - ParseError::WrongBotName(n) => write!(f, "Wrong bot name: {}", n), - ParseError::Custom(e) => write!(f, "{}", e), + ParseError::IncorrectFormat(e) => write!(f, "Incorrect format of command args: {e}"), + ParseError::UnknownCommand(e) => write!(f, "Unknown command: {e}"), + ParseError::WrongBotName(n) => write!(f, "Wrong bot name: {n}"), + ParseError::Custom(e) => write!(f, "{e}"), } } } diff --git a/crates/teloxide/src/utils/html.rs b/crates/teloxide/src/utils/html.rs index d2243d5d..70c4dabf 100644 --- a/crates/teloxide/src/utils/html.rs +++ b/crates/teloxide/src/utils/html.rs @@ -11,7 +11,7 @@ use teloxide_core::types::User; #[must_use = "This function returns a new string, rather than mutating the argument, so calling it \ without using its output does nothing useful"] pub fn bold(s: &str) -> String { - format!("{}", s) + format!("{s}") } /// Applies the italic font style to the string. @@ -21,7 +21,7 @@ pub fn bold(s: &str) -> String { #[must_use = "This function returns a new string, rather than mutating the argument, so calling it \ without using its output does nothing useful"] pub fn italic(s: &str) -> String { - format!("{}", s) + format!("{s}") } /// Applies the underline font style to the string. @@ -31,7 +31,7 @@ pub fn italic(s: &str) -> String { #[must_use = "This function returns a new string, rather than mutating the argument, so calling it \ without using its output does nothing useful"] pub fn underline(s: &str) -> String { - format!("{}", s) + format!("{s}") } /// Applies the strikethrough font style to the string. @@ -41,7 +41,7 @@ pub fn underline(s: &str) -> String { #[must_use = "This function returns a new string, rather than mutating the argument, so calling it \ without using its output does nothing useful"] pub fn strike(s: &str) -> String { - format!("{}", s) + format!("{s}") } /// Builds an inline link with an anchor. @@ -57,7 +57,7 @@ pub fn link(url: &str, text: &str) -> String { #[must_use = "This function returns a new string, rather than mutating the argument, so calling it \ without using its output does nothing useful"] pub fn user_mention(user_id: i64, text: &str) -> String { - link(format!("tg://user?id={}", user_id).as_str(), text) + link(format!("tg://user?id={user_id}").as_str(), text) } /// Formats the code block. diff --git a/crates/teloxide/src/utils/markdown.rs b/crates/teloxide/src/utils/markdown.rs index 976e0ab4..f181536a 100644 --- a/crates/teloxide/src/utils/markdown.rs +++ b/crates/teloxide/src/utils/markdown.rs @@ -11,7 +11,7 @@ use teloxide_core::types::User; #[must_use = "This function returns a new string, rather than mutating the argument, so calling it \ without using its output does nothing useful"] pub fn bold(s: &str) -> String { - format!("*{}*", s) + format!("*{s}*") } /// Applies the italic font style to the string. @@ -25,7 +25,7 @@ pub fn italic(s: &str) -> String { if s.starts_with("__") && s.ends_with("__") { format!(r"_{}\r__", &s[..s.len() - 1]) } else { - format!("_{}_", s) + format!("_{s}_") } } @@ -43,9 +43,9 @@ pub fn underline(s: &str) -> String { // ___italic underline_\r__, where \r is a character with code 13, which // will be ignored. if s.starts_with('_') && s.ends_with('_') { - format!(r"__{}\r__", s) + format!(r"__{s}\r__") } else { - format!("__{}__", s) + format!("__{s}__") } } @@ -56,7 +56,7 @@ pub fn underline(s: &str) -> String { #[must_use = "This function returns a new string, rather than mutating the argument, so calling it \ without using its output does nothing useful"] pub fn strike(s: &str) -> String { - format!("~{}~", s) + format!("~{s}~") } /// Builds an inline link with an anchor. @@ -72,7 +72,7 @@ pub fn link(url: &str, text: &str) -> String { #[must_use = "This function returns a new string, rather than mutating the argument, so calling it \ without using its output does nothing useful"] pub fn user_mention(user_id: i64, text: &str) -> String { - link(format!("tg://user?id={}", user_id).as_str(), text) + link(format!("tg://user?id={user_id}").as_str(), text) } /// Formats the code block.