From 25034f8d4d70b4048d7540264fc18461982406aa Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Fri, 3 Jun 2016 16:25:16 -0300 Subject: Add Clippy as dependency. This brings a lot of clean up on the code. --- src/desktop.rs | 23 ++++++++++------------- src/dotwm.rs | 9 ++++++++- src/event.rs | 30 +++++++++++++++--------------- src/main.rs | 9 +++++---- src/safe_x11/mod.rs | 5 +++-- src/safe_x11/window.rs | 12 ++++++------ src/socket/mod.rs | 13 +++++-------- src/socket/parser.rs | 8 ++++---- 8 files changed, 56 insertions(+), 53 deletions(-) (limited to 'src') diff --git a/src/desktop.rs b/src/desktop.rs index afe6a28..aac472f 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -69,18 +69,15 @@ impl Desktop { pub fn remove_window(&mut self, w: xlib::Window) { let pos = self.window_list.iter().position(|xw| xw.inner == w); - match pos { - Some(idx) => { - let new_idx = if idx == 0 { - if self.window_list.len() == 1 { usize::max_value() } else { 0 } - } else { - idx - 1 - }; - self.window_list.remove(idx); - self.cw_idx = new_idx; - self.focus_current_window(); - }, - None => (), + if let Some(idx) = pos { + let new_idx = if idx == 0 { + if self.window_list.len() == 1 { usize::max_value() } else { 0 } + } else { + idx - 1 + }; + self.window_list.remove(idx); + self.cw_idx = new_idx; + self.focus_current_window(); } } @@ -160,7 +157,7 @@ impl Desktop { (screen_width as i32, screen_height as i32), ]; - for w in self.window_list.iter() { + for w in &self.window_list { let attrs = w.attributes(); result.push((attrs.x, attrs.y)); result.push((attrs.x + attrs.width, attrs.y + attrs.height)); diff --git a/src/dotwm.rs b/src/dotwm.rs index 666deff..fd28765 100644 --- a/src/dotwm.rs +++ b/src/dotwm.rs @@ -51,6 +51,7 @@ pub enum WmAtom { } #[derive(Hash,Eq,PartialEq)] +#[allow(enum_variant_names)] pub enum NetAtom { NetSupported, NetFullscreen, @@ -74,7 +75,7 @@ pub struct DotWM { pub desktops: Vec, } -/// DotWM state. +/// `DotWM` state. impl DotWM { /// Create a state of the Dot Window Manager pub fn new() -> DotWM { @@ -218,6 +219,12 @@ impl DotWM { } } +impl Default for DotWM { + fn default() -> Self { + DotWM::new() + } +} + impl Drop for DotWM { fn drop(&mut self) { close_display(self.display); diff --git a/src/event.rs b/src/event.rs index b97686f..768e37a 100644 --- a/src/event.rs +++ b/src/event.rs @@ -56,21 +56,21 @@ pub enum Event { impl fmt::Debug for Event { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let s = match self { - &Event::Key(ev, pressed) => format!("Key({}, {})", XEvent::from(ev).get_type(), pressed), - &Event::Button(ev, pressed) => format!("Button({}, {})", XEvent::from(ev).get_type(), pressed), - &Event::Drag(ev) => format!("Drag({})", XEvent::from(ev).get_type()), - &Event::Generic(ev) => format!("Generic({})", XEvent::from(ev).get_type()), - &Event::Enter(ev) => format!("Enter({})", XEvent::from(ev).get_type()), - &Event::Leave(ev) => format!("Enter({})", XEvent::from(ev).get_type()), - &Event::Create(ev) => format!("Create({})", XEvent::from(ev).get_type()), - &Event::Destroy(ev) => format!("Destroy({})", XEvent::from(ev).get_type()), - &Event::Unmap(ev) => format!("Unmap({})", XEvent::from(ev).get_type()), - &Event::Map(ev) => format!("Map({})", XEvent::from(ev).get_type()), - &Event::Configure(ev) => format!("Configure({})", XEvent::from(ev).get_type()), - &Event::Expose(ev) => format!("Expose({})", XEvent::from(ev).get_type()), - &Event::Socket(_) => format!("Socket()"), - &Event::NoEvent => format!("NoEvent"), + let s = match *self { + Event::Key(ev, pressed) => format!("Key({}, {})", XEvent::from(ev).get_type(), pressed), + Event::Button(ev, pressed) => format!("Button({}, {})", XEvent::from(ev).get_type(), pressed), + Event::Drag(ev) => format!("Drag({})", XEvent::from(ev).get_type()), + Event::Generic(ev) => format!("Generic({})", XEvent::from(ev).get_type()), + Event::Enter(ev) => format!("Enter({})", XEvent::from(ev).get_type()), + Event::Leave(ev) => format!("Enter({})", XEvent::from(ev).get_type()), + Event::Create(ev) => format!("Create({})", XEvent::from(ev).get_type()), + Event::Destroy(ev) => format!("Destroy({})", XEvent::from(ev).get_type()), + Event::Unmap(ev) => format!("Unmap({})", XEvent::from(ev).get_type()), + Event::Map(ev) => format!("Map({})", XEvent::from(ev).get_type()), + Event::Configure(ev) => format!("Configure({})", XEvent::from(ev).get_type()), + Event::Expose(ev) => format!("Expose({})", XEvent::from(ev).get_type()), + Event::Socket(_) => String::from("Socket()"), + Event::NoEvent => String::from("NoEvent"), }; write!(f, "{}", s) diff --git a/src/main.rs b/src/main.rs index 4f292b6..b1873f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ // See LICENSE file for copyright and license details. +#![cfg_attr(feature="clippy", feature(plugin))] +#![cfg_attr(feature="clippy", plugin(clippy))] extern crate libc; extern crate x11; extern crate unix_socket; @@ -32,9 +34,8 @@ const SOCKET_PATH: &'static str = "./dotwm.sock"; fn run_autostart() { let paths = &["./autostart", "/home/matias/.config/dotwm/autostart"]; for path in paths { - match exec_cmd(path, &[]) { - Ok(_) => break, - Err(_) => (), + if exec_cmd(path, &[]).is_ok() { + break; } } } @@ -86,7 +87,7 @@ fn main() { s.read_to_string(&mut buf).unwrap(); for line in buf.lines() { - let res = parser::parse(&line); + let res = parser::parse(line); match res { Ok(pcmd) => { pcmd.handle(&mut dotwm, &mut bindings); diff --git a/src/safe_x11/mod.rs b/src/safe_x11/mod.rs index 3d08a4e..613424a 100644 --- a/src/safe_x11/mod.rs +++ b/src/safe_x11/mod.rs @@ -74,13 +74,13 @@ pub fn close_display(display: *mut Display) { unsafe { xlib::XCloseDisplay(display); } } -/// Get the file descriptor for the XServer +/// Get the file descriptor for the `XServer` pub fn x11_fd(display: *mut Display) -> c_int { unsafe { XConnectionNumber(display) } } /// Get the next event from the xserver. This call will not block forever -/// (like XNextEvent), instead it will wait for a certain time (1 second +/// (like `XNextEvent`), instead it will wait for a certain time (1 second /// for now, but it can change) so we're not blocking the socket interface /// on the main thread. pub fn next_xevent(display: *mut Display) -> XEvent { @@ -124,6 +124,7 @@ pub fn root_window(display: *mut Display) -> Window { /// 0, 0 /// ); /// ``` +#[allow(too_many_arguments)] pub fn grab_button(display: *mut Display, button: u32, modifiers: u32, owner_events: bool, event_mask: i64, pointer_mode: i32, keyboard_mode: i32, confine_to: Window, diff --git a/src/safe_x11/window.rs b/src/safe_x11/window.rs index 6d1e54e..eac4dbd 100644 --- a/src/safe_x11/window.rs +++ b/src/safe_x11/window.rs @@ -260,7 +260,7 @@ impl XWindow { .next() { let diff = attrs.width - (*v - attrs.x); if *v - attrs.x > 0 { - let _ = self.resize_to(attrs.width - diff, attrs.height); + self.resize_to(attrs.width - diff, attrs.height); } } } @@ -273,7 +273,7 @@ impl XWindow { .skip_while(|&a| *a <= win_bound) .next() { let new_width = *v - attrs.x; - let _ = self.resize_to(new_width, attrs.height); + self.resize_to(new_width, attrs.height); } } @@ -285,7 +285,7 @@ impl XWindow { .next() { let diff = attrs.height - (*v - attrs.y); if *v - attrs.y > 0 { - let _ = self.resize_to(attrs.width, attrs.height - diff); + self.resize_to(attrs.width, attrs.height - diff); } } } @@ -298,7 +298,7 @@ impl XWindow { .skip_while(|&a| *a <= win_bound) .next() { let new_height = *v - attrs.y; - let _ = self.resize_to(attrs.width, new_height); + self.resize_to(attrs.width, new_height); } } @@ -363,8 +363,8 @@ impl XWindow { let gc = xlib::XCreateGC(self.display, self.inner, 0, ptr::null_mut()); xlib::XSetForeground(self.display, gc, XWhitePixel(self.display, 0)); xlib::XSetBackground(self.display, gc, XBlackPixel(self.display, 0)); - println!("Loading fixed"); - let font: xlib::XFontStruct = ptr::read(xlib::XLoadQueryFont(self.display, CString::new("fixed").unwrap().as_ptr())); + let cstring = CString::new("fixed").unwrap(); + let font: xlib::XFontStruct = ptr::read(xlib::XLoadQueryFont(self.display, cstring.as_ptr())); xlib::XSetFont(self.display, gc, font.fid); xlib::XClearWindow(self.display, self.inner); XDrawString(self.display, self.inner, gc, diff --git a/src/socket/mod.rs b/src/socket/mod.rs index 66dae16..c29db2b 100644 --- a/src/socket/mod.rs +++ b/src/socket/mod.rs @@ -25,14 +25,11 @@ pub struct ParsedCmd<'a> { impl<'a> ParsedCmd<'a> { pub fn handle(self, wm: &mut DotWM, bindings: &mut BindingHash) { - match self.f { - FnType::Bind => { - let modifier: u32 = self.modifiers.iter() - .fold(0, |acc, x| acc | x ); - add_binding(wm, bindings, - self.key, modifier, self.func, &self.args); - }, - _ => (), + if self.f == FnType::Bind { + let modifier: u32 = self.modifiers.iter() + .fold(0, |acc, x| acc | x ); + add_binding(wm, bindings, + self.key, modifier, self.func, &self.args); } } } diff --git a/src/socket/parser.rs b/src/socket/parser.rs index 30487d8..e475962 100644 --- a/src/socket/parser.rs +++ b/src/socket/parser.rs @@ -36,7 +36,7 @@ fn modifier_from<'a>(s: &'a str) -> Result { fn modifiers<'a>(s: &'a str) -> Result, &'static str> { let mut result = vec![]; - for smod in s.split("-") { + for smod in s.split('-') { let modifier = simple_try!(modifier_from(smod)); result.push(modifier); } @@ -128,8 +128,8 @@ pub fn parse<'a>(input: &'a str) -> Result, &'static str> { match args.first() { Some(cmd) => { - match cmd { - &"bind" => { + match *cmd { + "bind" => { if args.len() > 2 { let modifiers = simple_try!(modifiers(args[1])); let key = simple_try!(key(args[2])); @@ -147,7 +147,7 @@ pub fn parse<'a>(input: &'a str) -> Result, &'static str> { Err("missing arguments") } }, - &"exec" => { + "exec" => { Ok(ParsedCmd { f: FnType::Exec, modifiers: vec![], -- cgit v1.2.3-70-g09d2