From b8c45110a33e03505ac1e8b485e501a33bad7290 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 21 Mar 2024 14:42:45 +0100 Subject: [PATCH] Fix assorted warnings --- crates/teloxide-core/src/adaptors/cache_me.rs | 2 +- .../teloxide-core/src/adaptors/parse_mode.rs | 2 +- crates/teloxide-core/src/lib.rs | 29 ++++++++++++++----- .../src/net/telegram_response.rs | 2 +- crates/teloxide-core/src/requests.rs | 1 - .../teloxide-core/src/requests/requester.rs | 6 +--- crates/teloxide-core/src/requests/utils.rs | 16 ---------- crates/teloxide-core/src/types.rs | 15 +++------- crates/teloxide-core/src/types/animation.rs | 2 -- crates/teloxide-core/src/types/audio.rs | 2 -- crates/teloxide-core/src/types/chat_member.rs | 2 +- crates/teloxide-core/src/types/input_file.rs | 11 ++----- crates/teloxide-core/src/types/message.rs | 2 +- .../teloxide-core/src/types/message_entity.rs | 1 - crates/teloxide-core/src/types/parse_mode.rs | 7 ++--- .../src/types/response_parameters.rs | 1 - crates/teloxide-core/src/types/seconds.rs | 5 +++- crates/teloxide-core/src/types/sticker.rs | 2 +- crates/teloxide-core/src/types/update.rs | 7 ++--- crates/teloxide-macros/src/unzip.rs | 2 -- crates/teloxide/examples/admin.rs | 8 +++-- .../teloxide/examples/chat_member_updates.rs | 7 +---- .../teloxide/examples/dispatching_features.rs | 6 +--- crates/teloxide/examples/inline.rs | 1 - .../dialogue/storage/trace_storage.rs | 6 +--- .../teloxide/src/update_listeners/polling.rs | 1 - crates/teloxide/src/utils/html.rs | 2 -- crates/teloxide/src/utils/markdown.rs | 1 - 28 files changed, 50 insertions(+), 99 deletions(-) delete mode 100644 crates/teloxide-core/src/requests/utils.rs diff --git a/crates/teloxide-core/src/adaptors/cache_me.rs b/crates/teloxide-core/src/adaptors/cache_me.rs index e34cb694..97f77a7a 100644 --- a/crates/teloxide-core/src/adaptors/cache_me.rs +++ b/crates/teloxide-core/src/adaptors/cache_me.rs @@ -12,7 +12,7 @@ use url::Url; use crate::{ payloads::GetMe, requests::{HasPayload, Request, Requester}, - types::{Me, Recipient, *}, + types::*, }; /// `get_me` cache. diff --git a/crates/teloxide-core/src/adaptors/parse_mode.rs b/crates/teloxide-core/src/adaptors/parse_mode.rs index c929cfbd..bd10e55d 100644 --- a/crates/teloxide-core/src/adaptors/parse_mode.rs +++ b/crates/teloxide-core/src/adaptors/parse_mode.rs @@ -11,7 +11,7 @@ use crate::{ }, prelude::Requester, requests::{HasPayload, Output, Request}, - types::{InputFile, ParseMode, Recipient, *}, + types::*, }; /// Default parse mode adaptor, see diff --git a/crates/teloxide-core/src/lib.rs b/crates/teloxide-core/src/lib.rs index bbd6e743..f42700aa 100644 --- a/crates/teloxide-core/src/lib.rs +++ b/crates/teloxide-core/src/lib.rs @@ -61,7 +61,7 @@ html_logo_url = "https://cdn.discordapp.com/attachments/224881373326999553/798598120760934410/logo.png", html_favicon_url = "https://cdn.discordapp.com/attachments/224881373326999553/798598120760934410/logo.png" )] -#![forbid(unsafe_code)] +// // we pass "--cfg docsrs" when building docs to add `This is supported on feature="..." only.` // // To properly build docs of this crate run @@ -78,16 +78,29 @@ )] #![cfg_attr(feature = "nightly", feature(type_alias_impl_trait))] #![cfg_attr(all(feature = "full", docsrs), deny(rustdoc::broken_intra_doc_links))] +// +// Lint levels +#![forbid(unsafe_code)] //#![deny(missing_docs)] #![warn(clippy::print_stdout, clippy::dbg_macro)] -#![allow(clippy::let_and_return)] -#![allow(clippy::bool_assert_comparison)] -// Unless this becomes machine applicable, I'm not adding 334 #[must_use]s (waffle) -#![allow(clippy::return_self_not_must_use)] -// Workaround for CI -#![allow(rustdoc::bare_urls)] -// FIXME: deal with these lints #![allow( + // Sometimes it's more readable to assign to a variable and return it immediately + clippy::let_and_return, + + // When you are testing ->bool functions, it makes sense to `assert_eq!(f(..), false)` + clippy::bool_assert_comparison, + + // Unless this becomes machine applicable, I'm not adding 334 #[must_use]s (waffle) + clippy::return_self_not_must_use, + + // This is dumb. `T: ?Sized where T: Trait` IMO makes perfect sense + clippy::multiple_bound_locations, + + // Workaround for CI + // FIXME: do we still need this? + rustdoc::bare_urls, + + // FIXME: deal with these lints clippy::collapsible_str_replace, clippy::borrow_deref_ref, clippy::unnecessary_lazy_evaluations, diff --git a/crates/teloxide-core/src/net/telegram_response.rs b/crates/teloxide-core/src/net/telegram_response.rs index 4dfa5ba1..b75400b2 100644 --- a/crates/teloxide-core/src/net/telegram_response.rs +++ b/crates/teloxide-core/src/net/telegram_response.rs @@ -49,7 +49,7 @@ impl From> for ResponseResult { #[cfg(test)] mod tests { use super::*; - use crate::{errors::ApiError, types::Update}; + use crate::types::Update; #[test] fn parse_terminated_by_other_get_updates() { diff --git a/crates/teloxide-core/src/requests.rs b/crates/teloxide-core/src/requests.rs index 4ed846f3..c1bd7ec9 100644 --- a/crates/teloxide-core/src/requests.rs +++ b/crates/teloxide-core/src/requests.rs @@ -20,4 +20,3 @@ mod payload; mod request; mod requester; mod requester_ext; -mod utils; diff --git a/crates/teloxide-core/src/requests/requester.rs b/crates/teloxide-core/src/requests/requester.rs index 887aca6d..bd211e46 100644 --- a/crates/teloxide-core/src/requests/requester.rs +++ b/crates/teloxide-core/src/requests/requester.rs @@ -3,11 +3,7 @@ use url::Url; -use crate::{ - payloads::{GetMe, SendMessage, *}, - requests::Request, - types::*, -}; +use crate::{payloads::*, requests::Request, types::*}; /// Telegram Bot API client. /// diff --git a/crates/teloxide-core/src/requests/utils.rs b/crates/teloxide-core/src/requests/utils.rs deleted file mode 100644 index 36e7510e..00000000 --- a/crates/teloxide-core/src/requests/utils.rs +++ /dev/null @@ -1,16 +0,0 @@ -use bytes::{Bytes, BytesMut}; -use tokio_util::codec::Decoder; - -struct FileDecoder; - -impl Decoder for FileDecoder { - type Item = Bytes; - type Error = std::io::Error; - - fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { - if src.is_empty() { - return Ok(None); - } - Ok(Some(src.split().freeze())) - } -} diff --git a/crates/teloxide-core/src/types.rs b/crates/teloxide-core/src/types.rs index 89897656..efbb50ac 100644 --- a/crates/teloxide-core/src/types.rs +++ b/crates/teloxide-core/src/types.rs @@ -268,16 +268,12 @@ pub use user_id::*; use serde::Serialize; -/// Converts an `i64` timestump to a `choro::DateTime`, producing serde error -/// for invalid timestumps +/// Converts an `i64` timestamp to a `choro::DateTime`, producing serde error +/// for invalid timestamps pub(crate) fn serde_timestamp( timestamp: i64, ) -> Result, E> { - use chrono::{DateTime, NaiveDateTime, Utc}; - - NaiveDateTime::from_timestamp_opt(timestamp, 0) - .ok_or_else(|| E::custom("invalid timestump")) - .map(|naive| DateTime::from_naive_utc_and_offset(naive, Utc)) + chrono::DateTime::from_timestamp(timestamp, 0).ok_or_else(|| E::custom("invalid timestump")) } pub(crate) mod serde_opt_date_from_unix_timestamp { @@ -313,10 +309,7 @@ pub(crate) mod serde_opt_date_from_unix_timestamp { { let json = r#"{"date":1}"#; - let expected = DateTime::from_naive_utc_and_offset( - chrono::NaiveDateTime::from_timestamp_opt(1, 0).unwrap(), - Utc, - ); + let expected = DateTime::from_timestamp(1, 0).unwrap(); let Struct { date } = serde_json::from_str(json).unwrap(); assert_eq!(date, Some(expected)); diff --git a/crates/teloxide-core/src/types/animation.rs b/crates/teloxide-core/src/types/animation.rs index 183f3b9e..7ac7b206 100644 --- a/crates/teloxide-core/src/types/animation.rs +++ b/crates/teloxide-core/src/types/animation.rs @@ -36,8 +36,6 @@ pub struct Animation { #[cfg(test)] mod tests { - use crate::types::FileMeta; - use super::*; #[test] diff --git a/crates/teloxide-core/src/types/audio.rs b/crates/teloxide-core/src/types/audio.rs index dcdca901..7032111a 100644 --- a/crates/teloxide-core/src/types/audio.rs +++ b/crates/teloxide-core/src/types/audio.rs @@ -36,8 +36,6 @@ pub struct Audio { #[cfg(test)] mod tests { - use crate::types::{FileMeta, Seconds}; - use super::*; #[test] diff --git a/crates/teloxide-core/src/types/chat_member.rs b/crates/teloxide-core/src/types/chat_member.rs index fbf6cecb..715e8d53 100644 --- a/crates/teloxide-core/src/types/chat_member.rs +++ b/crates/teloxide-core/src/types/chat_member.rs @@ -702,7 +702,7 @@ mod tests { can_invite_users: true, can_pin_messages: true, until_date: UntilDate::Date( - chrono::NaiveDateTime::from_timestamp_opt(1620000000, 0).unwrap().and_utc(), + chrono::DateTime::from_timestamp(1620000000, 0).unwrap(), ), }), }; diff --git a/crates/teloxide-core/src/types/input_file.rs b/crates/teloxide-core/src/types/input_file.rs index 77bcf4bc..5a064dac 100644 --- a/crates/teloxide-core/src/types/input_file.rs +++ b/crates/teloxide-core/src/types/input_file.rs @@ -15,15 +15,8 @@ use tokio::{ use tokio_util::codec::{Decoder, FramedRead}; use std::{ - borrow::Cow, - convert::{Infallible, TryFrom}, - fmt, - future::Future, - io, iter, mem, - path::PathBuf, - pin::Pin, - sync::Arc, - task, + borrow::Cow, convert::Infallible, fmt, future::Future, io, iter, mem, path::PathBuf, pin::Pin, + sync::Arc, task, }; use crate::types::InputSticker; diff --git a/crates/teloxide-core/src/types/message.rs b/crates/teloxide-core/src/types/message.rs index 3f46a556..e2de607f 100644 --- a/crates/teloxide-core/src/types/message.rs +++ b/crates/teloxide-core/src/types/message.rs @@ -1748,7 +1748,7 @@ mod tests { Message { id: MessageId(198283), thread_id: None, - date: chrono::NaiveDateTime::from_timestamp_opt(1567927221, 0).unwrap().and_utc(), + date: chrono::DateTime::from_timestamp(1567927221, 0).unwrap(), chat: Chat { id: ChatId(250918540), kind: ChatKind::Private(ChatPrivate { diff --git a/crates/teloxide-core/src/types/message_entity.rs b/crates/teloxide-core/src/types/message_entity.rs index 2dfdd6d1..5882c527 100644 --- a/crates/teloxide-core/src/types/message_entity.rs +++ b/crates/teloxide-core/src/types/message_entity.rs @@ -264,7 +264,6 @@ pub enum MessageEntityKind { mod tests { use super::*; use cool_asserts::assert_matches; - use MessageEntity; use MessageEntityKind::*; #[test] diff --git a/crates/teloxide-core/src/types/parse_mode.rs b/crates/teloxide-core/src/types/parse_mode.rs index d5a07c31..ba79c760 100644 --- a/crates/teloxide-core/src/types/parse_mode.rs +++ b/crates/teloxide-core/src/types/parse_mode.rs @@ -2,10 +2,7 @@ // (for built ins there no warnings, but for (De)Serialize, there are) #![allow(deprecated)] -use std::{ - convert::{TryFrom, TryInto}, - str::FromStr, -}; +use std::str::FromStr; use serde::{Deserialize, Serialize}; @@ -184,8 +181,8 @@ impl FromStr for ParseMode { } #[cfg(test)] +#[allow(deprecated)] mod tests { - #![allow(deprecated)] use super::*; diff --git a/crates/teloxide-core/src/types/response_parameters.rs b/crates/teloxide-core/src/types/response_parameters.rs index 8a68437b..1634e007 100644 --- a/crates/teloxide-core/src/types/response_parameters.rs +++ b/crates/teloxide-core/src/types/response_parameters.rs @@ -20,7 +20,6 @@ pub enum ResponseParameters { #[cfg(test)] mod tests { use super::*; - use crate::types::{ChatId, Seconds}; #[test] fn migrate_to_chat_id_deserialization() { diff --git a/crates/teloxide-core/src/types/seconds.rs b/crates/teloxide-core/src/types/seconds.rs index ab05fbb1..005ecaa7 100644 --- a/crates/teloxide-core/src/types/seconds.rs +++ b/crates/teloxide-core/src/types/seconds.rs @@ -26,8 +26,11 @@ impl Seconds { } /// Returns [`chrono::Duration`] equivalent of this duration. + // FIXME: rename to `time_delta` (the new name of `chrono::Duration`)? pub fn chrono_duration(self) -> chrono::Duration { - chrono::Duration::seconds(self.seconds() as i64) + // Unwrap: `self.seconds()` is a `u32`, which is always between `-i64::MAX/1000` + // and `i64::MAX/1000` + chrono::Duration::try_seconds(self.seconds() as i64).unwrap() } } diff --git a/crates/teloxide-core/src/types/sticker.rs b/crates/teloxide-core/src/types/sticker.rs index 6f41fccc..c687908b 100644 --- a/crates/teloxide-core/src/types/sticker.rs +++ b/crates/teloxide-core/src/types/sticker.rs @@ -1,4 +1,4 @@ -use std::{convert::TryFrom, ops::Deref}; +use std::ops::Deref; use serde::{Deserialize, Serialize}; diff --git a/crates/teloxide-core/src/types/update.rs b/crates/teloxide-core/src/types/update.rs index ec41ad94..caff4c50 100644 --- a/crates/teloxide-core/src/types/update.rs +++ b/crates/teloxide-core/src/types/update.rs @@ -384,16 +384,13 @@ mod test { MessageId, MessageKind, Update, UpdateId, UpdateKind, User, UserId, }; - use chrono::{DateTime, NaiveDateTime, Utc}; + use chrono::DateTime; // TODO: more tests for deserialization #[test] fn message() { let timestamp = 1_569_518_342; - let date = DateTime::from_naive_utc_and_offset( - NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap(), - Utc, - ); + let date = DateTime::from_timestamp(timestamp, 0).unwrap(); let json = r#"{ "update_id":892252934, diff --git a/crates/teloxide-macros/src/unzip.rs b/crates/teloxide-macros/src/unzip.rs index 372ad2e2..936a4876 100644 --- a/crates/teloxide-macros/src/unzip.rs +++ b/crates/teloxide-macros/src/unzip.rs @@ -1,5 +1,3 @@ -use std::iter::FromIterator; - pub(crate) struct Unzip(pub A, pub B); impl FromIterator<(T, U)> for Unzip diff --git a/crates/teloxide/examples/admin.rs b/crates/teloxide/examples/admin.rs index 8612e5de..c54b7b67 100644 --- a/crates/teloxide/examples/admin.rs +++ b/crates/teloxide/examples/admin.rs @@ -129,9 +129,11 @@ async fn mute_user(bot: Bot, msg: Message, time: Duration) -> ResponseResult<()> // Calculates time of user restriction. fn calc_restrict_time(time: u64, unit: UnitOfTime) -> Duration { + // FIXME: actually handle the case of too big integers correctly, instead of + // unwrapping match unit { - UnitOfTime::Hours => Duration::hours(time as i64), - UnitOfTime::Minutes => Duration::minutes(time as i64), - UnitOfTime::Seconds => Duration::seconds(time as i64), + UnitOfTime::Hours => Duration::try_hours(time as i64).unwrap(), + UnitOfTime::Minutes => Duration::try_minutes(time as i64).unwrap(), + UnitOfTime::Seconds => Duration::try_seconds(time as i64).unwrap(), } } diff --git a/crates/teloxide/examples/chat_member_updates.rs b/crates/teloxide/examples/chat_member_updates.rs index 35c3cdf4..b7045ec2 100644 --- a/crates/teloxide/examples/chat_member_updates.rs +++ b/crates/teloxide/examples/chat_member_updates.rs @@ -1,9 +1,4 @@ -use teloxide::{ - dispatching::Dispatcher, - prelude::*, - types::{ChatMemberUpdated, ParseMode, Update}, - utils::html, -}; +use teloxide::{prelude::*, types::ParseMode, utils::html}; use teloxide_core::adaptors::DefaultParseMode; /// We use a type alias to be able to write just `bot: Bot` in handlers, instead diff --git a/crates/teloxide/examples/dispatching_features.rs b/crates/teloxide/examples/dispatching_features.rs index 8e4c52a4..1082c29f 100644 --- a/crates/teloxide/examples/dispatching_features.rs +++ b/crates/teloxide/examples/dispatching_features.rs @@ -3,11 +3,7 @@ use rand::Rng; -use teloxide::{ - prelude::*, - types::{Dice, Update, UserId}, - utils::command::BotCommands, -}; +use teloxide::{prelude::*, types::Dice, utils::command::BotCommands}; #[tokio::main] async fn main() { diff --git a/crates/teloxide/examples/inline.rs b/crates/teloxide/examples/inline.rs index 444e7858..c2c9cd85 100644 --- a/crates/teloxide/examples/inline.rs +++ b/crates/teloxide/examples/inline.rs @@ -3,7 +3,6 @@ use teloxide::{ types::{ InlineQueryResult, InlineQueryResultArticle, InputMessageContent, InputMessageContentText, }, - Bot, }; #[tokio::main] diff --git a/crates/teloxide/src/dispatching/dialogue/storage/trace_storage.rs b/crates/teloxide/src/dispatching/dialogue/storage/trace_storage.rs index ef3d06e1..20a169d0 100644 --- a/crates/teloxide/src/dispatching/dialogue/storage/trace_storage.rs +++ b/crates/teloxide/src/dispatching/dialogue/storage/trace_storage.rs @@ -1,8 +1,4 @@ -use std::{ - fmt::Debug, - marker::{Send, Sync}, - sync::Arc, -}; +use std::{fmt::Debug, sync::Arc}; use futures::future::BoxFuture; use teloxide_core::types::ChatId; diff --git a/crates/teloxide/src/update_listeners/polling.rs b/crates/teloxide/src/update_listeners/polling.rs index be035f90..cf3cb4fd 100644 --- a/crates/teloxide/src/update_listeners/polling.rs +++ b/crates/teloxide/src/update_listeners/polling.rs @@ -1,5 +1,4 @@ use std::{ - convert::TryInto, future::Future, mem, pin::Pin, diff --git a/crates/teloxide/src/utils/html.rs b/crates/teloxide/src/utils/html.rs index e003ab41..37f6f034 100644 --- a/crates/teloxide/src/utils/html.rs +++ b/crates/teloxide/src/utils/html.rs @@ -115,8 +115,6 @@ pub fn user_mention_or_link(user: &User) -> String { #[cfg(test)] mod tests { - use teloxide_core::types::UserId; - use super::*; #[test] diff --git a/crates/teloxide/src/utils/markdown.rs b/crates/teloxide/src/utils/markdown.rs index e48b10c6..4c0fa475 100644 --- a/crates/teloxide/src/utils/markdown.rs +++ b/crates/teloxide/src/utils/markdown.rs @@ -157,7 +157,6 @@ pub fn user_mention_or_link(user: &User) -> String { #[cfg(test)] mod tests { use super::*; - use teloxide_core::types::{User, UserId}; #[test] fn test_bold() {