diff --git a/.cargo/config.toml b/.cargo/config.toml index 80d1208e..919c6d39 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -9,6 +9,7 @@ rustflags = [ "-Wprivate_in_public", "-Wrust_2018_idioms", "-Wsemicolon_in_expressions_from_macros", + "-Wunreachable_pub", "-Wunused_import_braces", "-Wunused_qualifications", "-Wclippy::branches_sharing_code", diff --git a/crates/ruma-client-api/src/backup/get_backup_info.rs b/crates/ruma-client-api/src/backup/get_backup_info.rs index ad1eb76c..0706a921 100644 --- a/crates/ruma-client-api/src/backup/get_backup_info.rs +++ b/crates/ruma-client-api/src/backup/get_backup_info.rs @@ -2,6 +2,7 @@ //! //! Get information about a specific backup. +#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/112615 pub mod v3 { //! `/v3/` ([spec]) //! diff --git a/crates/ruma-client-api/src/session/get_login_types.rs b/crates/ruma-client-api/src/session/get_login_types.rs index 81b242cb..1f9714ef 100644 --- a/crates/ruma-client-api/src/session/get_login_types.rs +++ b/crates/ruma-client-api/src/session/get_login_types.rs @@ -329,7 +329,7 @@ pub mod v3 { #[derive(Debug, Deserialize, Serialize)] struct Wrapper { - pub flows: Vec, + flows: Vec, } #[test] diff --git a/crates/ruma-client-api/src/state/send_state_event.rs b/crates/ruma-client-api/src/state/send_state_event.rs index 42ac4e78..0644f5f9 100644 --- a/crates/ruma-client-api/src/state/send_state_event.rs +++ b/crates/ruma-client-api/src/state/send_state_event.rs @@ -201,7 +201,7 @@ pub mod v3 { struct RequestQuery { /// Timestamp to use for the `origin_server_ts` of the event. #[serde(rename = "ts", skip_serializing_if = "Option::is_none")] - pub timestamp: Option, + timestamp: Option, } #[cfg(feature = "client")] diff --git a/crates/ruma-common/src/events/enums.rs b/crates/ruma-common/src/events/enums.rs index 6865c234..10c19207 100644 --- a/crates/ruma-common/src/events/enums.rs +++ b/crates/ruma-common/src/events/enums.rs @@ -248,7 +248,7 @@ impl From for AnySyncTimelineEvent { #[derive(Deserialize)] #[allow(clippy::exhaustive_structs)] struct EventDeHelper { - pub state_key: Option, + state_key: Option, } impl<'de> Deserialize<'de> for AnyTimelineEvent { diff --git a/crates/ruma-common/src/events/room.rs b/crates/ruma-common/src/events/room.rs index 436b9484..1c759893 100644 --- a/crates/ruma-common/src/events/room.rs +++ b/crates/ruma-common/src/events/room.rs @@ -56,7 +56,7 @@ impl<'de> Deserialize<'de> for MediaSource { D: serde::Deserializer<'de>, { #[derive(Deserialize)] - pub struct MediaSourceJsonRepr { + struct MediaSourceJsonRepr { url: Option, file: Option>, } diff --git a/crates/ruma-common/src/events/room/message/content_serde.rs b/crates/ruma-common/src/events/room/message/content_serde.rs index 4a05ae73..bdba96ed 100644 --- a/crates/ruma-common/src/events/room/message/content_serde.rs +++ b/crates/ruma-common/src/events/room/message/content_serde.rs @@ -75,6 +75,7 @@ impl<'de> Deserialize<'de> for MessageType { } } +#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/112615 #[cfg(feature = "unstable-msc3488")] pub(in super::super) mod msc3488 { use serde::{Deserialize, Serialize}; diff --git a/crates/ruma-common/src/events/room/message/reply.rs b/crates/ruma-common/src/events/room/message/reply.rs index ba7ca0b6..a99fe743 100644 --- a/crates/ruma-common/src/events/room/message/reply.rs +++ b/crates/ruma-common/src/events/room/message/reply.rs @@ -106,7 +106,7 @@ impl fmt::Display for FormattedOrPlainBody<'_> { /// /// [HTML tags and attributes]: https://spec.matrix.org/latest/client-server-api/#mroommessage-msgtypes /// [rich reply fallbacks]: https://spec.matrix.org/latest/client-server-api/#fallbacks-for-rich-replies -pub fn plain_and_formatted_reply_body( +pub(crate) fn plain_and_formatted_reply_body( body: &str, formatted: Option, original_message: &OriginalRoomMessageEvent, diff --git a/crates/ruma-common/src/events/room/message/sanitize.rs b/crates/ruma-common/src/events/room/message/sanitize.rs index d55e82ca..d2e5c64f 100644 --- a/crates/ruma-common/src/events/room/message/sanitize.rs +++ b/crates/ruma-common/src/events/room/message/sanitize.rs @@ -1,5 +1,7 @@ //! Convenience methods and types to sanitize text messages. +#![allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/112615 + #[cfg(feature = "unstable-sanitize")] mod html_fragment; #[cfg(feature = "unstable-sanitize")] diff --git a/crates/ruma-common/src/events/room/thumbnail_source_serde.rs b/crates/ruma-common/src/events/room/thumbnail_source_serde.rs index 9addbedc..79b405ef 100644 --- a/crates/ruma-common/src/events/room/thumbnail_source_serde.rs +++ b/crates/ruma-common/src/events/room/thumbnail_source_serde.rs @@ -10,7 +10,7 @@ use crate::OwnedMxcUri; use super::{EncryptedFile, MediaSource}; /// Serializes a MediaSource to a thumbnail source. -pub fn serialize(source: &Option, serializer: S) -> Result +pub(crate) fn serialize(source: &Option, serializer: S) -> Result where S: Serializer, { @@ -27,12 +27,12 @@ where } /// Deserializes a thumbnail source to a MediaSource. -pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> +pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { #[derive(Deserialize)] - pub struct ThumbnailSourceJsonRepr { + struct ThumbnailSourceJsonRepr { thumbnail_url: Option, thumbnail_file: Option>, } diff --git a/crates/ruma-common/tests/api/mod.rs b/crates/ruma-common/tests/api/mod.rs index 48cedd2d..385d77c5 100644 --- a/crates/ruma-common/tests/api/mod.rs +++ b/crates/ruma-common/tests/api/mod.rs @@ -1,4 +1,5 @@ #![cfg(feature = "api")] +#![allow(unreachable_pub)] mod conversions; mod header_override; diff --git a/crates/ruma-federation-api/src/serde.rs b/crates/ruma-federation-api/src/serde.rs index 60493bf4..fd060aed 100644 --- a/crates/ruma-federation-api/src/serde.rs +++ b/crates/ruma-federation-api/src/serde.rs @@ -1,4 +1,4 @@ //! Modules for custom serde de/-serialization implementations. -pub mod pdu_process_response; -pub mod v1_pdu; +pub(crate) mod pdu_process_response; +pub(crate) mod v1_pdu; diff --git a/crates/ruma-federation-api/src/serde/pdu_process_response.rs b/crates/ruma-federation-api/src/serde/pdu_process_response.rs index 6e115aa8..677b7215 100644 --- a/crates/ruma-federation-api/src/serde/pdu_process_response.rs +++ b/crates/ruma-federation-api/src/serde/pdu_process_response.rs @@ -13,7 +13,7 @@ struct WrappedError { error: Option, } -pub fn serialize( +pub(crate) fn serialize( response: &BTreeMap>, serializer: S, ) -> Result @@ -34,7 +34,7 @@ where } #[allow(clippy::type_complexity)] -pub fn deserialize<'de, D>( +pub(crate) fn deserialize<'de, D>( deserializer: D, ) -> Result>, D::Error> where diff --git a/crates/ruma-federation-api/src/serde/v1_pdu.rs b/crates/ruma-federation-api/src/serde/v1_pdu.rs index 5454d4f4..9422cca8 100644 --- a/crates/ruma-federation-api/src/serde/v1_pdu.rs +++ b/crates/ruma-federation-api/src/serde/v1_pdu.rs @@ -15,7 +15,7 @@ use serde::{ ser::{Serialize, SerializeSeq, Serializer}, }; -pub fn serialize(val: &T, serializer: S) -> Result +pub(crate) fn serialize(val: &T, serializer: S) -> Result where S: Serializer, T: Serialize, @@ -26,7 +26,7 @@ where seq.end() } -pub fn deserialize<'de, T, D>(deserializer: D) -> Result +pub(crate) fn deserialize<'de, T, D>(deserializer: D) -> Result where D: Deserializer<'de>, T: Deserialize<'de>, diff --git a/crates/ruma-macros/src/api/request.rs b/crates/ruma-macros/src/api/request.rs index f95e8a15..50d4067c 100644 --- a/crates/ruma-macros/src/api/request.rs +++ b/crates/ruma-macros/src/api/request.rs @@ -10,7 +10,7 @@ use super::{ attribute::{DeriveRequestMeta, RequestMeta}, ensure_feature_presence, }; -use crate::util::import_ruma_common; +use crate::util::{import_ruma_common, PrivateField}; mod incoming; mod outgoing; @@ -147,7 +147,8 @@ impl Request { let request_body_struct = self.has_body_fields().then(|| { let serde_attr = self.has_newtype_body().then(|| quote! { #[serde(transparent)] }); - let fields = self.fields.iter().filter_map(RequestField::as_body_field); + let fields = + self.fields.iter().filter_map(RequestField::as_body_field).map(PrivateField); quote! { /// Data in the request body. @@ -162,9 +163,11 @@ impl Request { let request_query_def = if let Some(f) = self.query_map_field() { let field = Field { ident: None, colon_token: None, ..f.clone() }; + let field = PrivateField(&field); Some(quote! { (#field); }) } else if self.has_query_fields() { - let fields = self.fields.iter().filter_map(RequestField::as_query_field); + let fields = + self.fields.iter().filter_map(RequestField::as_query_field).map(PrivateField); Some(quote! { { #(#fields),* } }) } else { None diff --git a/crates/ruma-macros/src/api/response.rs b/crates/ruma-macros/src/api/response.rs index 59556d9c..e581ee8c 100644 --- a/crates/ruma-macros/src/api/response.rs +++ b/crates/ruma-macros/src/api/response.rs @@ -13,7 +13,7 @@ use super::{ attribute::{DeriveResponseMeta, ResponseMeta}, ensure_feature_presence, }; -use crate::util::import_ruma_common; +use crate::util::{import_ruma_common, PrivateField}; mod incoming; mod outgoing; @@ -132,7 +132,8 @@ impl Response { }); let serde_attr = self.has_newtype_body().then(|| quote! { #[serde(transparent)] }); - let fields = self.fields.iter().filter_map(ResponseField::as_body_field); + let fields = + self.fields.iter().filter_map(ResponseField::as_body_field).map(PrivateField); quote! { /// Data in the response body. diff --git a/crates/ruma-macros/src/events/event_content.rs b/crates/ruma-macros/src/events/event_content.rs index 2382da95..f6882756 100644 --- a/crates/ruma-macros/src/events/event_content.rs +++ b/crates/ruma-macros/src/events/event_content.rs @@ -12,7 +12,7 @@ use syn::{ DeriveInput, Field, Ident, LitStr, Meta, Token, Type, }; -use crate::util::m_prefix_name_to_type_name; +use crate::util::{m_prefix_name_to_type_name, PrivateField}; use super::event_parse::{EventKind, EventKindVariation}; @@ -930,9 +930,10 @@ fn generate_event_content_impl<'a>( && matches!(a.parse_args(), Ok(EventFieldMeta::TypeFragment)) }) }) + .map(PrivateField) .collect::>(); let fields_ident_without_type_fragment = - fields_without_type_fragment.iter().filter_map(|f| f.ident.as_ref()); + fields_without_type_fragment.iter().filter_map(|f| f.0.ident.as_ref()); quote! { impl #ruma_common::events::EventContentFromType for #ident { diff --git a/crates/ruma-macros/src/lib.rs b/crates/ruma-macros/src/lib.rs index eb54d995..dbbe5b40 100644 --- a/crates/ruma-macros/src/lib.rs +++ b/crates/ruma-macros/src/lib.rs @@ -5,6 +5,7 @@ //! See the documentation for the individual macros for usage details. #![warn(missing_docs)] +#![allow(unreachable_pub)] // https://github.com/rust-lang/rust-clippy/issues/9029 #![allow(clippy::derive_partial_eq_without_eq)] diff --git a/crates/ruma-macros/src/util.rs b/crates/ruma-macros/src/util.rs index 651208d6..50ebed5d 100644 --- a/crates/ruma-macros/src/util.rs +++ b/crates/ruma-macros/src/util.rs @@ -1,7 +1,7 @@ use proc_macro2::TokenStream; use proc_macro_crate::{crate_name, FoundCrate}; -use quote::{format_ident, quote}; -use syn::{Ident, LitStr}; +use quote::{format_ident, quote, ToTokens}; +use syn::{Field, Ident, LitStr}; pub(crate) fn import_ruma_common() -> TokenStream { if let Ok(FoundCrate::Name(name)) = crate_name("ruma-common") { @@ -55,3 +55,21 @@ pub(crate) fn m_prefix_name_to_type_name(name: &LitStr) -> syn::Result { Ok(Ident::new(&s, span)) } + +/// Wrapper around [`syn::Field`] that emits the field without its visibility, +/// thus making it private. +pub struct PrivateField<'a>(pub &'a Field); + +impl ToTokens for PrivateField<'_> { + fn to_tokens(&self, tokens: &mut TokenStream) { + let Field { attrs, vis: _, mutability, ident, colon_token, ty } = self.0; + assert_eq!(*mutability, syn::FieldMutability::None); + + for attr in attrs { + attr.to_tokens(tokens); + } + ident.to_tokens(tokens); + colon_token.to_tokens(tokens); + ty.to_tokens(tokens); + } +} diff --git a/crates/ruma-push-gateway-api/src/send_event_notification.rs b/crates/ruma-push-gateway-api/src/send_event_notification.rs index a86158a7..57b36afc 100644 --- a/crates/ruma-push-gateway-api/src/send_event_notification.rs +++ b/crates/ruma-push-gateway-api/src/send_event_notification.rs @@ -305,7 +305,7 @@ pub mod v1 { Deserializer, Serializer, }; - pub fn serialize(tweak: &[Tweak], serializer: S) -> Result + pub(super) fn serialize(tweak: &[Tweak], serializer: S) -> Result where S: Serializer, { @@ -364,7 +364,7 @@ pub mod v1 { } } - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> + pub(super) fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { diff --git a/crates/ruma-signatures/src/keys.rs b/crates/ruma-signatures/src/keys.rs index f44f560d..2e1e3d73 100644 --- a/crates/ruma-signatures/src/keys.rs +++ b/crates/ruma-signatures/src/keys.rs @@ -24,7 +24,7 @@ pub trait KeyPair: Sized { fn sign(&self, message: &[u8]) -> Signature; } -pub const ED25519_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.101.112"); +const ED25519_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.101.112"); /// An Ed25519 key pair. pub struct Ed25519KeyPair { diff --git a/crates/ruma-signatures/src/verification.rs b/crates/ruma-signatures/src/verification.rs index 8b8efae9..3e690a98 100644 --- a/crates/ruma-signatures/src/verification.rs +++ b/crates/ruma-signatures/src/verification.rs @@ -5,7 +5,7 @@ use ed25519_dalek::{PublicKey, Verifier as _}; use crate::{Error, ParseError, VerificationError}; /// A digital signature verifier. -pub trait Verifier { +pub(crate) trait Verifier { /// Use a public key to verify a signature against the JSON object that was signed. /// /// # Parameters @@ -23,7 +23,7 @@ pub trait Verifier { /// A verifier for Ed25519 digital signatures. #[derive(Debug, Default)] -pub struct Ed25519Verifier; +pub(crate) struct Ed25519Verifier; impl Verifier for Ed25519Verifier { fn verify_json( diff --git a/crates/ruma-state-res/src/power_levels.rs b/crates/ruma-state-res/src/power_levels.rs index b013fc2f..00506c02 100644 --- a/crates/ruma-state-res/src/power_levels.rs +++ b/crates/ruma-state-res/src/power_levels.rs @@ -16,34 +16,34 @@ use crate::RoomVersion; #[derive(Deserialize)] struct IntRoomPowerLevelsEventContent { #[serde(default = "default_power_level")] - pub ban: Int, + ban: Int, #[serde(default)] - pub events: BTreeMap, + events: BTreeMap, #[serde(default)] - pub events_default: Int, + events_default: Int, #[serde(default)] - pub invite: Int, + invite: Int, #[serde(default = "default_power_level")] - pub kick: Int, + kick: Int, #[serde(default = "default_power_level")] - pub redact: Int, + redact: Int, #[serde(default = "default_power_level")] - pub state_default: Int, + state_default: Int, #[serde(default)] - pub users: BTreeMap, + users: BTreeMap, #[serde(default)] - pub users_default: Int, + users_default: Int, #[serde(default)] - pub notifications: IntNotificationPowerLevels, + notifications: IntNotificationPowerLevels, } impl From for RoomPowerLevelsEventContent { @@ -80,7 +80,7 @@ impl From for RoomPowerLevelsEventContent { #[derive(Deserialize)] struct IntNotificationPowerLevels { #[serde(default = "default_power_level")] - pub room: Int, + room: Int, } impl Default for IntNotificationPowerLevels { diff --git a/crates/ruma-state-res/src/test_utils.rs b/crates/ruma-state-res/src/test_utils.rs index cf907f5f..527d6edd 100644 --- a/crates/ruma-state-res/src/test_utils.rs +++ b/crates/ruma-state-res/src/test_utils.rs @@ -29,11 +29,11 @@ use tracing::info; use crate::{auth_types_for_event, Error, Event, EventTypeExt, Result, StateMap}; -pub use event::PduEvent; +pub(crate) use event::PduEvent; static SERVER_TIMESTAMP: AtomicU64 = AtomicU64::new(0); -pub fn do_check( +pub(crate) fn do_check( events: &[Arc], edges: Vec>, expected_state_ids: Vec, @@ -206,10 +206,10 @@ pub fn do_check( } #[allow(clippy::exhaustive_structs)] -pub struct TestStore(pub HashMap>); +pub(crate) struct TestStore(pub(crate) HashMap>); impl TestStore { - pub fn get_event(&self, _: &RoomId, event_id: &EventId) -> Result> { + pub(crate) fn get_event(&self, _: &RoomId, event_id: &EventId) -> Result> { self.0 .get(event_id) .map(Arc::clone) @@ -217,7 +217,7 @@ impl TestStore { } /// Returns a Vec of the related auth events to the given `event`. - pub fn auth_event_ids( + pub(crate) fn auth_event_ids( &self, room_id: &RoomId, event_ids: Vec, @@ -245,7 +245,7 @@ impl TestStore { // A StateStore implementation for testing #[allow(clippy::type_complexity)] impl TestStore { - pub fn set_up( + pub(crate) fn set_up( &mut self, ) -> (StateMap, StateMap, StateMap) { let create_event = to_pdu_event::<&EventId>( @@ -331,7 +331,7 @@ impl TestStore { } } -pub fn event_id(id: &str) -> OwnedEventId { +pub(crate) fn event_id(id: &str) -> OwnedEventId { if id.contains('$') { return id.try_into().unwrap(); } @@ -339,39 +339,39 @@ pub fn event_id(id: &str) -> OwnedEventId { format!("${id}:foo").try_into().unwrap() } -pub fn alice() -> &'static UserId { +pub(crate) fn alice() -> &'static UserId { user_id!("@alice:foo") } -pub fn bob() -> &'static UserId { +pub(crate) fn bob() -> &'static UserId { user_id!("@bob:foo") } -pub fn charlie() -> &'static UserId { +pub(crate) fn charlie() -> &'static UserId { user_id!("@charlie:foo") } -pub fn ella() -> &'static UserId { +pub(crate) fn ella() -> &'static UserId { user_id!("@ella:foo") } -pub fn zara() -> &'static UserId { +pub(crate) fn zara() -> &'static UserId { user_id!("@zara:foo") } -pub fn room_id() -> &'static RoomId { +pub(crate) fn room_id() -> &'static RoomId { room_id!("!test:foo") } -pub fn member_content_ban() -> Box { +pub(crate) fn member_content_ban() -> Box { to_raw_json_value(&RoomMemberEventContent::new(MembershipState::Ban)).unwrap() } -pub fn member_content_join() -> Box { +pub(crate) fn member_content_join() -> Box { to_raw_json_value(&RoomMemberEventContent::new(MembershipState::Join)).unwrap() } -pub fn to_init_pdu_event( +pub(crate) fn to_init_pdu_event( id: &str, sender: &UserId, ev_type: TimelineEventType, @@ -402,7 +402,7 @@ pub fn to_init_pdu_event( }) } -pub fn to_pdu_event( +pub(crate) fn to_pdu_event( id: &str, sender: &UserId, ev_type: TimelineEventType, @@ -442,7 +442,7 @@ where // all graphs start with these input events #[allow(non_snake_case)] -pub fn INITIAL_EVENTS() -> HashMap> { +pub(crate) fn INITIAL_EVENTS() -> HashMap> { vec![ to_pdu_event::<&EventId>( "CREATE", @@ -524,7 +524,7 @@ pub fn INITIAL_EVENTS() -> HashMap> { // all graphs start with these input events #[allow(non_snake_case)] -pub fn INITIAL_EVENTS_CREATE_ROOM() -> HashMap> { +pub(crate) fn INITIAL_EVENTS_CREATE_ROOM() -> HashMap> { vec![to_pdu_event::<&EventId>( "CREATE", alice(), @@ -540,14 +540,14 @@ pub fn INITIAL_EVENTS_CREATE_ROOM() -> HashMap> { } #[allow(non_snake_case)] -pub fn INITIAL_EDGES() -> Vec { +pub(crate) fn INITIAL_EDGES() -> Vec { vec!["START", "IMC", "IMB", "IJR", "IPOWER", "IMA", "CREATE"] .into_iter() .map(event_id) .collect::>() } -pub mod event { +pub(crate) mod event { use ruma_common::{ events::{pdu::Pdu, TimelineEventType}, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, UserId, @@ -648,9 +648,9 @@ pub mod event { #[derive(Clone, Debug, Deserialize, Serialize)] #[allow(clippy::exhaustive_structs)] - pub struct PduEvent { - pub event_id: OwnedEventId, + pub(crate) struct PduEvent { + pub(crate) event_id: OwnedEventId, #[serde(flatten)] - pub rest: Pdu, + pub(crate) rest: Pdu, } } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 531384f3..d21a847f 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -3,6 +3,7 @@ //! This binary is integrated into the `cargo` command line by using an alias in //! `.cargo/config`. Run commands as `cargo xtask [command]`. +#![allow(unreachable_pub)] #![allow(clippy::exhaustive_structs)] // https://github.com/rust-lang/rust-clippy/issues/9029 #![allow(clippy::derive_partial_eq_without_eq)]