Skip to content

Commit 6f5ee53

Browse files
committed
Make TabletBot pass most clippy::pedantic lints
1 parent bebdde4 commit 6f5ee53

8 files changed

Lines changed: 63 additions & 52 deletions

File tree

src/commands/mod.rs

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

2929
if let Err(e) = result {
30-
println!("Failed to respond: {}", e)
30+
println!("Failed to respond: {e}");
3131
}
3232
}
3333

@@ -55,8 +55,8 @@ pub async fn paginate_lists<U, E>(
5555
embed_title: &str,
5656
) -> Result<(), Error> {
5757
let ctx_id = ctx.id();
58-
let prev_button_id = format!("{}prev", ctx_id);
59-
let next_button_id = format!("{}next", ctx_id);
58+
let prev_button_id = format!("{ctx_id}prev");
59+
let next_button_id = format!("{ctx_id}next");
6060

6161
let colour = Colour::TEAL;
6262

src/commands/snippets.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
use ::serenity::futures::{Stream, StreamExt};
77
use poise::serenity_prelude::{futures, CreateAttachment, CreateEmbed};
88

9+
#[allow(clippy::unused_async)]
910
async fn autocomplete_snippet<'a>(
1011
ctx: Context<'a>,
1112
partial: &'a str,
@@ -39,7 +40,7 @@ pub async fn snippet(
3940
id: String,
4041
) -> Result<(), Error> {
4142
// Lazily get snippet because this is a prefix command too.
42-
if let Some(snippet) = get_snippet_lazy(&ctx, &id).await {
43+
if let Some(snippet) = get_snippet_lazy(&ctx, &id) {
4344
let embed = snippet.embed();
4445

4546
respond_embed(&ctx, embed, false).await;
@@ -80,7 +81,7 @@ pub async fn create_snippet(
8081
rwlock_guard.snippets.push(snippet.clone());
8182

8283
rwlock_guard.snippets = rwlock_guard.snippets.clone();
83-
println!("New snippet created '{}: {}'", id, title);
84+
println!("New snippet created '{id}: {title}'");
8485
rwlock_guard.write();
8586

8687
let mut embed = snippet.embed();
@@ -112,7 +113,7 @@ pub async fn edit_snippet(
112113
#[description = "The snippet's title"] title: Option<String>,
113114
#[description = "The snippet's content"] content: Option<String>,
114115
) -> Result<(), Error> {
115-
match get_snippet_lazy(&ctx, &id).await {
116+
match get_snippet_lazy(&ctx, &id) {
116117
Some(mut snippet) => {
117118
if let Some(title) = title {
118119
snippet.title = title;
@@ -135,7 +136,7 @@ pub async fn edit_snippet(
135136
None => {
136137
let title = &"Failed to edit snippet";
137138
let content = &&format!("The snippet '{id}' does not exist");
138-
respond_err(&ctx, title, content).await
139+
respond_err(&ctx, title, content).await;
139140
}
140141
};
141142

@@ -150,17 +151,17 @@ pub async fn remove_snippet(
150151
#[description = "The snippet's id"]
151152
id: String,
152153
) -> Result<(), Error> {
153-
match get_snippet_lazy(&ctx, &id).await {
154+
match get_snippet_lazy(&ctx, &id) {
154155
Some(snippet) => {
155-
rm_snippet(&ctx, &snippet).await;
156+
rm_snippet(&ctx, &snippet);
156157
let title = &"Snippet successfully removed";
157158
let content = &&format!("Removed snippet '{}: {}'", snippet.id, snippet.title);
158159
respond_ok(&ctx, title, content).await;
159160
}
160161
None => {
161162
let title = &"Failed to remove snippet";
162163
let content = &&format!("The snippet '{id}' does not exist");
163-
respond_err(&ctx, title, content).await
164+
respond_err(&ctx, title, content).await;
164165
}
165166
}
166167

@@ -194,7 +195,7 @@ pub async fn list_snippets(ctx: Context<'_>) -> Result<(), Error> {
194195
.map(|snippet| (snippet.id.clone(), snippet.title.clone(), true))
195196
.collect::<Vec<(String, String, bool)>>()
196197
.chunks(25)
197-
.map(|chunk| chunk.to_vec())
198+
.map(<[(String, String, bool)]>::to_vec)
198199
.collect();
199200

200201
super::paginate_lists(ctx, &pages, "Snippets").await?;
@@ -213,10 +214,10 @@ pub async fn export_snippet(
213214
#[description = "The snippet's id"]
214215
id: String,
215216
) -> Result<(), Error> {
216-
match get_snippet_lazy(&ctx, &id).await {
217+
match get_snippet_lazy(&ctx, &id) {
217218
Some(snippet) => {
218219
let attachment = CreateAttachment::bytes(
219-
format!("{}", &snippet.content.replace('\n', r"\n")),
220+
snippet.content.replace('\n', r"\n").to_string(),
220221
"snippet.txt",
221222
);
222223
let message = poise::CreateReply::default()
@@ -227,7 +228,7 @@ pub async fn export_snippet(
227228
None => {
228229
let title = &"Failed to export snippet";
229230
let content = &&format!("The snippet '{id}' does not exist");
230-
respond_err(&ctx, title, content).await
231+
respond_err(&ctx, title, content).await;
231232
}
232233
}
233234

@@ -245,7 +246,7 @@ impl Embeddable for Snippet {
245246
}
246247

247248
// Exact matches the snippet id and name.
248-
async fn _get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
249+
fn _get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
249250
let data = ctx.data();
250251
let rwlock_guard = data.state.read().unwrap();
251252

@@ -257,7 +258,7 @@ async fn _get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
257258
}
258259

259260
// Matches the snippet by checking if its starts with the id and name.
260-
async fn get_snippet_lazy(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
261+
fn get_snippet_lazy(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
261262
let data = ctx.data();
262263
let rwlock_guard = data.state.read().unwrap();
263264

@@ -268,7 +269,7 @@ async fn get_snippet_lazy(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
268269
.cloned()
269270
}
270271

271-
async fn rm_snippet(ctx: &Context<'_>, snippet: &Snippet) {
272+
fn rm_snippet(ctx: &Context<'_>, snippet: &Snippet) {
272273
let data = ctx.data();
273274
let mut rwlock_guard = data.state.write().unwrap();
274275

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,
@@ -65,7 +66,7 @@ pub async fn embed(
6566
match url.parse::<reqwest::Url>() {
6667
Ok(_) => {
6768
if title.is_some() {
68-
embed = embed.url(url)
69+
embed = embed.url(url);
6970
} else {
7071
respond_err(
7172
&ctx,
@@ -78,7 +79,7 @@ pub async fn embed(
7879
}
7980
Err(e) => {
8081
let title = "Invalid url provided";
81-
let content = &format!("The url '{}' is not a valid url: {}", url, e);
82+
let content = &format!("The url '{url}' is not a valid url: {e}");
8283
respond_err(&ctx, title, content).await;
8384
return Ok(());
8485
}
@@ -231,7 +232,7 @@ pub async fn edit_embed(
231232
let builder = EditMessage::default().embed(embedb);
232233

233234
match msg_clone.edit(ctx, builder).await {
234-
Ok(_) => {
235+
Ok(()) => {
235236
respond_ok(
236237
&ctx,
237238
"Successfully edited embed",
@@ -241,7 +242,7 @@ pub async fn edit_embed(
241242
}
242243
Err(error) => {
243244
// Better error handling later.
244-
respond_err(&ctx, "Error while handling message!", &format!("{}", error)).await
245+
respond_err(&ctx, "Error while handling message!", &format!("{error}")).await;
245246
}
246247
}
247248
} else {
@@ -315,7 +316,7 @@ pub async fn add_repo(
315316
respond_ok(
316317
&ctx,
317318
"Successfully added issue token",
318-
&format!("{}: {}/{}", key, owner, repository),
319+
&format!("{key}: {owner}/{repository}"),
319320
)
320321
.await;
321322

@@ -335,16 +336,16 @@ pub async fn remove_repo(
335336
// impl a solution directly into the types?
336337

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

343344
respond_ok(&ctx, "Successfully removed repository!", &key_str).await;
344345
}
345346
None => {
346347
let title = "Failure to find repository";
347-
let content = format!("The key '{}' does not exist.", key);
348+
let content = format!("The key '{key}' does not exist.");
348349
respond_err(&ctx, title, &content).await;
349350
}
350351
};
@@ -385,22 +386,22 @@ pub async fn list_repos(ctx: Context<'_>) -> Result<(), Error> {
385386
})
386387
.collect::<Vec<(String, String, bool)>>()
387388
.chunks(25)
388-
.map(|chunk| chunk.to_vec())
389+
.map(<[(String, String, bool)]>::to_vec)
389390
.collect();
390391

391392
super::paginate_lists(ctx, &pages, "Repositories").await?;
392393

393394
Ok(())
394395
}
395396

396-
async fn get_repo_details(ctx: &Context<'_>, key: &str) -> Option<RepositoryDetails> {
397+
fn get_repo_details(ctx: &Context<'_>, key: &str) -> Option<RepositoryDetails> {
397398
let data = ctx.data();
398399
let rwlock_guard = data.state.read().unwrap();
399400

400401
rwlock_guard.issue_prefixes.get(key).cloned()
401402
}
402403

403-
async fn rm_repo(ctx: &Context<'_>, key: &str) {
404+
fn rm_repo(ctx: &Context<'_>, key: &str) {
404405
let data = ctx.data();
405406
let mut rwlock_guard = data.state.write().unwrap();
406407

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
@@ -21,8 +21,8 @@ pub async fn message(data: &Data, ctx: &Context, message: &Message) {
2121
let typing = message.channel_id.start_typing(&ctx.http);
2222

2323
let ctx_id = message.id.get(); // poise context isn't available here.
24-
let remove_id = format!("{}remove", ctx_id);
25-
let hide_body_id = format!("{}hide_body", ctx_id);
24+
let remove_id = format!("{ctx_id}remove");
25+
let hide_body_id = format!("{ctx_id}hide_body");
2626
let components = serenity::CreateActionRow::Buttons(vec![
2727
serenity::CreateButton::new(&remove_id)
2828
.label("delete")
@@ -108,7 +108,7 @@ async fn issue_embeds(data: &Data, message: &Message) -> Option<Vec<CreateEmbed>
108108
let ratelimit = client.ratelimit();
109109

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

113113
let custom_repos = { data.state.read().unwrap().issue_prefixes.clone() };
114114

@@ -193,7 +193,7 @@ impl Document for Issue {
193193

194194
let mut description = String::default();
195195
for line in body.split('\n').take(15) {
196-
description.push_str(&format!("{}\n", line));
196+
description.push_str(&format!("{line}\n"));
197197
}
198198

199199
description.shrink_to(4096);
@@ -208,16 +208,16 @@ impl Document for Issue {
208208
}
209209

210210
fn get_labels(&self) -> Option<String> {
211-
if !self.labels.is_empty() {
211+
if self.labels.is_empty() {
212+
None
213+
} else {
212214
let labels = &self
213215
.labels
214216
.iter()
215217
.map(|l| l.name.clone())
216218
.collect::<Vec<String>>();
217219

218220
Some(format!("`{}`", labels.join("`, `")))
219-
} else {
220-
None
221221
}
222222
}
223223
}
@@ -252,7 +252,7 @@ impl Embeddable for PullRequest {
252252
embed = embed.field("Labels", labels, true);
253253
}
254254

255-
embed.to_owned()
255+
embed
256256
}
257257
}
258258

@@ -269,7 +269,7 @@ impl Document for PullRequest {
269269

270270
let mut content = String::default();
271271
for line in body.split('\n').take(15) {
272-
content.push_str(&format!("{}\n", line));
272+
content.push_str(&format!("{line}\n"));
273273
}
274274

275275
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)