refactor(clippy): apply all super pedantic lints

Realised that I hadn't turned on super pedantic mode for clippy in the
komorebi-core and komorebic crates. This commit ensures the same clippy
config across all crates and applies the lint suggestions that arose as
a result of turning on the same config everywhere.
This commit is contained in:
LGUG2Z
2021-08-20 13:26:14 -07:00
parent 1625ca6e5d
commit 292bdb282f
9 changed files with 115 additions and 126 deletions

View File

@@ -12,7 +12,8 @@ pub enum CycleDirection {
} }
impl CycleDirection { impl CycleDirection {
pub fn next_idx(&self, idx: usize, len: usize) -> usize { #[must_use]
pub const fn next_idx(&self, idx: usize, len: usize) -> usize {
match self { match self {
CycleDirection::Previous => { CycleDirection::Previous => {
if idx == 0 { if idx == 0 {

View File

@@ -20,13 +20,15 @@ pub enum Layout {
#[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ArgEnum)] #[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ArgEnum)]
#[strum(serialize_all = "snake_case")] #[strum(serialize_all = "snake_case")]
pub enum LayoutFlip { pub enum Flip {
Horizontal, Horizontal,
Vertical, Vertical,
HorizontalAndVertical, HorizontalAndVertical,
} }
impl Layout { impl Layout {
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn resize( pub fn resize(
&self, &self,
unaltered: &Rect, unaltered: &Rect,
@@ -35,10 +37,14 @@ impl Layout {
sizing: Sizing, sizing: Sizing,
step: Option<i32>, step: Option<i32>,
) -> Option<Rect> { ) -> Option<Rect> {
if !matches!(self, Self::BSP) {
return None;
};
let max_divisor = 1.005; let max_divisor = 1.005;
let mut r = resize.unwrap_or_default(); let mut r = resize.unwrap_or_default();
let resize_step = if let Some(step) = step { step } else { 50 }; let resize_step = step.unwrap_or(50);
match edge { match edge {
OperationDirection::Left => match sizing { OperationDirection::Left => match sizing {
@@ -117,19 +123,21 @@ impl Layout {
}, },
}; };
if !r.eq(&Rect::default()) { if r.eq(&Rect::default()) {
Option::from(r)
} else {
None None
} else {
Option::from(r)
} }
} }
#[must_use]
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
pub fn calculate( pub fn calculate(
&self, &self,
area: &Rect, area: &Rect,
len: NonZeroUsize, len: NonZeroUsize,
container_padding: Option<i32>, container_padding: Option<i32>,
layout_flip: Option<LayoutFlip>, layout_flip: Option<Flip>,
resize_dimensions: &[Option<Rect>], resize_dimensions: &[Option<Rect>],
) -> Vec<Rect> { ) -> Vec<Rect> {
let len = usize::from(len); let len = usize::from(len);
@@ -187,24 +195,6 @@ impl Layout {
} }
} }
impl Layout {
pub fn next(&mut self) {
match self {
Layout::BSP => *self = Layout::Columns,
Layout::Columns => *self = Layout::Rows,
Layout::Rows => *self = Layout::BSP,
}
}
pub fn previous(&mut self) {
match self {
Layout::BSP => *self = Layout::Rows,
Layout::Columns => *self = Layout::BSP,
Layout::Rows => *self = Layout::Columns,
}
}
}
fn calculate_resize_adjustments(resize_dimensions: &[Option<Rect>]) -> Vec<Option<Rect>> { fn calculate_resize_adjustments(resize_dimensions: &[Option<Rect>]) -> Vec<Option<Rect>> {
let mut resize_adjustments = resize_dimensions.to_vec(); let mut resize_adjustments = resize_dimensions.to_vec();
@@ -213,6 +203,7 @@ fn calculate_resize_adjustments(resize_dimensions: &[Option<Rect>]) -> Vec<Optio
if let Some(resize_ref) = opt { if let Some(resize_ref) = opt {
if i > 0 { if i > 0 {
if resize_ref.left != 0 { if resize_ref.left != 0 {
#[allow(clippy::if_not_else)]
let range = if i == 1 { let range = if i == 1 {
0..1 0..1
} else if i & 1 != 0 { } else if i & 1 != 0 {
@@ -291,7 +282,7 @@ fn recursive_fibonacci(
idx: usize, idx: usize,
count: usize, count: usize,
area: &Rect, area: &Rect,
layout_flip: Option<LayoutFlip>, layout_flip: Option<Flip>,
resize_adjustments: Vec<Option<Rect>>, resize_adjustments: Vec<Option<Rect>>,
) -> Vec<Rect> { ) -> Vec<Rect> {
let mut a = *area; let mut a = *area;
@@ -313,37 +304,37 @@ fn recursive_fibonacci(
let (main_x, alt_x, alt_y, main_y); let (main_x, alt_x, alt_y, main_y);
match layout_flip { if let Some(flip) = layout_flip {
Some(flip) => match flip { match flip {
LayoutFlip::Horizontal => { Flip::Horizontal => {
main_x = resized.left + half_width + (half_width - half_resized_width); main_x = resized.left + half_width + (half_width - half_resized_width);
alt_x = resized.left; alt_x = resized.left;
alt_y = resized.top + half_resized_height; alt_y = resized.top + half_resized_height;
main_y = resized.top; main_y = resized.top;
} }
LayoutFlip::Vertical => { Flip::Vertical => {
main_y = resized.top + half_height + (half_height - half_resized_height); main_y = resized.top + half_height + (half_height - half_resized_height);
alt_y = resized.top; alt_y = resized.top;
main_x = resized.left; main_x = resized.left;
alt_x = resized.left + half_resized_width; alt_x = resized.left + half_resized_width;
} }
LayoutFlip::HorizontalAndVertical => { Flip::HorizontalAndVertical => {
main_x = resized.left + half_width + (half_width - half_resized_width); main_x = resized.left + half_width + (half_width - half_resized_width);
alt_x = resized.left; alt_x = resized.left;
main_y = resized.top + half_height + (half_height - half_resized_height); main_y = resized.top + half_height + (half_height - half_resized_height);
alt_y = resized.top; alt_y = resized.top;
} }
},
None => {
main_x = resized.left;
alt_x = resized.left + half_resized_width;
main_y = resized.top;
alt_y = resized.top + half_resized_height;
} }
} else {
main_x = resized.left;
alt_x = resized.left + half_resized_width;
main_y = resized.top;
alt_y = resized.top + half_resized_height;
} }
#[allow(clippy::if_not_else)]
if count == 0 { if count == 0 {
vec![] vec![]
} else if count == 1 { } else if count == 1 {

View File

@@ -1,3 +1,6 @@
#![warn(clippy::all, clippy::nursery, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
use std::str::FromStr; use std::str::FromStr;
use clap::ArgEnum; use clap::ArgEnum;
@@ -8,8 +11,8 @@ use strum::Display;
use strum::EnumString; use strum::EnumString;
pub use cycle_direction::CycleDirection; pub use cycle_direction::CycleDirection;
pub use layout::Flip;
pub use layout::Layout; pub use layout::Layout;
pub use layout::LayoutFlip;
pub use operation_direction::OperationDirection; pub use operation_direction::OperationDirection;
pub use rect::Rect; pub use rect::Rect;
@@ -39,7 +42,7 @@ pub enum SocketMessage {
AdjustContainerPadding(Sizing, i32), AdjustContainerPadding(Sizing, i32),
AdjustWorkspacePadding(Sizing, i32), AdjustWorkspacePadding(Sizing, i32),
ChangeLayout(Layout), ChangeLayout(Layout),
FlipLayout(LayoutFlip), FlipLayout(Flip),
// Monitor and Workspace Commands // Monitor and Workspace Commands
EnsureWorkspaces(usize, usize), EnsureWorkspaces(usize, usize),
NewWorkspace, NewWorkspace,
@@ -99,7 +102,8 @@ pub enum Sizing {
} }
impl Sizing { impl Sizing {
pub fn adjust_by(&self, value: i32, adjustment: i32) -> i32 { #[must_use]
pub const fn adjust_by(&self, value: i32, adjustment: i32) -> i32 {
match self { match self {
Sizing::Increase => value + adjustment, Sizing::Increase => value + adjustment,
Sizing::Decrease => { Sizing::Decrease => {

View File

@@ -4,8 +4,8 @@ use serde::Serialize;
use strum::Display; use strum::Display;
use strum::EnumString; use strum::EnumString;
use crate::Flip;
use crate::Layout; use crate::Layout;
use crate::LayoutFlip;
#[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ArgEnum)] #[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ArgEnum)]
#[strum(serialize_all = "snake_case")] #[strum(serialize_all = "snake_case")]
@@ -17,59 +17,46 @@ pub enum OperationDirection {
} }
impl OperationDirection { impl OperationDirection {
pub fn opposite(self) -> Self { #[must_use]
pub const fn opposite(self) -> Self {
match self { match self {
OperationDirection::Left => OperationDirection::Right, Self::Left => Self::Right,
OperationDirection::Right => OperationDirection::Left, Self::Right => Self::Left,
OperationDirection::Up => OperationDirection::Down, Self::Up => Self::Down,
OperationDirection::Down => OperationDirection::Up, Self::Down => Self::Up,
} }
} }
fn flip_direction( fn flip_direction(direction: Self, layout_flip: Option<Flip>) -> Self {
direction: &OperationDirection, layout_flip.map_or(direction, |flip| match direction {
layout_flip: Option<LayoutFlip>, Self::Left => match flip {
) -> OperationDirection { Flip::Horizontal | Flip::HorizontalAndVertical => Self::Right,
if let Some(flip) = layout_flip { Flip::Vertical => direction,
match direction { },
OperationDirection::Left => match flip { Self::Right => match flip {
LayoutFlip::Horizontal | LayoutFlip::HorizontalAndVertical => { Flip::Horizontal | Flip::HorizontalAndVertical => Self::Left,
OperationDirection::Right Flip::Vertical => direction,
} },
_ => *direction, Self::Up => match flip {
}, Flip::Vertical | Flip::HorizontalAndVertical => Self::Down,
OperationDirection::Right => match flip { Flip::Horizontal => direction,
LayoutFlip::Horizontal | LayoutFlip::HorizontalAndVertical => { },
OperationDirection::Left Self::Down => match flip {
} Flip::Vertical | Flip::HorizontalAndVertical => Self::Up,
_ => *direction, Flip::Horizontal => direction,
}, },
OperationDirection::Up => match flip { })
LayoutFlip::Vertical | LayoutFlip::HorizontalAndVertical => {
OperationDirection::Down
}
_ => *direction,
},
OperationDirection::Down => match flip {
LayoutFlip::Vertical | LayoutFlip::HorizontalAndVertical => {
OperationDirection::Up
}
_ => *direction,
},
}
} else {
*direction
}
} }
#[must_use]
pub fn is_valid( pub fn is_valid(
&self, self,
layout: Layout, layout: Layout,
layout_flip: Option<LayoutFlip>, layout_flip: Option<Flip>,
idx: usize, idx: usize,
len: usize, len: usize,
) -> bool { ) -> bool {
match OperationDirection::flip_direction(self, layout_flip) { match Self::flip_direction(self, layout_flip) {
OperationDirection::Up => match layout { OperationDirection::Up => match layout {
Layout::BSP => len > 2 && idx != 0 && idx != 1, Layout::BSP => len > 2 && idx != 0 && idx != 1,
Layout::Columns => false, Layout::Columns => false,
@@ -93,9 +80,10 @@ impl OperationDirection {
} }
} }
pub fn new_idx(&self, layout: Layout, layout_flip: Option<LayoutFlip>, idx: usize) -> usize { #[must_use]
match OperationDirection::flip_direction(self, layout_flip) { pub fn new_idx(self, layout: Layout, layout_flip: Option<Flip>, idx: usize) -> usize {
OperationDirection::Up => match layout { match Self::flip_direction(self, layout_flip) {
Self::Up => match layout {
Layout::BSP => { Layout::BSP => {
if idx % 2 == 0 { if idx % 2 == 0 {
idx - 1 idx - 1
@@ -106,11 +94,11 @@ impl OperationDirection {
Layout::Columns => unreachable!(), Layout::Columns => unreachable!(),
Layout::Rows => idx - 1, Layout::Rows => idx - 1,
}, },
OperationDirection::Down => match layout { Self::Down => match layout {
Layout::BSP | Layout::Rows => idx + 1, Layout::BSP | Layout::Rows => idx + 1,
Layout::Columns => unreachable!(), Layout::Columns => unreachable!(),
}, },
OperationDirection::Left => match layout { Self::Left => match layout {
Layout::BSP => { Layout::BSP => {
if idx % 2 == 0 { if idx % 2 == 0 {
idx - 2 idx - 2
@@ -121,7 +109,7 @@ impl OperationDirection {
Layout::Columns => idx - 1, Layout::Columns => idx - 1,
Layout::Rows => unreachable!(), Layout::Rows => unreachable!(),
}, },
OperationDirection::Right => match layout { Self::Right => match layout {
Layout::BSP | Layout::Columns => idx + 1, Layout::BSP | Layout::Columns => idx + 1,
Layout::Rows => unreachable!(), Layout::Rows => unreachable!(),
}, },

View File

@@ -12,7 +12,7 @@ pub struct Rect {
impl Default for Rect { impl Default for Rect {
fn default() -> Self { fn default() -> Self {
Rect { Self {
left: 0, left: 0,
top: 0, top: 0,
right: 0, right: 0,
@@ -23,7 +23,7 @@ impl Default for Rect {
impl From<RECT> for Rect { impl From<RECT> for Rect {
fn from(rect: RECT) -> Self { fn from(rect: RECT) -> Self {
Rect { Self {
left: rect.left, left: rect.left,
top: rect.top, top: rect.top,
right: rect.right - rect.left, right: rect.right - rect.left,
@@ -42,7 +42,8 @@ impl Rect {
} }
} }
pub fn contains_point(&self, point: (i32, i32)) -> bool { #[must_use]
pub const fn contains_point(&self, point: (i32, i32)) -> bool {
point.0 >= self.left point.0 >= self.left
&& point.0 <= self.left + self.right && point.0 <= self.left + self.right
&& point.1 >= self.top && point.1 >= self.top

View File

@@ -267,7 +267,7 @@ impl Window {
&& (allow_layered || !ex_style.contains(GwlExStyle::LAYERED)) && (allow_layered || !ex_style.contains(GwlExStyle::LAYERED))
|| managed_override || managed_override
{ {
return Ok(true) return Ok(true);
} else if event.is_some() { } else if event.is_some() {
tracing::debug!("ignoring (exe: {}, title: {})", exe_name, title); tracing::debug!("ignoring (exe: {}, title: {})", exe_name, title);
} }

View File

@@ -15,8 +15,8 @@ use serde::Serialize;
use uds_windows::UnixListener; use uds_windows::UnixListener;
use komorebi_core::CycleDirection; use komorebi_core::CycleDirection;
use komorebi_core::Flip;
use komorebi_core::Layout; use komorebi_core::Layout;
use komorebi_core::LayoutFlip;
use komorebi_core::OperationDirection; use komorebi_core::OperationDirection;
use komorebi_core::Rect; use komorebi_core::Rect;
use komorebi_core::Sizing; use komorebi_core::Sizing;
@@ -432,21 +432,21 @@ impl WindowManager {
// can flip them however they need to be flipped once the resizing has been done // can flip them however they need to be flipped once the resizing has been done
if let Some(flip) = workspace.layout_flip() { if let Some(flip) = workspace.layout_flip() {
match flip { match flip {
LayoutFlip::Horizontal => { Flip::Horizontal => {
if matches!(direction, OperationDirection::Left) if matches!(direction, OperationDirection::Left)
|| matches!(direction, OperationDirection::Right) || matches!(direction, OperationDirection::Right)
{ {
direction = direction.opposite(); direction = direction.opposite();
} }
} }
LayoutFlip::Vertical => { Flip::Vertical => {
if matches!(direction, OperationDirection::Up) if matches!(direction, OperationDirection::Up)
|| matches!(direction, OperationDirection::Down) || matches!(direction, OperationDirection::Down)
{ {
direction = direction.opposite(); direction = direction.opposite();
} }
} }
LayoutFlip::HorizontalAndVertical => direction = direction.opposite(), Flip::HorizontalAndVertical => direction = direction.opposite(),
} }
} }
@@ -746,7 +746,7 @@ impl WindowManager {
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn flip_layout(&mut self, layout_flip: LayoutFlip) -> Result<()> { pub fn flip_layout(&mut self, layout_flip: Flip) -> Result<()> {
tracing::info!("flipping layout"); tracing::info!("flipping layout");
let workspace = self.focused_workspace_mut()?; let workspace = self.focused_workspace_mut()?;
@@ -758,28 +758,28 @@ impl WindowManager {
} }
Some(current_layout_flip) => { Some(current_layout_flip) => {
match current_layout_flip { match current_layout_flip {
LayoutFlip::Horizontal => match layout_flip { Flip::Horizontal => match layout_flip {
LayoutFlip::Horizontal => workspace.set_layout_flip(None), Flip::Horizontal => workspace.set_layout_flip(None),
LayoutFlip::Vertical => workspace Flip::Vertical => {
.set_layout_flip(Option::from(LayoutFlip::HorizontalAndVertical)), workspace.set_layout_flip(Option::from(Flip::HorizontalAndVertical))
LayoutFlip::HorizontalAndVertical => workspace
.set_layout_flip(Option::from(LayoutFlip::HorizontalAndVertical)),
},
LayoutFlip::Vertical => match layout_flip {
LayoutFlip::Horizontal => workspace
.set_layout_flip(Option::from(LayoutFlip::HorizontalAndVertical)),
LayoutFlip::Vertical => workspace.set_layout_flip(None),
LayoutFlip::HorizontalAndVertical => workspace
.set_layout_flip(Option::from(LayoutFlip::HorizontalAndVertical)),
},
LayoutFlip::HorizontalAndVertical => match layout_flip {
LayoutFlip::Horizontal => {
workspace.set_layout_flip(Option::from(LayoutFlip::Vertical))
} }
LayoutFlip::Vertical => { Flip::HorizontalAndVertical => {
workspace.set_layout_flip(Option::from(LayoutFlip::Horizontal)) workspace.set_layout_flip(Option::from(Flip::HorizontalAndVertical))
} }
LayoutFlip::HorizontalAndVertical => workspace.set_layout_flip(None), },
Flip::Vertical => match layout_flip {
Flip::Horizontal => {
workspace.set_layout_flip(Option::from(Flip::HorizontalAndVertical))
}
Flip::Vertical => workspace.set_layout_flip(None),
Flip::HorizontalAndVertical => {
workspace.set_layout_flip(Option::from(Flip::HorizontalAndVertical))
}
},
Flip::HorizontalAndVertical => match layout_flip {
Flip::Horizontal => workspace.set_layout_flip(Option::from(Flip::Vertical)),
Flip::Vertical => workspace.set_layout_flip(Option::from(Flip::Horizontal)),
Flip::HorizontalAndVertical => workspace.set_layout_flip(None),
}, },
}; };
} }

View File

@@ -9,8 +9,8 @@ use getset::MutGetters;
use getset::Setters; use getset::Setters;
use serde::Serialize; use serde::Serialize;
use komorebi_core::Flip;
use komorebi_core::Layout; use komorebi_core::Layout;
use komorebi_core::LayoutFlip;
use komorebi_core::OperationDirection; use komorebi_core::OperationDirection;
use komorebi_core::Rect; use komorebi_core::Rect;
@@ -39,7 +39,7 @@ pub struct Workspace {
#[getset(get_copy = "pub", set = "pub")] #[getset(get_copy = "pub", set = "pub")]
layout: Layout, layout: Layout,
#[getset(get_copy = "pub", set = "pub")] #[getset(get_copy = "pub", set = "pub")]
layout_flip: Option<LayoutFlip>, layout_flip: Option<Flip>,
#[getset(get_copy = "pub", set = "pub")] #[getset(get_copy = "pub", set = "pub")]
workspace_padding: Option<i32>, workspace_padding: Option<i32>,
#[getset(get_copy = "pub", set = "pub")] #[getset(get_copy = "pub", set = "pub")]

View File

@@ -1,3 +1,6 @@
#![warn(clippy::all, clippy::nursery, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
use std::fs::File; use std::fs::File;
use std::io::BufRead; use std::io::BufRead;
use std::io::BufReader; use std::io::BufReader;
@@ -22,8 +25,8 @@ use bindings::Windows::Win32::UI::WindowsAndMessaging::SHOW_WINDOW_CMD;
use bindings::Windows::Win32::UI::WindowsAndMessaging::SW_RESTORE; use bindings::Windows::Win32::UI::WindowsAndMessaging::SW_RESTORE;
use komorebi_core::ApplicationIdentifier; use komorebi_core::ApplicationIdentifier;
use komorebi_core::CycleDirection; use komorebi_core::CycleDirection;
use komorebi_core::Flip;
use komorebi_core::Layout; use komorebi_core::Layout;
use komorebi_core::LayoutFlip;
use komorebi_core::OperationDirection; use komorebi_core::OperationDirection;
use komorebi_core::Sizing; use komorebi_core::Sizing;
use komorebi_core::SocketMessage; use komorebi_core::SocketMessage;
@@ -63,7 +66,7 @@ gen_enum_subcommand_args! {
Move: OperationDirection, Move: OperationDirection,
Stack: OperationDirection, Stack: OperationDirection,
CycleStack: CycleDirection, CycleStack: CycleDirection,
FlipLayout: LayoutFlip, FlipLayout: Flip,
SetLayout: Layout, SetLayout: Layout,
WatchConfiguration: BooleanState, WatchConfiguration: BooleanState,
FocusFollowsMouse: BooleanState FocusFollowsMouse: BooleanState
@@ -303,6 +306,7 @@ pub fn send_message(bytes: &[u8]) -> Result<()> {
Ok(stream.write_all(&*bytes)?) Ok(stream.write_all(&*bytes)?)
} }
#[allow(clippy::too_many_lines)]
fn main() -> Result<()> { fn main() -> Result<()> {
let opts: Opts = Opts::parse(); let opts: Opts = Opts::parse();
@@ -313,7 +317,7 @@ fn main() -> Result<()> {
let file = TailedFile::new(File::open(color_log)?); let file = TailedFile::new(File::open(color_log)?);
let locked = file.lock(); let locked = file.lock();
for line in locked.lines() { for line in locked.lines() {
println!("{}", line?) println!("{}", line?);
} }
} }
SubCommand::Focus(arg) => { SubCommand::Focus(arg) => {
@@ -449,7 +453,7 @@ fn main() -> Result<()> {
send_message(&*SocketMessage::ChangeLayout(arg.layout).as_bytes()?)?; send_message(&*SocketMessage::ChangeLayout(arg.layout).as_bytes()?)?;
} }
SubCommand::FlipLayout(arg) => { SubCommand::FlipLayout(arg) => {
send_message(&*SocketMessage::FlipLayout(arg.layout_flip).as_bytes()?)?; send_message(&*SocketMessage::FlipLayout(arg.flip).as_bytes()?)?;
} }
SubCommand::FocusMonitor(arg) => { SubCommand::FocusMonitor(arg) => {
send_message(&*SocketMessage::FocusMonitorNumber(arg.target).as_bytes()?)?; send_message(&*SocketMessage::FocusMonitorNumber(arg.target).as_bytes()?)?;
@@ -496,13 +500,13 @@ fn main() -> Result<()> {
Ok(incoming) => { Ok(incoming) => {
let stream = BufReader::new(incoming.0); let stream = BufReader::new(incoming.0);
for line in stream.lines() { for line in stream.lines() {
println!("{}", line?) println!("{}", line?);
} }
return Ok(()); return Ok(());
} }
Err(error) => { Err(error) => {
panic!("{}", error) panic!("{}", error);
} }
} }
} }