Skip to content

Commit

Permalink
refactor: denylist macros
Browse files Browse the repository at this point in the history
Add a Clippy denylist for macros. Denies the use of the following
macros:

- `serde_json::json`
- `std::vec`
- `std::write`

Closes #1628.
  • Loading branch information
zeylahellyer committed Jan 18, 2023
1 parent 227e407 commit 1fdeeae
Show file tree
Hide file tree
Showing 47 changed files with 890 additions and 407 deletions.
5 changes: 5 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
disallowed-macros = [
{ path = "serde_json::json", reason = "less flexible and performant, use structs and serde_derive"},
{ path = "std::vec", reason = "the Vec::new and Vec::from methods are preferred" },
{ path = "std::write", reason = "slow to compile and run, use Formatter, Display, or Debug calls instead"},
]
2 changes: 1 addition & 1 deletion examples/gateway-request-members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async fn main() -> anyhow::Result<()> {
// check to make sure you're requesting at most that many:
let request = RequestGuildMembers::builder(Id::new(1))
.nonce("requesting two member")
.user_ids(vec![Id::new(2), Id::new(3)])
.user_ids(Vec::from([Id::new(2), Id::new(3)]))
.unwrap();

shard.command(&request).await?;
Expand Down
4 changes: 3 additions & 1 deletion examples/model-webhook-slash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ where
// Check if the signature matches and else return a error response.
if PUB_KEY
.verify(
vec![timestamp.as_bytes(), &whole_body].concat().as_ref(),
Vec::from([timestamp.as_bytes(), &whole_body])
.concat()
.as_ref(),
&signature,
)
.is_err()
Expand Down
8 changes: 4 additions & 4 deletions twilight-cache-inmemory/src/event/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ mod tests {
cache.cache_emoji(guild_id, emote_3.clone());

cache.update(&GuildEmojisUpdate {
emojis: vec![emote.clone(), emote_3.clone()],
emojis: Vec::from([emote.clone(), emote_3.clone()]),
guild_id,
});

Expand All @@ -171,7 +171,7 @@ mod tests {
assert!(cache.emoji(emote_3.id).is_some());

cache.update(&GuildEmojisUpdate {
emojis: vec![emote.clone()],
emojis: Vec::from([emote.clone()]),
guild_id,
});

Expand All @@ -183,7 +183,7 @@ mod tests {
let emote_4 = test::emoji(Id::new(4), None);

cache.update(&GuildEmojisUpdate {
emojis: vec![emote_4.clone()],
emojis: Vec::from([emote_4.clone()]),
guild_id,
});

Expand All @@ -193,7 +193,7 @@ mod tests {
assert!(cache.emoji(emote.id).is_none());

cache.update(&GuildEmojisUpdate {
emojis: vec![],
emojis: Vec::new(),
guild_id,
});

Expand Down
2 changes: 1 addition & 1 deletion twilight-cache-inmemory/src/event/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ mod tests {
discovery_splash: None,
emojis: Vec::new(),
explicit_content_filter: ExplicitContentFilter::AllMembers,
features: vec![],
features: Vec::new(),
icon: None,
id: Id::new(123),
joined_at: Some(Timestamp::from_secs(1_632_072_645).expect("non zero")),
Expand Down
6 changes: 3 additions & 3 deletions twilight-cache-inmemory/src/event/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ mod tests {
pending: false,
permissions: Permissions::empty(),
premium_since: None,
roles: vec![Id::new(8)],
roles: Vec::from([Id::new(8)]),
},
)]),
messages: HashMap::from([(
Expand Down Expand Up @@ -176,11 +176,11 @@ mod tests {
pinned: false,
reactions: Vec::new(),
reference: None,
sticker_items: vec![MessageSticker {
sticker_items: Vec::from([MessageSticker {
format_type: StickerFormatType::Png,
id: Id::new(1),
name: "sticker name".to_owned(),
}],
}]),
referenced_message: None,
thread: None,
timestamp,
Expand Down
6 changes: 3 additions & 3 deletions twilight-cache-inmemory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,14 +965,14 @@ mod tests {
nick: None,
pending: false,
premium_since: None,
roles: vec![Id::new(1), Id::new(2)],
roles: Vec::from([Id::new(1), Id::new(2)]),
user,
},
);

cache.cache_roles(
guild_id,
vec![
Vec::from([
Role {
color: 0,
hoist: false,
Expand All @@ -999,7 +999,7 @@ mod tests {
tags: None,
unicode_emoji: None,
},
],
]),
);

assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions twilight-cache-inmemory/src/model/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod tests {
name: "foo".to_owned(),
managed: false,
require_colons: true,
roles: vec![],
roles: Vec::new(),
user: None,
available: true,
};
Expand All @@ -151,7 +151,7 @@ mod tests {
name: "foo".to_owned(),
managed: false,
require_colons: true,
roles: vec![],
roles: Vec::new(),
user_id: None,
available: true,
};
Expand Down
4 changes: 2 additions & 2 deletions twilight-gateway/src/shard/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ impl ShardBuilder {
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let shard = Shard::builder(env::var("DISCORD_TOKEN")?, Intents::empty()).presence(
/// UpdatePresencePayload::new(
/// vec![MinimalActivity {
/// Vec::from([MinimalActivity {
/// kind: ActivityType::Playing,
/// name: "Not accepting commands".into(),
/// url: None,
/// }
/// .into()],
/// .into()]),
/// false,
/// None,
/// Status::Idle,
Expand Down
2 changes: 1 addition & 1 deletion twilight-gateway/src/shard/processor/compression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Compression {
/// message failed.
#[cfg_attr(
not(any(feature = "zlib-stock", feature = "zlib-simd")),
allow(clippy::unused_self)
allow(clippy::unnecessary_wraps, clippy::unused_self)
)]
pub fn message_mut(&mut self) -> Result<Option<&mut [u8]>, ReceivingEventError> {
#[cfg(any(feature = "zlib-stock", feature = "zlib-simd"))]
Expand Down
4 changes: 2 additions & 2 deletions twilight-gateway/tests/test_shard_command_ratelimit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn test_shard_command_ratelimit() {

// now that we're connected we can test sending
let payload = UpdatePresence::new(
vec![Activity {
Vec::from([Activity {
application_id: None,
assets: None,
buttons: Vec::new(),
Expand All @@ -61,7 +61,7 @@ async fn test_shard_command_ratelimit() {
state: None,
timestamps: None,
url: None,
}],
}]),
false,
Some(1),
Status::DoNotDisturb,
Expand Down
36 changes: 18 additions & 18 deletions twilight-http/src/request/guild/create_guild/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ mod tests {
VoiceFieldsBuilder::new("voicename".to_owned())
.unwrap()
.bitrate(96_000)
.permission_overwrites(vec![overwrite()])
.permission_overwrites(Vec::from([overwrite()]))
.user_limit(40)
.build()
}
Expand Down Expand Up @@ -838,12 +838,12 @@ mod tests {
id: Id::new(1),
kind: ChannelType::GuildVoice,
name: String::from("voicename"),
permission_overwrites: Some(vec![PermissionOverwrite {
permission_overwrites: Some(Vec::from([PermissionOverwrite {
allow: Some(perms()),
deny: Some(Permissions::empty()),
id: Id::new(2),
kind: PermissionOverwriteType::Role,
}]),
}])),
parent_id: None,
user_limit: Some(40),
}
Expand All @@ -854,7 +854,7 @@ mod tests {
TextFieldsBuilder::new("textname".to_owned())
.unwrap()
.nsfw()
.permission_overwrites(vec![overwrite()])
.permission_overwrites(Vec::from([overwrite()]))
.rate_limit_per_user(4_000)
.unwrap()
.topic("a topic".to_owned())
Expand All @@ -877,12 +877,12 @@ mod tests {
kind: ChannelType::GuildText,
name: String::from("textname"),
nsfw: Some(true),
permission_overwrites: Some(vec![PermissionOverwrite {
permission_overwrites: Some(Vec::from([PermissionOverwrite {
allow: Some(perms()),
deny: Some(Permissions::empty()),
id: Id::new(2),
kind: PermissionOverwriteType::Role
}]),
}])),
parent_id: None,
rate_limit_per_user: Some(4_000),
topic: Some(String::from("a topic")),
Expand Down Expand Up @@ -910,7 +910,7 @@ mod tests {

assert_eq!(
channels.build(),
vec![
Vec::from([
GuildChannelFields::Category(CategoryFields {
id: Id::new(2),
kind: ChannelType::GuildCategory,
Expand All @@ -922,7 +922,7 @@ mod tests {
kind: ChannelType::GuildText,
name: String::from("textname"),
nsfw: Some(true),
permission_overwrites: Some(vec![PermissionOverwrite {
permission_overwrites: Some(Vec::from([PermissionOverwrite {
allow: Some(
Permissions::CONNECT
| Permissions::SPEAK
Expand All @@ -931,7 +931,7 @@ mod tests {
deny: Some(Permissions::empty()),
id: Id::new(2),
kind: PermissionOverwriteType::Role,
}]),
}])),
parent_id: Some(Id::new(2)),
rate_limit_per_user: Some(4_000),
topic: Some(String::from("a topic")),
Expand All @@ -941,7 +941,7 @@ mod tests {
id: Id::new(1),
kind: ChannelType::GuildVoice,
name: String::from("voicename"),
permission_overwrites: Some(vec![PermissionOverwrite {
permission_overwrites: Some(Vec::from([PermissionOverwrite {
allow: Some(
Permissions::CONNECT
| Permissions::SPEAK
Expand All @@ -950,11 +950,11 @@ mod tests {
deny: Some(Permissions::empty()),
id: Id::new(2),
kind: PermissionOverwriteType::Role,
}]),
}])),
parent_id: Some(Id::new(2)),
user_limit: Some(40),
}),
]
])
);
}

Expand All @@ -966,13 +966,13 @@ mod tests {

assert_eq!(
channels.build(),
vec![
Vec::from([
GuildChannelFields::Text(TextFields {
id: Id::new(1),
kind: ChannelType::GuildText,
name: String::from("textname"),
nsfw: Some(true),
permission_overwrites: Some(vec![PermissionOverwrite {
permission_overwrites: Some(Vec::from([PermissionOverwrite {
allow: Some(
Permissions::CONNECT
| Permissions::SPEAK
Expand All @@ -981,7 +981,7 @@ mod tests {
deny: Some(Permissions::empty()),
id: Id::new(2),
kind: PermissionOverwriteType::Role,
}]),
}])),
parent_id: None,
rate_limit_per_user: Some(4_000),
topic: Some(String::from("a topic")),
Expand All @@ -991,7 +991,7 @@ mod tests {
id: Id::new(1),
kind: ChannelType::GuildVoice,
name: String::from("voicename"),
permission_overwrites: Some(vec![PermissionOverwrite {
permission_overwrites: Some(Vec::from([PermissionOverwrite {
allow: Some(
Permissions::CONNECT
| Permissions::SPEAK
Expand All @@ -1000,11 +1000,11 @@ mod tests {
deny: Some(Permissions::empty()),
id: Id::new(2),
kind: PermissionOverwriteType::Role,
}]),
}])),
parent_id: None,
user_limit: Some(40),
}),
]
])
);
}
}
8 changes: 4 additions & 4 deletions twilight-http/src/request/guild/create_guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl<'a> CreateGuild<'a> {
pub fn add_role(mut self, role: RoleFields) -> Self {
if self.fields.roles.is_none() {
let builder = RoleFieldsBuilder::new("@everyone".to_owned());
self.fields.roles.replace(vec![builder.build()]);
self.fields.roles.replace(Vec::from([builder.build()]));
}

if let Some(roles) = self.fields.roles.as_mut() {
Expand Down Expand Up @@ -389,7 +389,7 @@ impl<'a> CreateGuild<'a> {
roles.remove(0);
roles.insert(0, everyone);
} else {
self.fields.roles.replace(vec![everyone]);
self.fields.roles.replace(Vec::from([everyone]));
}

self
Expand Down Expand Up @@ -424,9 +424,9 @@ impl<'a> CreateGuild<'a> {
/// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::new("my token".to_owned());
///
/// let roles = vec![RoleFieldsBuilder::new("role 1".to_owned())
/// let roles = Vec::from([RoleFieldsBuilder::new("role 1".to_owned())
/// .color(0x543923)?
/// .build()];
/// .build()]);
/// client
/// .create_guild("guild name".to_owned())?
/// .roles(roles)?
Expand Down
11 changes: 8 additions & 3 deletions twilight-lavalink/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,20 @@ pub fn unmark_failed_address(
authorization: impl AsRef<str>,
route_address: impl Into<IpAddr>,
) -> Result<Request<Vec<u8>>, HttpError> {
#[derive(Serialize)]
struct UnmarkFailedRequestBody {
address: IpAddr,
}

let mut req = Request::post(format!("{}/routeplanner/status", node_address.into()));

let auth_value = HeaderValue::from_str(authorization.as_ref())?;
req = req.header(AUTHORIZATION, auth_value);

req.body(
serde_json::to_vec(&serde_json::json!({
"address": route_address.into(),
}))
serde_json::to_vec(&UnmarkFailedRequestBody {
address: route_address.into(),
})
.expect("valid json"),
)
}
Expand Down
Loading

0 comments on commit 1fdeeae

Please sign in to comment.