From 71c48f66c4922813c2dc30b7b875200e06ce4b75 Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Sun, 12 May 2024 15:32:23 -0700 Subject: [PATCH] enable `as_conversions` lint There were some very, uh, creative (and inconsistent) ways to convert between numeric types in here... --- Cargo.toml | 1 + src/api/client_server/backup.rs | 55 +++++++++++++++---------- src/api/client_server/context.rs | 8 ++-- src/api/client_server/directory.rs | 16 +++---- src/api/client_server/message.rs | 7 +++- src/api/client_server/relations.rs | 30 ++++++++------ src/api/client_server/search.rs | 24 +++++++---- src/api/client_server/space.rs | 14 +++++-- src/api/client_server/sync.rs | 36 +++++++++------- src/api/client_server/typing.rs | 3 +- src/api/client_server/user_directory.rs | 2 +- src/api/ruma_wrapper/axum.rs | 4 +- src/api/server_server.rs | 2 +- src/database.rs | 27 +++++++++++- src/database/abstraction/rocksdb.rs | 8 +++- src/database/abstraction/sqlite.rs | 11 +++-- src/service.rs | 15 +++++++ src/service/media.rs | 16 +++---- src/service/rooms/auth_chain.rs | 3 ++ src/service/sending.rs | 2 +- src/utils.rs | 2 + 21 files changed, 195 insertions(+), 91 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e25bc864..26ffd0d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ unused_qualifications = "warn" # Keep alphabetically sorted [workspace.lints.clippy] +as_conversions = "warn" assertions_on_result_states = "warn" cloned_instead_of_copied = "warn" dbg_macro = "warn" diff --git a/src/api/client_server/backup.rs b/src/api/client_server/backup.rs index 0f9c8082..709ed73a 100644 --- a/src/api/client_server/backup.rs +++ b/src/api/client_server/backup.rs @@ -56,7 +56,11 @@ pub(crate) async fn get_latest_backup_info_route( Ok(get_latest_backup_info::v3::Response { algorithm, - count: (services().key_backups.count_keys(sender_user, &version)? as u32).into(), + count: services() + .key_backups + .count_keys(sender_user, &version)? + .try_into() + .expect("count should fit in UInt"), etag: services().key_backups.get_etag(sender_user, &version)?, version, }) @@ -79,10 +83,11 @@ pub(crate) async fn get_backup_info_route( Ok(get_backup_info::v3::Response { algorithm, - count: (services() + count: services() .key_backups - .count_keys(sender_user, &body.version)? as u32) - .into(), + .count_keys(sender_user, &body.version)? + .try_into() + .expect("count should fit in UInt"), etag: services() .key_backups .get_etag(sender_user, &body.version)?, @@ -144,10 +149,11 @@ pub(crate) async fn add_backup_keys_route( } Ok(add_backup_keys::v3::Response { - count: (services() + count: services() .key_backups - .count_keys(sender_user, &body.version)? as u32) - .into(), + .count_keys(sender_user, &body.version)? + .try_into() + .expect("count should fit in UInt"), etag: services() .key_backups .get_etag(sender_user, &body.version)?, @@ -189,10 +195,11 @@ pub(crate) async fn add_backup_keys_for_room_route( } Ok(add_backup_keys_for_room::v3::Response { - count: (services() + count: services() .key_backups - .count_keys(sender_user, &body.version)? as u32) - .into(), + .count_keys(sender_user, &body.version)? + .try_into() + .expect("count should fit in UInt"), etag: services() .key_backups .get_etag(sender_user, &body.version)?, @@ -232,10 +239,11 @@ pub(crate) async fn add_backup_keys_for_session_route( )?; Ok(add_backup_keys_for_session::v3::Response { - count: (services() + count: services() .key_backups - .count_keys(sender_user, &body.version)? as u32) - .into(), + .count_keys(sender_user, &body.version)? + .try_into() + .expect("count should fit in UInt"), etag: services() .key_backups .get_etag(sender_user, &body.version)?, @@ -302,10 +310,11 @@ pub(crate) async fn delete_backup_keys_route( .delete_all_keys(sender_user, &body.version)?; Ok(delete_backup_keys::v3::Response { - count: (services() + count: services() .key_backups - .count_keys(sender_user, &body.version)? as u32) - .into(), + .count_keys(sender_user, &body.version)? + .try_into() + .expect("count should fit in UInt"), etag: services() .key_backups .get_etag(sender_user, &body.version)?, @@ -325,10 +334,11 @@ pub(crate) async fn delete_backup_keys_for_room_route( .delete_room_keys(sender_user, &body.version, &body.room_id)?; Ok(delete_backup_keys_for_room::v3::Response { - count: (services() + count: services() .key_backups - .count_keys(sender_user, &body.version)? as u32) - .into(), + .count_keys(sender_user, &body.version)? + .try_into() + .expect("count should fit in UInt"), etag: services() .key_backups .get_etag(sender_user, &body.version)?, @@ -351,10 +361,11 @@ pub(crate) async fn delete_backup_keys_for_session_route( )?; Ok(delete_backup_keys_for_session::v3::Response { - count: (services() + count: services() .key_backups - .count_keys(sender_user, &body.version)? as u32) - .into(), + .count_keys(sender_user, &body.version)? + .try_into() + .expect("count should fit in UInt"), etag: services() .key_backups .get_etag(sender_user, &body.version)?, diff --git a/src/api/client_server/context.rs b/src/api/client_server/context.rs index 205ecc3a..db83fc8f 100644 --- a/src/api/client_server/context.rs +++ b/src/api/client_server/context.rs @@ -2,6 +2,7 @@ use crate::{services, Error, Result, Ruma}; use ruma::{ api::client::{context::get_context, error::ErrorKind, filter::LazyLoadOptions}, events::StateEventType, + uint, }; use std::collections::HashSet; use tracing::error; @@ -70,7 +71,8 @@ pub(crate) async fn get_context_route( } // Use limit with maximum 100 - let limit = u64::from(body.limit).min(100) as usize; + let half_limit = + usize::try_from(body.limit.min(uint!(100)) / uint!(2)).expect("0-50 should fit in usize"); let base_event = base_event.to_room_event(); @@ -78,7 +80,7 @@ pub(crate) async fn get_context_route( .rooms .timeline .pdus_until(sender_user, &room_id, base_token)? - .take(limit / 2) + .take(half_limit) .filter_map(|r| r.ok()) // Remove buggy events .filter(|(_, pdu)| { services() @@ -115,7 +117,7 @@ pub(crate) async fn get_context_route( .rooms .timeline .pdus_after(sender_user, &room_id, base_token)? - .take(limit / 2) + .take(half_limit) .filter_map(|r| r.ok()) // Remove buggy events .filter(|(_, pdu)| { services() diff --git a/src/api/client_server/directory.rs b/src/api/client_server/directory.rs index feba5ff3..4d926442 100644 --- a/src/api/client_server/directory.rs +++ b/src/api/client_server/directory.rs @@ -24,7 +24,7 @@ use ruma::{ }, StateEventType, }, - ServerName, UInt, + uint, ServerName, UInt, }; use tracing::{error, info, warn}; @@ -157,8 +157,8 @@ pub(crate) async fn get_public_rooms_filtered_helper( }); } - let limit = limit.map_or(10, u64::from); - let mut num_since = 0_u64; + let limit = limit.unwrap_or(uint!(10)); + let mut num_since = UInt::MIN; if let Some(s) = &since { let mut characters = s.chars(); @@ -340,21 +340,21 @@ pub(crate) async fn get_public_rooms_filtered_helper( all_rooms.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members)); - let total_room_count_estimate = (all_rooms.len() as u32).into(); + let total_room_count_estimate = all_rooms.len().try_into().unwrap_or(UInt::MAX); let chunk: Vec<_> = all_rooms .into_iter() - .skip(num_since as usize) - .take(limit as usize) + .skip(num_since.try_into().expect("UInt should fit in usize")) + .take(limit.try_into().expect("UInt should fit in usize")) .collect(); - let prev_batch = if num_since == 0 { + let prev_batch = if num_since == uint!(0) { None } else { Some(format!("p{num_since}")) }; - let next_batch = if chunk.len() < limit as usize { + let next_batch = if chunk.len() < limit.try_into().expect("UInt should fit in usize") { None } else { Some(format!("n{}", num_since + limit)) diff --git a/src/api/client_server/message.rs b/src/api/client_server/message.rs index 36890cab..77a1d81d 100644 --- a/src/api/client_server/message.rs +++ b/src/api/client_server/message.rs @@ -8,6 +8,7 @@ use ruma::{ message::{get_message_events, send_message_event}, }, events::{StateEventType, TimelineEventType}, + uint, }; use std::{ collections::{BTreeMap, HashSet}, @@ -136,7 +137,11 @@ pub(crate) async fn get_message_events_route( .lazy_load_confirm_delivery(sender_user, sender_device, &body.room_id, from) .await?; - let limit = u64::from(body.limit).min(100) as usize; + let limit = body + .limit + .min(uint!(100)) + .try_into() + .expect("0-100 should fit in usize"); let next_token; diff --git a/src/api/client_server/relations.rs b/src/api/client_server/relations.rs index abf3ea70..2bdad29c 100644 --- a/src/api/client_server/relations.rs +++ b/src/api/client_server/relations.rs @@ -1,6 +1,9 @@ -use ruma::api::client::relations::{ - get_relating_events, get_relating_events_with_rel_type, - get_relating_events_with_rel_type_and_event_type, +use ruma::{ + api::client::relations::{ + get_relating_events, get_relating_events_with_rel_type, + get_relating_events_with_rel_type_and_event_type, + }, + uint, }; use crate::{service::rooms::timeline::PduCount, services, Result, Ruma}; @@ -28,9 +31,10 @@ pub(crate) async fn get_relating_events_with_rel_type_and_event_type_route( // Use limit or else 10, with maximum 100 let limit = body .limit - .and_then(|u| u32::try_from(u).ok()) - .map_or(10_usize, |u| u as usize) - .min(100); + .map(|x| x.min(uint!(100))) + .unwrap_or(uint!(10)) + .try_into() + .expect("0-100 should fit in usize"); let res = services() .rooms @@ -78,9 +82,10 @@ pub(crate) async fn get_relating_events_with_rel_type_route( // Use limit or else 10, with maximum 100 let limit = body .limit - .and_then(|u| u32::try_from(u).ok()) - .map_or(10_usize, |u| u as usize) - .min(100); + .map(|x| x.min(uint!(100))) + .unwrap_or(uint!(10)) + .try_into() + .expect("0-100 should fit in usize"); let res = services() .rooms @@ -126,9 +131,10 @@ pub(crate) async fn get_relating_events_route( // Use limit or else 10, with maximum 100 let limit = body .limit - .and_then(|u| u32::try_from(u).ok()) - .map_or(10_usize, |u| u as usize) - .min(100); + .map(|x| x.min(uint!(100))) + .unwrap_or(uint!(10)) + .try_into() + .expect("0-100 should fit in usize"); services() .rooms diff --git a/src/api/client_server/search.rs b/src/api/client_server/search.rs index 1fb70253..eb6902b9 100644 --- a/src/api/client_server/search.rs +++ b/src/api/client_server/search.rs @@ -1,10 +1,13 @@ use crate::{services, Error, Result, Ruma}; -use ruma::api::client::{ - error::ErrorKind, - search::search_events::{ - self, - v3::{EventContextResult, ResultCategories, ResultRoomEvents, SearchResult}, +use ruma::{ + api::client::{ + error::ErrorKind, + search::search_events::{ + self, + v3::{EventContextResult, ResultCategories, ResultRoomEvents, SearchResult}, + }, }, + uint, }; use std::collections::BTreeMap; @@ -32,7 +35,12 @@ pub(crate) async fn search_events_route( }); // Use limit or else 10, with maximum 100 - let limit = filter.limit.map_or(10, u64::from).min(100) as usize; + let limit = filter + .limit + .map(|x| x.min(uint!(100))) + .unwrap_or(uint!(10)) + .try_into() + .expect("0-100 should fit in usize"); let mut searches = Vec::new(); @@ -123,8 +131,8 @@ pub(crate) async fn search_events_route( Ok(search_events::v3::Response::new(ResultCategories { room_events: ResultRoomEvents { - count: Some((results.len() as u32).into()), // TODO: set this to none. Element shouldn't depend on it - groups: BTreeMap::new(), // TODO + count: None, + groups: BTreeMap::new(), // TODO next_batch, results, state: BTreeMap::new(), // TODO diff --git a/src/api/client_server/space.rs b/src/api/client_server/space.rs index ffbd5b8d..a05d5c36 100644 --- a/src/api/client_server/space.rs +++ b/src/api/client_server/space.rs @@ -1,5 +1,5 @@ use crate::{services, Result, Ruma}; -use ruma::api::client::space::get_hierarchy; +use ruma::{api::client::space::get_hierarchy, uint}; /// # `GET /_matrix/client/v1/rooms/{room_id}/hierarchy`` /// @@ -15,9 +15,17 @@ pub(crate) async fn get_hierarchy_route( .and_then(|s| s.parse::().ok()) .unwrap_or(0); - let limit = body.limit.map_or(10, u64::from).min(100) as usize; + let limit = body + .limit + .map(|x| x.min(uint!(100))) + .unwrap_or(uint!(10)) + .try_into() + .expect("0-100 should fit in usize"); - let max_depth = body.max_depth.map_or(3, u64::from).min(10) as usize + 1; // +1 to skip the space room itself + let max_depth = usize::try_from(body.max_depth.map(|x| x.min(uint!(10))).unwrap_or(uint!(3))) + .expect("0-10 should fit in usize") + // Skip the space room itself + + 1; services() .rooms diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs index e10563f4..60f54e4f 100644 --- a/src/api/client_server/sync.rs +++ b/src/api/client_server/sync.rs @@ -1087,8 +1087,8 @@ async fn load_joined_room( }, summary: RoomSummary { heroes, - joined_member_count: joined_member_count.map(|n| (n as u32).into()), - invited_member_count: invited_member_count.map(|n| (n as u32).into()), + joined_member_count: joined_member_count.map(UInt::new_saturating), + invited_member_count: invited_member_count.map(UInt::new_saturating), }, unread_notifications: UnreadNotificationsCount { highlight_count, @@ -1140,7 +1140,7 @@ fn load_timeline( // Take the last events for the timeline timeline_pdus = non_timeline_pdus .by_ref() - .take(limit as usize) + .take(limit.try_into().expect("limit should fit in usize")) .collect::>() .into_iter() .rev() @@ -1427,12 +1427,16 @@ pub(crate) async fn sync_events_v4_route( .ranges .into_iter() .map(|mut r| { - r.0 = - r.0.clamp(uint!(0), UInt::from(all_joined_rooms.len() as u32 - 1)); - r.1 = - r.1.clamp(r.0, UInt::from(all_joined_rooms.len() as u32 - 1)); - let room_ids = all_joined_rooms - [(u64::from(r.0) as usize)..=(u64::from(r.1) as usize)] + r.0 = r.0.clamp( + uint!(0), + UInt::try_from(all_joined_rooms.len() - 1).unwrap_or(UInt::MAX), + ); + r.1 = r.1.clamp( + r.0, + UInt::try_from(all_joined_rooms.len() - 1).unwrap_or(UInt::MAX), + ); + let room_ids = all_joined_rooms[r.0.try_into().unwrap_or(usize::MAX) + ..=r.1.try_into().unwrap_or(usize::MAX)] .to_vec(); new_known_rooms.extend(room_ids.iter().cloned()); for room_id in &room_ids { @@ -1468,7 +1472,7 @@ pub(crate) async fn sync_events_v4_route( } }) .collect(), - count: UInt::from(all_joined_rooms.len() as u32), + count: UInt::try_from(all_joined_rooms.len()).unwrap_or(UInt::MAX), }, ); @@ -1663,20 +1667,20 @@ pub(crate) async fn sync_events_v4_route( prev_batch, limited, joined_count: Some( - (services() + services() .rooms .state_cache .room_joined_count(room_id)? - .unwrap_or(0) as u32) - .into(), + .map(UInt::new_saturating) + .unwrap_or(uint!(0)), ), invited_count: Some( - (services() + services() .rooms .state_cache .room_invited_count(room_id)? - .unwrap_or(0) as u32) - .into(), + .map(UInt::new_saturating) + .unwrap_or(uint!(0)), ), num_live: None, // Count events in timeline greater than global sync counter timestamp: None, diff --git a/src/api/client_server/typing.rs b/src/api/client_server/typing.rs index 7a3dd5c9..d9a24530 100644 --- a/src/api/client_server/typing.rs +++ b/src/api/client_server/typing.rs @@ -30,7 +30,8 @@ pub(crate) async fn create_typing_event_route( .typing_add( sender_user, &body.room_id, - duration.as_millis() as u64 + utils::millis_since_unix_epoch(), + duration.as_millis().try_into().unwrap_or(u64::MAX) + + utils::millis_since_unix_epoch(), ) .await?; } else { diff --git a/src/api/client_server/user_directory.rs b/src/api/client_server/user_directory.rs index 30f7686e..fe8ad179 100644 --- a/src/api/client_server/user_directory.rs +++ b/src/api/client_server/user_directory.rs @@ -17,7 +17,7 @@ pub(crate) async fn search_users_route( body: Ruma, ) -> Result { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); - let limit = u64::from(body.limit) as usize; + let limit = body.limit.try_into().unwrap_or(usize::MAX); let mut users = services().users.iter().filter_map(|user_id| { // Filter out buggy users (they should not exist, but you never know...) diff --git a/src/api/ruma_wrapper/axum.rs b/src/api/ruma_wrapper/axum.rs index 7f5c9b8a..c1ebaa7a 100644 --- a/src/api/ruma_wrapper/axum.rs +++ b/src/api/ruma_wrapper/axum.rs @@ -437,7 +437,9 @@ where }; // With more than 1 buf, we gotta flatten into a Vec first. - let cap = first.remaining() + second.remaining() + body.size_hint().lower() as usize; + let cap = first.remaining() + + second.remaining() + + body.size_hint().lower().try_into().unwrap_or(usize::MAX); let mut vec = Vec::with_capacity(cap); vec.put(first); vec.put(second); diff --git a/src/api/server_server.rs b/src/api/server_server.rs index cc1ebbd3..5bf8101b 100644 --- a/src/api/server_server.rs +++ b/src/api/server_server.rs @@ -1100,7 +1100,7 @@ pub(crate) async fn get_missing_events_route( let mut events = Vec::new(); let mut i = 0; - while i < queued_events.len() && events.len() < u64::from(body.limit) as usize { + while i < queued_events.len() && events.len() < body.limit.try_into().unwrap_or(usize::MAX) { if let Some(pdu) = services().rooms.timeline.get_pdu_json(&queued_events[i])? { let room_id_str = pdu .get("room_id") diff --git a/src/database.rs b/src/database.rs index 0de879d4..8e684079 100644 --- a/src/database.rs +++ b/src/database.rs @@ -345,18 +345,43 @@ impl KeyValueDatabase { .try_into() .expect("pdu cache capacity fits into usize"), )), + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] auth_chain_cache: Mutex::new(LruCache::new( (100_000.0 * config.cache_capacity_modifier) as usize, )), + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] shorteventid_cache: Mutex::new(LruCache::new( (100_000.0 * config.cache_capacity_modifier) as usize, )), + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] eventidshort_cache: Mutex::new(LruCache::new( (100_000.0 * config.cache_capacity_modifier) as usize, )), + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] shortstatekey_cache: Mutex::new(LruCache::new( (100_000.0 * config.cache_capacity_modifier) as usize, )), + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] statekeyshort_cache: Mutex::new(LruCache::new( (100_000.0 * config.cache_capacity_modifier) as usize, )), @@ -979,7 +1004,7 @@ impl KeyValueDatabase { use std::time::{Duration, Instant}; let timer_interval = - Duration::from_secs(services().globals.config.cleanup_second_interval as u64); + Duration::from_secs(u64::from(services().globals.config.cleanup_second_interval)); tokio::spawn(async move { let mut i = interval(timer_interval); diff --git a/src/database/abstraction/rocksdb.rs b/src/database/abstraction/rocksdb.rs index 9fd24e1e..da1763a8 100644 --- a/src/database/abstraction/rocksdb.rs +++ b/src/database/abstraction/rocksdb.rs @@ -32,7 +32,7 @@ fn db_options(max_open_files: i32, rocksdb_cache: &rocksdb::Cache) -> rocksdb::O let mut db_opts = rocksdb::Options::default(); db_opts.set_block_based_table_factory(&block_based_options); db_opts.create_if_missing(true); - db_opts.increase_parallelism(num_cpus::get() as i32); + db_opts.increase_parallelism(num_cpus::get().try_into().unwrap_or(i32::MAX)); db_opts.set_max_open_files(max_open_files); db_opts.set_compression_type(rocksdb::DBCompressionType::Lz4); db_opts.set_bottommost_compression_type(rocksdb::DBCompressionType::Zstd); @@ -58,6 +58,11 @@ fn db_options(max_open_files: i32, rocksdb_cache: &rocksdb::Cache) -> rocksdb::O impl KeyValueDatabaseEngine for Arc { fn open(config: &Config) -> Result { + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] let cache_capacity_bytes = (config.db_cache_capacity_mb * 1024.0 * 1024.0) as usize; let rocksdb_cache = rocksdb::Cache::new_lru_cache(cache_capacity_bytes); @@ -109,6 +114,7 @@ impl KeyValueDatabaseEngine for Arc { Ok(()) } + #[allow(clippy::as_conversions, clippy::cast_precision_loss)] fn memory_usage(&self) -> Result { let stats = rocksdb::perf::get_memory_usage_stats(Some(&[&self.rocks]), Some(&[&self.cache]))?; diff --git a/src/database/abstraction/sqlite.rs b/src/database/abstraction/sqlite.rs index 8e7d3f56..1ec56c59 100644 --- a/src/database/abstraction/sqlite.rs +++ b/src/database/abstraction/sqlite.rs @@ -88,9 +88,14 @@ impl KeyValueDatabaseEngine for Arc { // 1. convert MB to KiB // 2. divide by permanent connections + permanent iter connections + write connection // 3. round down to nearest integer - let cache_size_per_thread: u32 = ((config.db_cache_capacity_mb * 1024.0) - / ((num_cpus::get().max(1) * 2) + 1) as f64) - as u32; + #[allow( + clippy::as_conversions, + clippy::cast_possible_truncation, + clippy::cast_precision_loss, + clippy::cast_sign_loss + )] + let cache_size_per_thread = ((config.db_cache_capacity_mb * 1024.0) + / ((num_cpus::get() as f64 * 2.0) + 1.0)) as u32; let writer = Mutex::new(Engine::prepare_conn(&path, cache_size_per_thread)?); diff --git a/src/service.rs b/src/service.rs index 4ab89c68..fe06c40d 100644 --- a/src/service.rs +++ b/src/service.rs @@ -84,9 +84,19 @@ impl Services { state: rooms::state::Service { db }, state_accessor: rooms::state_accessor::Service { db, + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] server_visibility_cache: StdMutex::new(LruCache::new( (100.0 * config.cache_capacity_modifier) as usize, )), + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] user_visibility_cache: StdMutex::new(LruCache::new( (100.0 * config.cache_capacity_modifier) as usize, )), @@ -94,6 +104,11 @@ impl Services { state_cache: rooms::state_cache::Service { db }, state_compressor: rooms::state_compressor::Service { db, + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] stateinfo_cache: StdMutex::new(LruCache::new( (100.0 * config.cache_capacity_modifier) as usize, )), diff --git a/src/service/media.rs b/src/service/media.rs index 9ecd2668..3dc764e1 100644 --- a/src/service/media.rs +++ b/src/service/media.rs @@ -154,10 +154,8 @@ impl Service { } else { let (exact_width, exact_height) = { // Copied from image::dynimage::resize_dimensions - let ratio = u64::from(original_width) * u64::from(height); - let nratio = u64::from(width) * u64::from(original_height); - - let use_width = nratio <= ratio; + let use_width = (u64::from(width) * u64::from(original_height)) + <= (u64::from(original_width) * u64::from(height)); let intermediate = if use_width { u64::from(original_height) * u64::from(width) / u64::from(original_width) @@ -167,21 +165,23 @@ impl Service { }; if use_width { if intermediate <= u64::from(::std::u32::MAX) { - (width, intermediate as u32) + (width, intermediate.try_into().unwrap_or(u32::MAX)) } else { ( (u64::from(width) * u64::from(::std::u32::MAX) / intermediate) - as u32, + .try_into() + .unwrap_or(u32::MAX), ::std::u32::MAX, ) } } else if intermediate <= u64::from(::std::u32::MAX) { - (intermediate as u32, height) + (intermediate.try_into().unwrap_or(u32::MAX), height) } else { ( ::std::u32::MAX, (u64::from(height) * u64::from(::std::u32::MAX) / intermediate) - as u32, + .try_into() + .unwrap_or(u32::MAX), ) } }; diff --git a/src/service/rooms/auth_chain.rs b/src/service/rooms/auth_chain.rs index e1dc6527..40f49713 100644 --- a/src/service/rooms/auth_chain.rs +++ b/src/service/rooms/auth_chain.rs @@ -44,6 +44,9 @@ impl Service { let mut i = 0; for id in starting_events { let short = services().rooms.short.get_or_create_shorteventid(&id)?; + // I'm afraid to change this in case there is accidental reliance on + // the truncation + #[allow(clippy::as_conversions, clippy::cast_possible_truncation)] let bucket_id = (short % NUM_BUCKETS as u64) as usize; buckets[bucket_id].insert((short, id.clone())); i += 1; diff --git a/src/service/sending.rs b/src/service/sending.rs index f873f5ab..1e6e233e 100644 --- a/src/service/sending.rs +++ b/src/service/sending.rs @@ -107,7 +107,7 @@ impl Service { db, sender, receiver: Mutex::new(receiver), - maximum_requests: Arc::new(Semaphore::new(config.max_concurrent_requests as usize)), + maximum_requests: Arc::new(Semaphore::new(config.max_concurrent_requests.into())), }) } diff --git a/src/utils.rs b/src/utils.rs index b679e6b6..530f1886 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -11,6 +11,8 @@ use std::{ time::{SystemTime, UNIX_EPOCH}, }; +// Hopefully we have a better chat protocol in 530 years +#[allow(clippy::as_conversions, clippy::cast_possible_truncation)] pub(crate) fn millis_since_unix_epoch() -> u64 { SystemTime::now() .duration_since(UNIX_EPOCH)