enable unused_self lint

Functions using `services()` are allowed to pointlessly take `self`
because the existence of `services()` is a crime and the solution is
making the types store references to their dependencies and then going
through `self`, so just allowing the lint saves us from modifying some
code only to switch it back later. Much later. Getting rid of
`services()` will probably be an ordeal.
This commit is contained in:
Charles Hall 2024-05-14 19:40:35 -07:00
parent f855bd09d1
commit e3672eb4e0
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
10 changed files with 39 additions and 30 deletions

View file

@ -91,6 +91,7 @@ unnested_or_patterns = "warn"
unreadable_literal = "warn"
unseparated_literal_suffix = "warn"
unused_async = "warn"
unused_self = "warn"
verbose_file_reads = "warn"
wildcard_dependencies = "warn"

View file

@ -277,12 +277,12 @@ impl Service {
let command_line = lines.next().expect("each string has at least one line");
let body: Vec<_> = lines.collect();
let admin_command = match self.parse_admin_command(command_line) {
let admin_command = match Self::parse_admin_command(command_line) {
Ok(command) => command,
Err(error) => {
let server_name = services().globals.server_name();
let message = error.replace("server.name", server_name.as_str());
let html_message = self.usage_to_html(&message, server_name);
let html_message = Self::usage_to_html(&message, server_name);
return RoomMessageEventContent::text_html(message, html_message);
}
@ -306,7 +306,7 @@ impl Service {
}
// Parse chat messages from the admin room into an AdminCommand object
fn parse_admin_command(&self, command_line: &str) -> std::result::Result<AdminCommand, String> {
fn parse_admin_command(command_line: &str) -> std::result::Result<AdminCommand, String> {
// Note: argv[0] is `@grapevine:servername:`, which is treated as the main command
let mut argv: Vec<_> = command_line.split_whitespace().collect();
@ -864,7 +864,7 @@ impl Service {
}
// Utility to turn clap's `--help` text to HTML.
fn usage_to_html(&self, text: &str, server_name: &ServerName) -> String {
fn usage_to_html(text: &str, server_name: &ServerName) -> String {
// Replace `@grapevine:servername:-subcmdname` with `@grapevine:servername: subcmdname`
let text = text.replace(
&format!("@grapevine:{server_name}:-"),
@ -1183,6 +1183,8 @@ impl Service {
/// Gets the room ID of the admin room
///
/// Errors are propagated from the database, and will have None if there is no admin room
// Allowed because this function uses `services()`
#[allow(clippy::unused_self)]
pub(crate) fn get_admin_room(&self) -> Result<Option<OwnedRoomId>> {
let admin_room_alias: Box<RoomAliasId> =
format!("#admins:{}", services().globals.server_name())

View file

@ -86,7 +86,7 @@ impl Service {
/// Returns width, height of the thumbnail and whether it should be cropped. Returns None when
/// the server should send the original file.
pub(crate) fn thumbnail_properties(&self, width: u32, height: u32) -> Option<(u32, u32, bool)> {
fn thumbnail_properties(width: u32, height: u32) -> Option<(u32, u32, bool)> {
match (width, height) {
(0..=32, 0..=32) => Some((32, 32, true)),
(0..=96, 0..=96) => Some((96, 96, true)),
@ -113,9 +113,8 @@ impl Service {
width: u32,
height: u32,
) -> Result<Option<FileMeta>> {
let (width, height, crop) = self
.thumbnail_properties(width, height)
.unwrap_or((0, 0, false)); // 0, 0 because that's the original file
let (width, height, crop) =
Self::thumbnail_properties(width, height).unwrap_or((0, 0, false)); // 0, 0 because that's the original file
if let Ok((content_disposition, content_type, key)) =
self.db.search_file_metadata(mxc.clone(), width, height)

View file

@ -132,7 +132,7 @@ impl Service {
pub_key_map,
)
.await?;
self.check_room_id(room_id, &incoming_pdu)?;
Self::check_room_id(room_id, &incoming_pdu)?;
// 8. if not timeline event: stop
if !is_timeline_event {
@ -375,7 +375,7 @@ impl Service {
)
.map_err(|_| Error::bad_database("Event is not a valid PDU."))?;
self.check_room_id(room_id, &incoming_pdu)?;
Self::check_room_id(room_id, &incoming_pdu)?;
if !auth_events_known {
// 4. fetch any missing auth events doing all checks listed here starting at 1. These are not timeline events
@ -411,7 +411,7 @@ impl Service {
continue;
};
self.check_room_id(room_id, &auth_event)?;
Self::check_room_id(room_id, &auth_event)?;
match auth_events.entry((
auth_event.kind.to_string().into(),
@ -1287,7 +1287,7 @@ impl Service {
.await
.pop()
{
self.check_room_id(room_id, &pdu)?;
Self::check_room_id(room_id, &pdu)?;
if amount > services().globals.max_fetch_prev_events() {
// Max limit reached
@ -1612,6 +1612,8 @@ impl Service {
}
/// Returns Ok if the acl allows the server
// Allowed because this function uses `services()`
#[allow(clippy::unused_self)]
pub(crate) fn acl_check(&self, server_name: &ServerName, room_id: &RoomId) -> Result<()> {
let Some(acl_event) = services().rooms.state_accessor.room_state_get(
room_id,
@ -1815,7 +1817,7 @@ impl Service {
))
}
fn check_room_id(&self, room_id: &RoomId, pdu: &PduEvent) -> Result<()> {
fn check_room_id(room_id: &RoomId, pdu: &PduEvent) -> Result<()> {
if pdu.room_id != room_id {
warn!("Found event from room {} in room {}", pdu.room_id, room_id);
return Err(Error::BadRequest(

View file

@ -40,7 +40,11 @@ impl Service {
}
}
#[allow(clippy::too_many_arguments)]
#[allow(
clippy::too_many_arguments,
// Allowed because this function uses `services()`
clippy::unused_self,
)]
pub(crate) fn paginate_relations_with_filter(
&self,
sender_user: &UserId,

View file

@ -415,7 +415,7 @@ impl Service {
));
}
self.translate_joinrule(&join_rule)?
Self::translate_joinrule(&join_rule)?
},
room_type: services()
.rooms
@ -436,7 +436,7 @@ impl Service {
})
}
fn translate_joinrule(&self, join_rule: &JoinRule) -> Result<SpaceRoomJoinRule> {
fn translate_joinrule(join_rule: &JoinRule) -> Result<SpaceRoomJoinRule> {
match join_rule {
JoinRule::Invite => Ok(SpaceRoomJoinRule::Invite),
JoinRule::Knock => Ok(SpaceRoomJoinRule::Knock),
@ -448,6 +448,8 @@ impl Service {
}
}
// Allowed because this function uses `services()`
#[allow(clippy::unused_self)]
fn handle_simplified_join_rule(
&self,
join_rule: &SpaceRoomJoinRule,
@ -473,7 +475,7 @@ impl Service {
room_id: &RoomId,
) -> Result<bool> {
if self.handle_simplified_join_rule(
&self.translate_joinrule(join_rule)?,
&Self::translate_joinrule(join_rule)?,
sender_user,
room_id,
)? {

View file

@ -282,10 +282,7 @@ impl Service {
}
pub(crate) fn get_name(&self, room_id: &RoomId) -> Result<Option<String>> {
services()
.rooms
.state_accessor
.room_state_get(room_id, &StateEventType::RoomName, "")?
self.room_state_get(room_id, &StateEventType::RoomName, "")?
.map_or(Ok(None), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomNameEventContent| Some(c.name))
@ -300,16 +297,15 @@ impl Service {
}
pub(crate) fn get_avatar(&self, room_id: &RoomId) -> Result<JsOption<RoomAvatarEventContent>> {
services()
.rooms
.state_accessor
.room_state_get(room_id, &StateEventType::RoomAvatar, "")?
self.room_state_get(room_id, &StateEventType::RoomAvatar, "")?
.map_or(Ok(JsOption::Undefined), |s| {
serde_json::from_str(s.content.get())
.map_err(|_| Error::bad_database("Invalid room avatar event in database."))
})
}
// Allowed because this function uses `services()`
#[allow(clippy::unused_self)]
pub(crate) fn user_can_invite(
&self,
room_id: &RoomId,
@ -340,10 +336,7 @@ impl Service {
room_id: &RoomId,
user_id: &UserId,
) -> Result<Option<RoomMemberEventContent>> {
services()
.rooms
.state_accessor
.room_state_get(room_id, &StateEventType::RoomMember, user_id.as_str())?
self.room_state_get(room_id, &StateEventType::RoomMember, user_id.as_str())?
.map_or(Ok(None), |s| {
serde_json::from_str(s.content.get())
.map_err(|_| Error::bad_database("Invalid room member event in database."))

View file

@ -89,6 +89,8 @@ impl Service {
}
}
// Allowed because this function uses `services()`
#[allow(clippy::unused_self)]
pub(crate) fn compress_state_event(
&self,
shortstatekey: u64,
@ -106,6 +108,8 @@ impl Service {
}
/// Returns shortstatekey, event id
// Allowed because this function uses `services()`
#[allow(clippy::unused_self)]
pub(crate) fn parse_compressed_state_event(
&self,
compressed_event: &CompressedStateEvent,

View file

@ -662,7 +662,7 @@ impl Service {
// Our depth is the maximum depth of prev_events + 1
let depth = prev_events
.iter()
.filter_map(|event_id| Some(services().rooms.timeline.get_pdu(event_id).ok()??.depth))
.filter_map(|event_id| Some(self.get_pdu(event_id).ok()??.depth))
.max()
.unwrap_or_else(|| uint!(0))
+ uint!(1);

View file

@ -260,6 +260,8 @@ impl Service {
}
/// Check if a user is an admin
// Allowed because this function uses `services()`
#[allow(clippy::unused_self)]
pub(crate) fn is_admin(&self, user_id: &UserId) -> Result<bool> {
let admin_room_alias_id =
RoomAliasId::parse(format!("#admins:{}", services().globals.server_name()))