Skip to content

Commit bc7839a

Browse files
authored
refactor: clippy fixes and working doctests (#834)
1 parent cbbd809 commit bc7839a

34 files changed

+158
-136
lines changed

packages/watcher/src/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async fn main() -> anyhow::Result<()> {
3535

3636
let managed_windows = managed_handles
3737
.into_iter()
38-
.map(|handle| NativeWindow::new(handle))
38+
.map(NativeWindow::new)
3939
.collect::<Vec<_>>();
4040

4141
run_cleanup(managed_windows);
@@ -56,7 +56,7 @@ async fn query_initial_windows(
5656
.context("Failed to send window query command.")?;
5757

5858
client
59-
.client_response(&query_message)
59+
.client_response(query_message)
6060
.await
6161
.and_then(|response| match response.data {
6262
Some(ClientResponseData::Windows(data)) => Some(data),
@@ -83,12 +83,12 @@ async fn watch_managed_handles(
8383
"sub -e window_managed window_unmanaged application_exiting";
8484

8585
client
86-
.send(&subscription_message)
86+
.send(subscription_message)
8787
.await
8888
.context("Failed to send subscribe command to IPC server.")?;
8989

9090
let subscription_id = client
91-
.client_response(&subscription_message)
91+
.client_response(subscription_message)
9292
.await
9393
.and_then(|response| match response.data {
9494
Some(ClientResponseData::EventSubscribe(data)) => {

packages/wm/src/app_command.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::{
3838
},
3939
};
4040

41-
const VERSION: &'static str = env!("VERSION_NUMBER");
41+
const VERSION: &str = env!("VERSION_NUMBER");
4242

4343
#[derive(Clone, Debug, Parser)]
4444
#[clap(author, version = VERSION, about, long_about = None)]
@@ -522,11 +522,7 @@ impl InvokeCommand {
522522
match subject_container.as_window_container() {
523523
Ok(window) => {
524524
_ = window.native().set_title_bar_visibility(
525-
if *visibility == TitleBarVisibility::Shown {
526-
true
527-
} else {
528-
false
529-
},
525+
*visibility == TitleBarVisibility::Shown,
530526
);
531527
Ok(())
532528
}
@@ -536,7 +532,7 @@ impl InvokeCommand {
536532
InvokeCommand::ShellExec {
537533
hide_window,
538534
command,
539-
} => shell_exec(&command.join(" "), hide_window.clone()),
535+
} => shell_exec(&command.join(" "), *hide_window),
540536
InvokeCommand::Size(args) => {
541537
match subject_container.as_window_container() {
542538
Ok(window) => set_window_size(

packages/wm/src/common/commands/cycle_focus.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ pub fn cycle_focus(
4646
let window_of_type = workspace
4747
.descendant_focus_order()
4848
.filter_map(|descendant| descendant.as_window_container().ok())
49-
.find(|descendant| match (descendant.state(), &next) {
50-
(WindowState::Floating(_), WindowState::Floating(_)) => true,
51-
(WindowState::Fullscreen(_), WindowState::Fullscreen(_)) => true,
52-
(WindowState::Minimized, WindowState::Minimized) => true,
53-
(WindowState::Tiling, WindowState::Tiling) => true,
54-
_ => false,
49+
.find(|descendant| {
50+
matches!(
51+
(descendant.state(), &next),
52+
(WindowState::Floating(_), WindowState::Floating(_))
53+
| (WindowState::Fullscreen(_), WindowState::Fullscreen(_))
54+
| (WindowState::Minimized, WindowState::Minimized)
55+
| (WindowState::Tiling, WindowState::Tiling)
56+
)
5557
});
5658

5759
if let Some(window) = window_of_type {

packages/wm/src/common/commands/platform_sync.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn platform_sync(
2222
state: &mut WmState,
2323
config: &UserConfig,
2424
) -> anyhow::Result<()> {
25-
if state.pending_sync.containers_to_redraw.len() > 0 {
25+
if !state.pending_sync.containers_to_redraw.is_empty() {
2626
redraw_containers(state)?;
2727
state.pending_sync.containers_to_redraw.clear();
2828
}
@@ -129,10 +129,10 @@ fn redraw_containers(state: &mut WmState) -> anyhow::Result<()> {
129129
.to_rect()?
130130
.apply_delta(&window.total_border_delta()?, None);
131131

132-
let is_visible = match window.display_state() {
133-
DisplayState::Showing | DisplayState::Shown => true,
134-
_ => false,
135-
};
132+
let is_visible = matches!(
133+
window.display_state(),
134+
DisplayState::Showing | DisplayState::Shown
135+
);
136136

137137
if let Err(err) = window.native().set_position(
138138
&window.state(),

packages/wm/src/common/commands/reload_config.rs

-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ fn update_container_gaps(state: &mut WmState, config: &UserConfig) {
119119
let tiling_containers = state
120120
.root_container
121121
.self_and_descendants()
122-
.into_iter()
123122
.filter_map(|container| container.as_tiling_container().ok());
124123

125124
for container in tiling_containers {

packages/wm/src/common/direction.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ impl Direction {
1717
///
1818
/// Example:
1919
/// ```
20-
/// Direction::Left.inverse() // Direction::Right
20+
/// # use wm::common::Direction;
21+
/// let dir = Direction::Left.inverse();
22+
/// assert_eq!(dir, Direction::Right);
2123
/// ```
2224
pub fn inverse(&self) -> Direction {
2325
match self {
@@ -36,7 +38,10 @@ impl FromStr for Direction {
3638
///
3739
/// Example:
3840
/// ```
39-
/// Direction::from_str("left") // Direction::Left
41+
/// # use wm::common::Direction;
42+
/// # use std::str::FromStr;
43+
/// let dir = Direction::from_str("left");
44+
/// assert_eq!(dir.unwrap(), Direction::Left);
4045
/// ```
4146
fn from_str(unparsed: &str) -> anyhow::Result<Self> {
4247
match unparsed {

packages/wm/src/common/events/handle_display_settings_changed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub fn handle_display_settings_changed(
8585
}
8686

8787
for native_monitor in new_native_monitors {
88-
match pending_monitors.get(0) {
88+
match pending_monitors.first() {
8989
Some(_) => {
9090
let monitor = pending_monitors.remove(0);
9191
update_monitor(monitor, native_monitor, state)

packages/wm/src/common/events/handle_window_location_changed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn handle_window_location_changed(
6363
// A fullscreen window that gets minimized can hit this arm, so
6464
// ignore such events and let it be handled by the handler for
6565
// `PlatformEvent::WindowMinimized` instead.
66-
if !(is_fullscreen || is_maximized) && !is_minimized {
66+
if !(is_fullscreen || is_maximized || !is_minimized) {
6767
info!("Window restored");
6868

6969
let target_state = window

packages/wm/src/common/events/handle_window_minimized.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ pub fn handle_window_minimized(
3434
)?;
3535

3636
// Focus should be reassigned after a window has been minimized.
37-
if let Some(focus_target) =
38-
state.focus_target_after_removal(&window.into())
37+
if let Some(focus_target) = state.focus_target_after_removal(&window)
3938
{
4039
set_focused_descendant(focus_target, None);
4140
state.pending_sync.focus_change = true;

packages/wm/src/common/length_value.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ impl FromStr for LengthValue {
5050
///
5151
/// Example:
5252
/// ```
53-
/// LengthValue::from_str("100px") // { amount: 100.0, unit: LengthUnit::Pixel }
53+
/// # use wm::common::{LengthValue, LengthUnit};
54+
/// # use std::str::FromStr;
55+
/// let check = LengthValue {
56+
/// amount: 100.0,
57+
/// unit: LengthUnit::Pixel,
58+
/// };
59+
/// let parsed = LengthValue::from_str("100px");
60+
/// assert_eq!(parsed.unwrap(), check);
5461
/// ```
5562
fn from_str(unparsed: &str) -> anyhow::Result<Self> {
5663
let units_regex = Regex::new(r"([+-]?\d+)(%|px)?")?;

packages/wm/src/common/memo.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,25 @@ where
1010
value: Arc<Mutex<Option<T>>>,
1111
}
1212

13-
impl<T> Memo<T>
13+
impl<T> Default for Memo<T>
1414
where
1515
T: Clone,
1616
{
17-
/// Creates a new `Memo` instance with `None` as the initial value.
18-
pub fn new() -> Self {
17+
fn default() -> Self {
1918
Memo {
2019
value: Arc::new(Mutex::new(None)),
2120
}
2221
}
22+
}
23+
24+
impl<T> Memo<T>
25+
where
26+
T: Clone,
27+
{
28+
/// Creates a new `Memo` instance with `None` as the initial value.
29+
pub fn new() -> Self {
30+
Self::default()
31+
}
2332

2433
/// Retrieves the cached value if it exists, otherwise initializes it
2534
/// using the provided closure.
@@ -38,7 +47,7 @@ where
3847
.as_ref()
3948
.map(|value| Ok(value.clone()))
4049
.unwrap_or_else(|| {
41-
let value = retriever_fn(&arg)?;
50+
let value = retriever_fn(arg)?;
4251
*value_ref = Some(value.clone());
4352
Ok(value)
4453
})
@@ -53,7 +62,7 @@ where
5362
{
5463
let mut value_ref = self.value.lock().unwrap();
5564

56-
let value = retriever_fn(&arg)?;
65+
let value = retriever_fn(arg)?;
5766
*value_ref = Some(value.clone());
5867
Ok(value)
5968
}

packages/wm/src/common/platform/event_listener.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl EventListener {
6262
pub fn update(
6363
&mut self,
6464
config: &UserConfig,
65-
binding_modes: &Vec<BindingModeConfig>,
65+
binding_modes: &[BindingModeConfig],
6666
) {
6767
// Modify keybindings based on active binding modes.
6868
let keybindings = match binding_modes.first() {

packages/wm/src/common/platform/keyboard_hook.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ impl KeyboardHook {
121121
.collect::<Vec<_>>();
122122

123123
// Safety: A split string always has at least one element.
124-
let trigger_key = vk_codes.last().unwrap().clone();
124+
let trigger_key = *vk_codes.last().unwrap();
125125

126126
keybinding_map
127127
.entry(trigger_key)
128-
.or_insert_with(|| Vec::new())
128+
.or_insert_with(Vec::new)
129129
.push(ActiveKeybinding {
130130
vk_codes,
131131
config: keybinding.clone(),

packages/wm/src/common/platform/native_monitor.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ impl NativeMonitor {
8888
// Get the display devices associated with the monitor.
8989
let mut display_devices = (0..)
9090
.map_while(|index| {
91-
let mut display_device = DISPLAY_DEVICEW::default();
92-
display_device.cb =
93-
std::mem::size_of::<DISPLAY_DEVICEW>() as u32;
91+
let mut display_device = DISPLAY_DEVICEW {
92+
cb: std::mem::size_of::<DISPLAY_DEVICEW>() as u32,
93+
..Default::default()
94+
};
9495

9596
// Due to the `EDD_GET_DEVICE_INTERFACE_NAME` flag, the device
9697
// struct will contain the DOS device path under the `DeviceId`
@@ -104,7 +105,7 @@ impl NativeMonitor {
104105
)
105106
}
106107
.as_bool()
107-
.then(|| display_device)
108+
.then_some(display_device)
108109
})
109110
// Filter out any devices that are not active.
110111
.filter(|device| device.StateFlags & DISPLAY_DEVICE_ACTIVE != 0);
@@ -173,7 +174,7 @@ pub fn available_monitors() -> anyhow::Result<Vec<NativeMonitor>> {
173174
Ok(
174175
available_monitor_handles()?
175176
.into_iter()
176-
.map(|handle| NativeMonitor::new(handle))
177+
.map(NativeMonitor::new)
177178
.collect(),
178179
)
179180
}

packages/wm/src/common/platform/platform.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,18 @@ impl Platform {
337337
///
338338
/// # Examples
339339
///
340-
/// ```
341-
/// let (prog, args) = parse_command("code .")?;
342-
/// assert_eq!(prog, "code");
343-
/// assert_eq!(args, ".");
340+
/// ```no_run
341+
/// # use wm::common::platform::Platform;
342+
/// let (prog, args) = Platform::parse_command("code .")?;
343+
/// assert_eq!(prog, "code");
344+
/// assert_eq!(args, ".");
344345
///
345-
/// let (prog, args) = parse_command(
346-
/// r#"C:\Program Files\Git\git-bash --cd=C:\Users\larsb\.glaze-wm"#,
347-
/// )?;
348-
/// assert_eq!(prog, r#"C:\Program Files\Git\git-bash"#);
349-
/// assert_eq!(args, r#"--cd=C:\Users\larsb\.glaze-wm"#);
346+
/// let (prog, args) = Platform::parse_command(
347+
/// r#"C:\Program Files\Git\git-bash --cd=C:\Users\larsb\.glaze-wm"#,
348+
/// )?;
349+
/// assert_eq!(prog, r#"C:\Program Files\Git\git-bash"#);
350+
/// assert_eq!(args, r#"--cd=C:\Users\larsb\.glaze-wm"#);
351+
/// # Ok::<(), anyhow::Error>(())
350352
/// ```
351353
pub fn parse_command(command: &str) -> anyhow::Result<(String, String)> {
352354
// Expand environment variables in the command string.
@@ -376,7 +378,7 @@ impl Platform {
376378
};
377379

378380
let command_parts: Vec<&str> =
379-
expanded_command.trim().split_whitespace().collect();
381+
expanded_command.split_whitespace().collect();
380382

381383
// If the command starts with double quotes, then the program name/path
382384
// is wrapped in double quotes (e.g. `"C:\path\to\app.exe" --flag`).
@@ -435,8 +437,8 @@ impl Platform {
435437
// causes issues where the pointer is dropped while `ShellExecuteExW`
436438
// is using it. This is likely a `windows-rs` bug, and we can avoid
437439
// it by keeping separate variables for the wide strings.
438-
let program_wide = to_wide(&program);
439-
let args_wide = to_wide(&args);
440+
let program_wide = to_wide(program);
441+
let args_wide = to_wide(args);
440442
let home_dir_wide = to_wide(&home_dir);
441443

442444
// Using the built-in `Command::new` function in Rust launches the

packages/wm/src/common/tiling_direction.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ impl TilingDirection {
1717
///
1818
/// Example:
1919
/// ```
20-
/// TilingDirection::Horizontal.inverse() // TilingDirection::Vertical
20+
/// # use wm::common::TilingDirection;
21+
/// let dir = TilingDirection::Horizontal.inverse();
22+
/// assert_eq!(dir, TilingDirection::Vertical);
2123
/// ```
2224
pub fn inverse(&self) -> TilingDirection {
2325
match self {
@@ -31,7 +33,9 @@ impl TilingDirection {
3133
///
3234
/// Example:
3335
/// ```
34-
/// TilingDirection::from_direction(Direction::Left) // TilingDirection::Horizontal
36+
/// # use wm::common::{Direction, TilingDirection};
37+
/// let dir = TilingDirection::from_direction(&Direction::Left);
38+
/// assert_eq!(dir, TilingDirection::Horizontal);
3539
/// ```
3640
pub fn from_direction(direction: &Direction) -> TilingDirection {
3741
match direction {
@@ -48,7 +52,13 @@ impl FromStr for TilingDirection {
4852
///
4953
/// Example:
5054
/// ```
51-
/// TilingDirection::from_str("horizontal") // TilingDirection::Horizontal
55+
/// # use wm::common::TilingDirection;
56+
/// # use std::str::FromStr;
57+
/// let dir = TilingDirection::from_str("horizontal");
58+
/// assert_eq!(dir.unwrap(), TilingDirection::Horizontal);
59+
///
60+
/// let dir = TilingDirection::from_str("vertical");
61+
/// assert_eq!(dir.unwrap(), TilingDirection::Vertical);
5262
/// ```
5363
fn from_str(unparsed: &str) -> anyhow::Result<Self> {
5464
match unparsed {

packages/wm/src/containers/commands/flatten_child_split_containers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::containers::{
88
/// parent container.
99
///
1010
/// For example:
11-
/// ```
11+
/// ```ignore,compile_fail
1212
/// H[1 H[V[2, 3]]] -> H[1, 2, 3]
1313
/// H[1 H[2, 3]] -> H[1, 2, 3]
1414
/// H[V[1]] -> V[1]

packages/wm/src/containers/commands/flatten_split_container.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn flatten_split_container(
1717
let parent = split_container.parent().context("No parent.")?;
1818

1919
let updated_children =
20-
split_container.children().into_iter().map(|child| {
20+
split_container.children().into_iter().inspect(|child| {
2121
*child.borrow_parent_mut() = Some(parent.clone());
2222

2323
// Resize tiling children to fit the size of the split container.
@@ -26,8 +26,6 @@ pub fn flatten_split_container(
2626
split_container.tiling_size() * tiling_child.tiling_size(),
2727
);
2828
}
29-
30-
child
3129
});
3230

3331
let index = split_container.index();

0 commit comments

Comments
 (0)