Skip to content

Commit 6288847

Browse files
Merge pull request #30 from jamesbt365/pedantic
Make TabletBot pass most clippy::pedantic lints
2 parents 8dfffd1 + c040216 commit 6288847

8 files changed

Lines changed: 67 additions & 58 deletions

File tree

src/commands/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub async fn respond_embed(ctx: &Context<'_>, embed: CreateEmbed, ephemeral: boo
2828
let result = ctx.send(builder).await;
2929

3030
if let Err(e) = result {
31-
println!("Failed to respond: {}", e)
31+
println!("Failed to respond: {e}");
3232
}
3333
}
3434

@@ -70,8 +70,8 @@ pub async fn paginate_lists<U, E>(
7070
embed_title: &str,
7171
) -> Result<(), Error> {
7272
let ctx_id = ctx.id();
73-
let prev_button_id = format!("{}prev", ctx_id);
74-
let next_button_id = format!("{}next", ctx_id);
73+
let prev_button_id = format!("{ctx_id}prev");
74+
let next_button_id = format!("{ctx_id}next");
7575

7676
let colour = Colour::TEAL;
7777

src/commands/snippets.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use poise::serenity_prelude::{
99
CreateInteractionResponseMessage,
1010
};
1111

12+
#[allow(clippy::unused_async)]
1213
async fn autocomplete_snippet<'a>(
1314
ctx: Context<'a>,
1415
partial: &'a str,
@@ -40,7 +41,7 @@ pub async fn snippet(
4041
id: String,
4142
) -> Result<(), Error> {
4243
// Lazily get snippet because this is a prefix command too.
43-
if let Some(snippet) = get_snippet_lazy(&ctx, &id).await {
44+
if let Some(snippet) = get_snippet_lazy(&ctx, &id) {
4445
let embed = snippet.embed();
4546

4647
respond_embed(&ctx, embed, false).await;
@@ -78,6 +79,7 @@ pub async fn create_snippet(
7879
};
7980

8081
println!("New snippet created '{}: {}'", snippet.id, snippet.title);
82+
8183
let mut embed = snippet.embed();
8284

8385
embed = embed.colour(super::OK_COLOUR);
@@ -111,7 +113,7 @@ pub async fn edit_snippet(
111113
#[description = "The snippet's title"] title: Option<String>,
112114
#[description = "The snippet's content"] content: Option<String>,
113115
) -> Result<(), Error> {
114-
match get_snippet_lazy(&ctx, &id).await {
116+
match get_snippet_lazy(&ctx, &id) {
115117
Some(mut snippet) => {
116118
if let Some(title) = title {
117119
snippet.title = title;
@@ -134,7 +136,7 @@ pub async fn edit_snippet(
134136
None => {
135137
let title = &"Failed to edit snippet";
136138
let content = &&format!("The snippet '{id}' does not exist");
137-
respond_err(&ctx, title, content).await
139+
respond_err(&ctx, title, content).await;
138140
}
139141
};
140142

@@ -149,14 +151,14 @@ pub async fn remove_snippet(
149151
#[description = "The snippet's id"]
150152
id: String,
151153
) -> Result<(), Error> {
152-
match get_snippet_lazy(&ctx, &id).await {
154+
match get_snippet_lazy(&ctx, &id) {
153155
Some(snippet) => {
154156
remove_snippet_confirm(&ctx, &snippet).await?;
155157
}
156158
None => {
157159
let title = &"Failed to remove snippet";
158160
let content = &&format!("The snippet '{id}' does not exist");
159-
respond_err(&ctx, title, content).await
161+
respond_err(&ctx, title, content).await;
160162
}
161163
}
162164

@@ -190,7 +192,7 @@ pub async fn list_snippets(ctx: Context<'_>) -> Result<(), Error> {
190192
.map(|snippet| (snippet.id.clone(), snippet.title.clone(), true))
191193
.collect::<Vec<(String, String, bool)>>()
192194
.chunks(25)
193-
.map(|chunk| chunk.to_vec())
195+
.map(<[(String, String, bool)]>::to_vec)
194196
.collect();
195197

196198
super::paginate_lists(ctx, &pages, "Snippets").await?;
@@ -209,7 +211,7 @@ pub async fn export_snippet(
209211
#[description = "The snippet's id"]
210212
id: String,
211213
) -> Result<(), Error> {
212-
match get_snippet_lazy(&ctx, &id).await {
214+
match get_snippet_lazy(&ctx, &id) {
213215
Some(snippet) => {
214216
let attachment =
215217
CreateAttachment::bytes(snippet.content.replace('\n', r"\n"), "snippet.txt");
@@ -221,7 +223,7 @@ pub async fn export_snippet(
221223
None => {
222224
let title = &"Failed to export snippet";
223225
let content = &&format!("The snippet '{id}' does not exist");
224-
respond_err(&ctx, title, content).await
226+
respond_err(&ctx, title, content).await;
225227
}
226228
}
227229

@@ -239,7 +241,7 @@ impl Embeddable for Snippet {
239241
}
240242

241243
// Exact matches the snippet id and name.
242-
async fn _get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
244+
fn _get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
243245
let data = ctx.data();
244246
let rwlock_guard = data.state.read().unwrap();
245247

@@ -251,7 +253,7 @@ async fn _get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
251253
}
252254

253255
// Matches the snippet by checking if its starts with the id and name.
254-
async fn get_snippet_lazy(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
256+
fn get_snippet_lazy(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
255257
let data = ctx.data();
256258
let rwlock_guard = data.state.read().unwrap();
257259

@@ -262,7 +264,7 @@ async fn get_snippet_lazy(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
262264
.cloned()
263265
}
264266

265-
async fn rm_snippet(ctx: &Context<'_>, snippet: &Snippet) {
267+
fn rm_snippet(ctx: &Context<'_>, snippet: &Snippet) {
266268
let data = ctx.data();
267269
let mut rwlock_guard = data.state.write().unwrap();
268270

@@ -281,8 +283,8 @@ async fn remove_snippet_confirm(ctx: &Context<'_>, snippet: &Snippet) -> Result<
281283
let snippet_embed = snippet.embed();
282284

283285
let ctx_id = ctx.id();
284-
let delete_id = format!("{}cancel", ctx_id);
285-
let cancel_id = format!("{}delete", ctx_id);
286+
let delete_id = format!("{ctx_id}cancel");
287+
let cancel_id = format!("{ctx_id}delete");
286288

287289
let components = serenity::CreateActionRow::Buttons(vec![
288290
serenity::CreateButton::new(&cancel_id).label("Cancel"),
@@ -322,7 +324,7 @@ async fn handle_delete(
322324
snippet: &Snippet,
323325
interaction: serenity::ComponentInteraction,
324326
) -> Result<(), Error> {
325-
rm_snippet(ctx, snippet).await;
327+
rm_snippet(ctx, snippet);
326328
interaction
327329
.create_response(
328330
ctx,

src/commands/utils.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use poise::serenity_prelude::{Colour, CreateEmbed, CreateEmbedFooter, EditMessag
88
use regex::Regex;
99
use serenity::futures::{self, Stream, StreamExt};
1010

11+
#[allow(clippy::unused_async)]
1112
async fn autocomplete_key<'a>(
1213
ctx: Context<'a>,
1314
partial: &'a str,
@@ -63,7 +64,7 @@ pub async fn embed(
6364
match url.parse::<reqwest::Url>() {
6465
Ok(_) => {
6566
if title.is_some() {
66-
embed = embed.url(url)
67+
embed = embed.url(url);
6768
} else {
6869
respond_err(
6970
&ctx,
@@ -76,7 +77,7 @@ pub async fn embed(
7677
}
7778
Err(e) => {
7879
let title = "Invalid url provided";
79-
let content = &format!("The url '{}' is not a valid url: {}", url, e);
80+
let content = &format!("The url '{url}' is not a valid url: {e}");
8081
respond_err(&ctx, title, content).await;
8182
return Ok(());
8283
}
@@ -229,7 +230,7 @@ pub async fn edit_embed(
229230
let builder = EditMessage::default().embed(embedb);
230231

231232
match msg_clone.edit(ctx, builder).await {
232-
Ok(_) => {
233+
Ok(()) => {
233234
respond_ok(
234235
&ctx,
235236
"Successfully edited embed",
@@ -239,7 +240,7 @@ pub async fn edit_embed(
239240
}
240241
Err(error) => {
241242
// Better error handling later.
242-
respond_err(&ctx, "Error while handling message!", &format!("{}", error)).await
243+
respond_err(&ctx, "Error while handling message!", &format!("{error}")).await;
243244
}
244245
}
245246
} else {
@@ -313,7 +314,7 @@ pub async fn add_repo(
313314
respond_ok(
314315
&ctx,
315316
"Successfully added issue token",
316-
&format!("{}: {}/{}", key, owner, repository),
317+
&format!("{key}: {owner}/{repository}"),
317318
)
318319
.await;
319320

@@ -333,16 +334,16 @@ pub async fn remove_repo(
333334
// impl a solution directly into the types?
334335

335336
// not sure why I have to do this, it won't settle otherwise.
336-
let key_str = format!("The repository with the key '{}' has been removed", key);
337-
match get_repo_details(&ctx, &key).await {
337+
let key_str = format!("The repository with the key '{key}' has been removed");
338+
match get_repo_details(&ctx, &key) {
338339
Some(_) => {
339-
rm_repo(&ctx, &key).await;
340+
rm_repo(&ctx, &key);
340341

341342
respond_ok(&ctx, "Successfully removed repository!", &key_str).await;
342343
}
343344
None => {
344345
let title = "Failure to find repository";
345-
let content = format!("The key '{}' does not exist.", key);
346+
let content = format!("The key '{key}' does not exist.");
346347
respond_err(&ctx, title, &content).await;
347348
}
348349
};
@@ -383,22 +384,22 @@ pub async fn list_repos(ctx: Context<'_>) -> Result<(), Error> {
383384
})
384385
.collect::<Vec<(String, String, bool)>>()
385386
.chunks(25)
386-
.map(|chunk| chunk.to_vec())
387+
.map(<[(String, String, bool)]>::to_vec)
387388
.collect();
388389

389390
super::paginate_lists(ctx, &pages, "Repositories").await?;
390391

391392
Ok(())
392393
}
393394

394-
async fn get_repo_details(ctx: &Context<'_>, key: &str) -> Option<RepositoryDetails> {
395+
fn get_repo_details(ctx: &Context<'_>, key: &str) -> Option<RepositoryDetails> {
395396
let data = ctx.data();
396397
let rwlock_guard = data.state.read().unwrap();
397398

398399
rwlock_guard.issue_prefixes.get(key).cloned()
399400
}
400401

401-
async fn rm_repo(ctx: &Context<'_>, key: &str) {
402+
fn rm_repo(ctx: &Context<'_>, key: &str) {
402403
let data = ctx.data();
403404
let mut rwlock_guard = data.state.write().unwrap();
404405

src/events/code.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ async fn get_embeds(ctx: &Context, message: &Message) -> Option<Vec<CreateEmbed>
3535

3636
typing.stop();
3737

38-
if !embeds.is_empty() {
39-
Some(embeds)
40-
} else {
38+
if embeds.is_empty() {
4139
None
40+
} else {
41+
Some(embeds)
4242
}
4343
}
4444

@@ -47,12 +47,12 @@ async fn http_get_body_text(url: &String) -> Option<String> {
4747
Ok(res) => match res.text().await {
4848
Ok(content) => Some(content),
4949
Err(e) => {
50-
println!("Failed to get text: {}", e);
50+
println!("Failed to get text: {e}");
5151
None
5252
}
5353
},
5454
Err(e) => {
55-
println!("Failed to get response: {}", e);
55+
println!("Failed to get response: {e}");
5656
None
5757
}
5858
}
@@ -95,10 +95,10 @@ impl FileReference<'_> {
9595
})
9696
.collect();
9797

98-
if !files.is_empty() {
99-
Some(files)
100-
} else {
98+
if files.is_empty() {
10199
None
100+
} else {
101+
Some(files)
102102
}
103103
}
104104

@@ -108,7 +108,7 @@ impl FileReference<'_> {
108108
if let Some(mut content) = self.display().await {
109109
content.shrink_to(4096 - 8 - extension.len());
110110

111-
let description = format!("```{}\n{}\n```", extension, content);
111+
let description = format!("```{extension}\n{content}\n```");
112112

113113
let mut default = CreateEmbed::default();
114114
default = default
@@ -135,7 +135,7 @@ impl FileReference<'_> {
135135
"https://raw.githubusercontent.com/{}/{}/{}/{}",
136136
self.owner, self.repo, self.git_ref, self.path
137137
);
138-
println!("Downloading content: {}", url);
138+
println!("Downloading content: {url}");
139139

140140
if let Some(content) = http_get_body_text(&url).await {
141141
let lines: Vec<&str> = content.split('\n').collect();

src/events/issue.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub async fn message(data: &Data, ctx: &Context, message: &Message) {
2222
let typing = message.channel_id.start_typing(&ctx.http);
2323

2424
let ctx_id = message.id.get(); // poise context isn't available here.
25-
let remove_id = format!("{}remove", ctx_id);
26-
let hide_body_id = format!("{}hide_body", ctx_id);
25+
let remove_id = format!("{ctx_id}remove");
26+
let hide_body_id = format!("{ctx_id}hide_body");
2727
let remove = CreateActionRow::Buttons(vec![CreateButton::new(&remove_id)
2828
.label("delete")
2929
.style(ButtonStyle::Danger)]);
@@ -124,7 +124,7 @@ async fn issue_embeds(data: &Data, message: &Message) -> Option<Vec<CreateEmbed>
124124
let client = octocrab::instance();
125125
let ratelimit = client.ratelimit();
126126

127-
let regex = Regex::new(r#" ?([a-zA-Z0-9-_.]+)?#([0-9]+) ?"#).expect("Expected numbers regex");
127+
let regex = Regex::new(r" ?([a-zA-Z0-9-_.]+)?#([0-9]+) ?").expect("Expected numbers regex");
128128

129129
let custom_repos = { data.state.read().unwrap().issue_prefixes.clone() };
130130

@@ -211,7 +211,7 @@ impl Document for Issue {
211211

212212
let mut description = String::default();
213213
for line in body.split('\n').take(15) {
214-
description.push_str(&format!("{}\n", line));
214+
description.push_str(&format!("{line}\n"));
215215
}
216216

217217
description.shrink_to(4096);
@@ -226,16 +226,16 @@ impl Document for Issue {
226226
}
227227

228228
fn get_labels(&self) -> Option<String> {
229-
if !self.labels.is_empty() {
229+
if self.labels.is_empty() {
230+
None
231+
} else {
230232
let labels = &self
231233
.labels
232234
.iter()
233235
.map(|l| l.name.clone())
234236
.collect::<Vec<String>>();
235237

236238
Some(format!("`{}`", labels.join("`, `")))
237-
} else {
238-
None
239239
}
240240
}
241241
}
@@ -270,7 +270,7 @@ impl Embeddable for PullRequest {
270270
embed = embed.field("Labels", labels, true);
271271
}
272272

273-
embed.to_owned()
273+
embed
274274
}
275275
}
276276

@@ -287,7 +287,7 @@ impl Document for PullRequest {
287287

288288
let mut content = String::default();
289289
for line in body.split('\n').take(15) {
290-
content.push_str(&format!("{}\n", line));
290+
content.push_str(&format!("{line}\n"));
291291
}
292292

293293
content.shrink_to(4096);

src/formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub fn trim_indent(lines: &[&str]) -> String {
22
let base_indent = get_base_indent(lines);
3-
let prefix = String::from_iter(std::iter::repeat(' ').take(base_indent));
3+
let prefix = " ".repeat(base_indent);
44

55
let trimmed_lines: Vec<&str> = lines
66
.iter()

0 commit comments

Comments
 (0)