Skip to content

Commit 43e73f2

Browse files
committed
clean: general pedantic fixes
1 parent 99f1b56 commit 43e73f2

13 files changed

Lines changed: 96 additions & 86 deletions

File tree

src/app/apps.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,22 @@ pub struct App {
8383
pub desc: String,
8484

8585
/// The information specific to a certain type of app
86-
pub app_data: AppData,
86+
pub data: AppData,
8787

8888
/// A unique ID generated for each instance of an App.
89+
#[allow(unused)]
8990
id: usize,
9091
}
9192

9293
impl PartialEq for App {
9394
fn eq(&self, other: &Self) -> bool {
94-
self.app_data == other.app_data && self.name == other.name
95+
self.data == other.data && self.name == other.name
9596
}
9697
}
9798

9899
impl App {
99100
/// Get the numeric id of an app
101+
#[allow(unused)]
100102
pub fn id(&self) -> usize {
101103
self.id
102104
}
@@ -110,7 +112,7 @@ impl App {
110112
name: name.to_string(),
111113
desc: desc.to_string(),
112114
id: ID.fetch_add(1, Ordering::Relaxed),
113-
app_data: data,
115+
data,
114116
}
115117
}
116118

@@ -245,7 +247,7 @@ impl App {
245247
.height(50);
246248

247249
if theme.show_icons {
248-
match self.app_data {
250+
match self.data {
249251
AppData::Command {
250252
icon: Some(ref icon),
251253
..
@@ -277,7 +279,7 @@ impl App {
277279
}
278280
row = row.push(container(text_block).width(Fill));
279281

280-
let msg = match self.app_data {
282+
let msg = match self.data {
281283
AppData::Builtin {
282284
command: AppCommand::Function(func),
283285
..

src/app/menubar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn menu_icon(#[cfg(not(target_os = "linux"))] hotkey: HotKey, sender: ExtSen
2727

2828
let menu = Menu::with_items(&[
2929
&version_item(),
30-
&about_item(image),
30+
&about_item(&image),
3131
&open_github_item(),
3232
&PredefinedMenuItem::separator(),
3333
&refresh_item(),
@@ -196,7 +196,7 @@ fn quit_item() -> PredefinedMenuItem {
196196
PredefinedMenuItem::quit(Some("Quit"))
197197
}
198198

199-
fn about_item(image: DynamicImage) -> PredefinedMenuItem {
199+
fn about_item(image: &DynamicImage) -> PredefinedMenuItem {
200200
let about_metadata_builder = AboutMetadataBuilder::new()
201201
.name(Some("RustCast"))
202202
.version(Some(

src/app/pages/clipboard.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ use iced::widget::{
66
use crate::{app::pages::prelude::*, clipboard::ClipBoardContentType};
77

88
pub fn clipboard_view(
9-
clipboard_content: Vec<ClipBoardContentType>,
9+
clipboard_content: &[ClipBoardContentType],
1010
focussed_id: u32,
11-
theme: Theme,
11+
theme: &Theme,
1212
focus_id: u32,
1313
) -> Element<'static, Message> {
1414
let theme_clone = theme.clone();
1515
let theme_clone_2 = theme.clone();
1616
container(Row::from_vec(vec![
1717
container(
1818
scrollable(
19-
Column::from_iter(clipboard_content.iter().enumerate().map(|(i, content)| {
19+
clipboard_content.iter().enumerate().map(|(i, content)| {
20+
// I'd be surprised if you get 4 billion entries
21+
#[allow(clippy::cast_possible_truncation)]
2022
content.to_app().render(theme.clone(), i as u32, focus_id)
21-
}))
23+
}).collect::<Column<_>>()
2224
.width(WINDOW_WIDTH / 3.),
2325
)
2426
.id("results"),

src/app/pages/emoji.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use crate::{app::pages::prelude::*, clipboard::ClipBoardContentType, commands::F
44

55
pub fn emoji_page(
66
tile_theme: Theme,
7-
emojis: Vec<App>,
7+
emojis: &[App],
88
focussed_id: u32,
99
) -> Element<'static, Message> {
1010
let emoji_vec = emojis
1111
.chunks(6)
12-
.map(<[crate::app::apps::App]>::to_vec)
12+
.map(<[App]>::to_vec)
1313
.collect::<Vec<Vec<App>>>();
1414

1515
let mut column = Vec::new();

src/app/tile/elm.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,36 +192,38 @@ pub fn view(tile: &Tile, wid: window::Id) -> Element<'_, Message> {
192192

193193
let results = if tile.page == Page::ClipboardHistory {
194194
clipboard_view(
195-
tile.clipboard_content.clone(),
195+
&tile.clipboard_content,
196196
tile.focus_id,
197-
tile.config.theme.clone(),
197+
&tile.config.theme,
198198
tile.focus_id,
199199
)
200200
} else if tile.results.is_empty() {
201201
space().into()
202202
} else if tile.page == Page::EmojiSearch {
203-
emoji_page(
204-
tile.config.theme.clone(),
205-
tile.emoji_apps
203+
let results: Vec<_> = tile.emoji_apps
206204
.search_prefix(&tile.query_lc)
207205
.map(std::borrow::ToOwned::to_owned)
208-
.collect(),
206+
.collect();
207+
208+
emoji_page(
209+
tile.config.theme.clone(),
210+
&results,
209211
tile.focus_id,
210212
)
211213
} else {
212-
container(Column::from_iter(tile.results.iter().enumerate().map(
214+
container(tile.results.iter().enumerate().map(
213215
|(i, app)| {
216+
#[allow(clippy::cast_possible_truncation)]
214217
app.clone()
215218
.render(tile.config.theme.clone(), i as u32, tile.focus_id)
216-
},
217-
)))
219+
}
220+
).collect::<Column<_>>())
218221
.into()
219222
};
220223

221224
let results_count = match &tile.page {
222-
Page::Main => tile.results.len(),
223225
Page::ClipboardHistory => tile.clipboard_content.len(),
224-
Page::EmojiSearch => tile.results.len(),
226+
Page::Main | Page::EmojiSearch => tile.results.len(),
225227
};
226228

227229
let height = if tile.page == Page::ClipboardHistory {
@@ -230,6 +232,7 @@ pub fn view(tile: &Tile, wid: window::Id) -> Element<'_, Message> {
230232
std::cmp::min(tile.results.len() * 60, 290)
231233
};
232234

235+
#[allow(clippy::cast_possible_truncation)]
233236
let scrollable = Scrollable::with_direction(results, scrollbar_direction)
234237
.id("results")
235238
.height(height as u32);

src/app/tile/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ pub struct Tile {
119119

120120
impl Tile {
121121
/// This returns the theme of the window
122-
pub fn theme(&self, _: window::Id) -> Option<Theme> {
123-
Some(self.theme.clone())
122+
pub fn theme(&self, _: window::Id) -> Theme {
123+
self.theme.clone()
124124
}
125125

126126
/// This handles the subscriptions of the window
@@ -328,10 +328,7 @@ fn handle_hot_reloading() -> impl futures::Stream<Item = Message> {
328328

329329
fn count_dirs_in_dir(dir: &PathBuf) -> usize {
330330
// Read the directory; if it fails, treat as empty
331-
let entries = match fs::read_dir(dir) {
332-
Ok(e) => e,
333-
Err(_) => return 0,
334-
};
331+
let Ok(entries) = fs::read_dir(dir) else { return 0 };
335332

336333
entries
337334
.filter_map(std::result::Result::ok)

src/app/tile/search_query.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::cmp;
77
use super::Tile;
88
use crate::{
99
app::{
10-
ArrowKey, DEFAULT_WINDOW_HEIGHT, Message, Page, RUSTCAST_DESC_NAME, WINDOW_WIDTH,
10+
ArrowKey, DEFAULT_WINDOW_HEIGHT, Message, Page, WINDOW_WIDTH,
1111
apps::{App, AppCommand},
1212
},
1313
calculator::Expr,
@@ -19,6 +19,7 @@ use crate::{
1919
#[cfg(target_os = "macos")]
2020
use crate::cross_platform::macos::haptics::{HapticPattern, perform_haptic};
2121

22+
#[allow(clippy::too_many_lines)]
2223
pub(super) fn handle_change(tile: &mut Tile, input: &str, id: Id) -> iced::Task<Message> {
2324
tile.focus_id = 0;
2425
#[cfg(target_os = "macos")]
@@ -92,9 +93,9 @@ pub(super) fn handle_change(tile: &mut Tile, input: &str, id: Id) -> iced::Task<
9293
{
9394
let res_string = res.eval().map_or(String::new(), |x| x.to_string());
9495
tile.results.push(App::new_builtin(
95-
RUSTCAST_DESC_NAME,
9696
&res_string,
9797
"",
98+
"Calculation result",
9899
AppCommand::Function(Function::Calculate(res.clone())),
99100
));
100101
} else if tile.results.is_empty()
@@ -162,6 +163,7 @@ pub(super) fn handle_change(tile: &mut Tile, input: &str, id: Id) -> iced::Task<
162163
let max_elem = cmp::min(5, new_length);
163164

164165
if prev_size != new_length && tile.page != Page::ClipboardHistory {
166+
#[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation, clippy::cast_sign_loss)]
165167
Task::batch([
166168
window::resize(
167169
id,
@@ -173,6 +175,7 @@ pub(super) fn handle_change(tile: &mut Tile, input: &str, id: Id) -> iced::Task<
173175
Task::done(Message::ChangeFocus(ArrowKey::Left)),
174176
])
175177
} else if tile.page == Page::ClipboardHistory {
178+
#[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation, clippy::cast_sign_loss)]
176179
Task::batch([
177180
window::resize(
178181
id,

src/app/tile/update.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::commands::Function;
2121
use crate::config::Config;
2222
use crate::utils::index_installed_apps;
2323

24+
#[allow(clippy::too_many_lines)]
2425
pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
2526
tracing::trace!("Handling update (message: {:?})", message);
2627

@@ -95,10 +96,11 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
9596
}
9697

9798
Message::ChangeFocus(key) => {
99+
#[allow(clippy::cast_possible_truncation)] // No, there won't be more than 2^32-1 items in a list
98100
let len = match tile.page {
99101
Page::ClipboardHistory => tile.clipboard_content.len() as u32,
100102
Page::EmojiSearch => tile.emoji_apps.search_prefix(&tile.query_lc).count() as u32, // or tile.results.len()
101-
_ => tile.results.len() as u32,
103+
Page::Main => tile.results.len() as u32,
102104
};
103105

104106
let old_focus_id = tile.focus_id;
@@ -139,6 +141,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
139141
Page::EmojiSearch => 5.,
140142
};
141143

144+
#[allow(clippy::cast_precision_loss)]
142145
Task::batch([
143146
task,
144147
operation::scroll_to(
@@ -154,7 +157,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
154157
Message::OpenFocused => match tile
155158
.results
156159
.get(tile.focus_id as usize)
157-
.map(|x| &x.app_data)
160+
.map(|x| &x.data)
158161
{
159162
Some(AppData::Builtin {
160163
command: AppCommand::Function(func),

src/calculator.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl Expr {
101101
pub fn from_str(s: &str) -> Result<Expr, String> {
102102
let mut p = Parser::new(s);
103103
let expr = p.parse_expr()?;
104-
p.expect(Token::End)?;
104+
p.expect(&Token::End)?;
105105
Ok(expr)
106106
}
107107
}
@@ -151,10 +151,7 @@ impl<'a> Lexer<'a> {
151151

152152
fn next_token(&mut self) -> Result<Token, String> {
153153
self.skip_ws();
154-
let c = match self.peek_char() {
155-
Some(c) => c,
156-
None => return Ok(Token::End),
157-
};
154+
let Some(c) = self.peek_char() else { return Ok(Token::End) };
158155

159156
// single-char tokens
160157
let tok = match c {
@@ -195,7 +192,7 @@ impl<'a> Lexer<'a> {
195192
if c.is_ascii_digit() || c == '.' {
196193
return self.lex_number();
197194
} else if c.is_ascii_alphabetic() || c == '_' {
198-
return self.lex_ident();
195+
return Ok(self.lex_ident());
199196
}
200197
return Err(format!("Unexpected character: {c}"));
201198
}
@@ -232,7 +229,7 @@ impl<'a> Lexer<'a> {
232229
Ok(Token::Number(n))
233230
}
234231

235-
fn lex_ident(&mut self) -> Result<Token, String> {
232+
fn lex_ident(&mut self) -> Token {
236233
let start = self.i;
237234
while let Some(c) = self.peek_char() {
238235
if c.is_ascii_alphanumeric() || c == '_' {
@@ -241,7 +238,7 @@ impl<'a> Lexer<'a> {
241238
break;
242239
}
243240
}
244-
Ok(Token::Ident(self.input[start..self.i].to_string()))
241+
Token::Ident(self.input[start..self.i].to_string())
245242
}
246243
}
247244

@@ -264,8 +261,8 @@ impl<'a> Parser<'a> {
264261
Ok(())
265262
}
266263

267-
fn expect(&mut self, t: Token) -> Result<(), String> {
268-
if self.cur == t {
264+
fn expect(&mut self, t: &Token) -> Result<(), String> {
265+
if self.cur == *t {
269266
self.bump()
270267
} else {
271268
Err(format!("Expected {:?}, found {:?}", t, self.cur))
@@ -359,14 +356,14 @@ impl<'a> Parser<'a> {
359356
Token::LParen => {
360357
self.bump()?;
361358
let e = self.parse_expr()?;
362-
self.expect(Token::RParen)?;
359+
self.expect(&Token::RParen)?;
363360
Ok(e)
364361
}
365362
Token::Ident(name) => {
366363
let name = name.clone();
367364
self.bump()?;
368365
// function call must be ident '(' ...
369-
self.expect(Token::LParen)?;
366+
self.expect(&Token::LParen)?;
370367
let mut args = Vec::new();
371368
if self.cur != Token::RParen {
372369
loop {
@@ -378,7 +375,7 @@ impl<'a> Parser<'a> {
378375
break;
379376
}
380377
}
381-
self.expect(Token::RParen)?;
378+
self.expect(&Token::RParen)?;
382379
Ok(Expr::Func { name, args })
383380
}
384381
_ => Err(format!("Unexpected token: {:?}", self.cur)),

0 commit comments

Comments
 (0)