Skip to content

Commit

Permalink
Remove usages of mem::replace where its return value is unused (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis authored May 6, 2020
1 parent ed61459 commit 3135e0f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 52 deletions.
9 changes: 2 additions & 7 deletions src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,9 @@ impl GuildChannel {
f(&mut edit_channel);
let edited = serenity_utils::hashmap_to_json_map(edit_channel.0);

match cache_http.http().edit_channel(self.id.0, &edited) {
Ok(channel) => {
std::mem::replace(self, channel);
*self = cache_http.http().edit_channel(self.id.0, &edited)?;

Ok(())
},
Err(why) => Err(why),
}
Ok(())
}

/// Edits a [`Message`] in the channel given its Id.
Expand Down
21 changes: 5 additions & 16 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use serde::{
use super::utils::U64Visitor;
#[cfg(feature = "model")]
use std::{
mem,
result::Result as StdResult,
};
#[cfg(feature = "model")]
Expand Down Expand Up @@ -280,14 +279,9 @@ impl Message {

let map = serenity_utils::hashmap_to_json_map(builder.0);

match cache_http.http().edit_message(self.channel_id.0, self.id.0, &Value::Object(map)) {
Ok(edited) => {
mem::replace(self, edited);
*self = cache_http.http().edit_message(self.channel_id.0, self.id.0, &Value::Object(map))?;

Ok(())
},
Err(why) => Err(why),
}
Ok(())
}

pub(crate) fn transform_content(&mut self) {
Expand Down Expand Up @@ -580,14 +574,9 @@ impl Message {

let map = serenity_utils::hashmap_to_json_map(suppress.0);

match cache_http.http().edit_message(self.channel_id.0, self.id.0, &Value::Object(map))
{
Ok(edited) => {
mem::replace(self, edited);
Ok(())
}
Err(why) => Err(why),
}
*self = cache_http.http().edit_message(self.channel_id.0, self.id.0, &Value::Object(map))?;

Ok(())
}

/// Checks whether the message mentions passed [`UserId`].
Expand Down
11 changes: 2 additions & 9 deletions src/model/guild/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use serde_json::json;
#[cfg(all(feature = "cache", feature = "model"))]
use crate::internal::prelude::*;
#[cfg(all(feature = "cache", feature = "model"))]
use std::mem;
#[cfg(all(feature = "cache", feature = "model"))]
use super::super::ModelError;
#[cfg(all(feature = "cache", feature = "model"))]
use super::super::id::GuildId;
Expand Down Expand Up @@ -119,14 +117,9 @@ impl Emoji {
"name": name,
});

match cache_http.http().edit_emoji(guild_id.0, self.id.0, &map) {
Ok(emoji) => {
mem::replace(self, emoji);
*self = cache_http.http().edit_emoji(guild_id.0, self.id.0, &map)?;

Ok(())
},
Err(why) => Err(why),
}
Ok(())
},
None => Err(Error::Model(ModelError::ItemMissing)),
}
Expand Down
18 changes: 5 additions & 13 deletions src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use crate::http::GuildPagination;
use parking_lot::RwLock;
#[cfg(feature = "model")]
use std::fmt::Write;
#[cfg(feature = "model")]
use std::mem;
#[cfg(all(feature = "cache", feature = "model"))]
use crate::cache::CacheRwLock;
#[cfg(feature = "model")]
Expand Down Expand Up @@ -118,14 +116,9 @@ impl CurrentUser {
f(&mut edit_profile);
let map = utils::hashmap_to_json_map(edit_profile.0);

match http.as_ref().edit_profile(&map) {
Ok(new) => {
let _ = mem::replace(self, new);
*self = http.as_ref().edit_profile(&map)?;

Ok(())
},
Err(why) => Err(why),
}
Ok(())
}

/// Retrieves the URL to the current user's avatar, falling back to the
Expand Down Expand Up @@ -721,11 +714,10 @@ impl User {
/// Replaces the instance with the data retrieved over the REST API.
#[inline]
pub fn refresh(&mut self, cache_http: impl CacheHttp) -> Result<()> {
self.id.to_user(cache_http).map(|replacement| {
mem::replace(self, replacement);
})
}
*self = self.id.to_user(cache_http)?;

Ok(())
}

/// Returns a static formatted URL of the user's icon, if one exists.
///
Expand Down
9 changes: 2 additions & 7 deletions src/model/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,9 @@ impl Webhook {
map.insert("name".to_string(), Value::String(name.to_string()));
}

match http.as_ref().edit_webhook_with_token(self.id.0, &self.token, &map) {
Ok(replacement) => {
mem::replace(self, replacement);
*self = http.as_ref().edit_webhook_with_token(self.id.0, &self.token, &map)?;

Ok(())
},
Err(why) => Err(why),
}
Ok(())
}

/// Executes a webhook with the fields set via the given builder.
Expand Down

0 comments on commit 3135e0f

Please sign in to comment.