Repository: kkawakam/rustyline
Pull request #337 (2020-02-22 10:32:20)

Clippy + rustfmt


Patch:
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index 6e2926c..e5de5f1 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -17,7 +17,7 @@ jobs:
         os: [ubuntu-latest, windows-latest]
 
     steps:
-    - uses: actions/checkout@v1
+    - uses: actions/checkout@v2
     - uses: actions-rs/toolchain@v1
       with:
         default: true
diff --git a/src/edit.rs b/src/edit.rs
index bdcb0f0..3082506 100644
--- a/src/edit.rs
+++ b/src/edit.rs
@@ -225,9 +225,7 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
             let result = validator.validate(&mut ValidationContext::new(self))?;
             let corrected = self.changes.borrow_mut().end();
             let validated = match result {
-                ValidationResult::Incomplete => {
-                    false
-                }
+                ValidationResult::Incomplete => false,
                 ValidationResult::Valid(msg) => {
                     // Accept the line regardless of where the cursor is.
                     if corrected || self.has_hint() || msg.is_some() {
diff --git a/src/error.rs b/src/error.rs
index 4a6f977..f19600b 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,6 +1,4 @@
 //! Contains error type for handling I/O and Errno errors
-#[cfg(unix)]
-use nix;
 #[cfg(windows)]
 use std::char;
 use std::error;
diff --git a/src/history.rs b/src/history.rs
index b12a0bd..c59cf6c 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -1,7 +1,5 @@
 //! History API
 
-#[cfg(unix)]
-use libc;
 use std::collections::vec_deque;
 use std::collections::VecDeque;
 use std::fs::File;
diff --git a/src/keymap.rs b/src/keymap.rs
index fb99971..ffe0700 100644
--- a/src/keymap.rs
+++ b/src/keymap.rs
@@ -578,16 +578,17 @@ impl InputState {
             }
         }
         let cmd = match key {
-            KeyPress::Char('$') |
-            KeyPress::End => Cmd::Move(Movement::EndOfLine),
-            KeyPress::Char('.') => { // vi-redo (repeat last command)
+            KeyPress::Char('$') | KeyPress::End => Cmd::Move(Movement::EndOfLine),
+            KeyPress::Char('.') => {
+                // vi-redo (repeat last command)
                 if no_num_args {
                     self.last_cmd.redo(None, wrt)
                 } else {
                     self.last_cmd.redo(Some(n), wrt)
                 }
-            },
-            // TODO KeyPress::Char('%') => Cmd::???, Move to the corresponding opening/closing bracket
+            }
+            // TODO KeyPress::Char('%') => Cmd::???, Move to the corresponding opening/closing
+            // bracket
             KeyPress::Char('0') => Cmd::Move(Movement::BeginningOfLine),
             KeyPress::Char('^') => Cmd::Move(Movement::ViFirstPrint),
             KeyPress::Char('a') => {
@@ -615,14 +616,11 @@ impl InputState {
                 self.input_mode = InputMode::Insert;
                 Cmd::Replace(Movement::EndOfLine, None)
             }
-            KeyPress::Char('d') => {
-                match self.vi_cmd_motion(rdr, wrt, key, n)? {
-                    Some(mvt) => Cmd::Kill(mvt),
-                    None => Cmd::Unknown,
-                }
-            }
-            KeyPress::Char('D') |
-            KeyPress::Ctrl('K') => Cmd::Kill(Movement::EndOfLine),
+            KeyPress::Char('d') => match self.vi_cmd_motion(rdr, wrt, key, n)? {
+                Some(mvt) => Cmd::Kill(mvt),
+                None => Cmd::Unknown,
+            },
+            KeyPress::Char('D') | KeyPress::Ctrl('K') => Cmd::Kill(Movement::EndOfLine),
             KeyPress::Char('e') => Cmd::Move(Movement::ForwardWord(n, At::BeforeEnd, Word::Vi)),
             KeyPress::Char('E') => Cmd::Move(Movement::ForwardWord(n, At::BeforeEnd, Word::Big)),
             KeyPress::Char('i') => {
@@ -645,18 +643,14 @@ impl InputState {
                     None => Cmd::Unknown,
                 }
             }
-            KeyPress::Char(';') => {
-                match self.last_char_search {
-                    Some(cs) => Cmd::Move(Movement::ViCharSearch(n, cs)),
-                    None => Cmd::Noop,
-                }
-            }
-            KeyPress::Char(',') => {
-                match self.last_char_search {
-                    Some(ref cs) => Cmd::Move(Movement::ViCharSearch(n, cs.opposite())),
-                    None => Cmd::Noop,
-                }
-            }
+            KeyPress::Char(';') => match self.last_char_search {
+                Some(cs) => Cmd::Move(Movement::ViCharSearch(n, cs)),
+                None => Cmd::Noop,
+            },
+            KeyPress::Char(',') => match self.last_char_search {
+                Some(ref cs) => Cmd::Move(Movement::ViCharSearch(n, cs.opposite())),
+                None => Cmd::Noop,
+            },
             // TODO KeyPress::Char('G') => Cmd::???, Move to the history line n
             KeyPress::Char('p') => Cmd::Yank(n, Anchor::After), // vi-put
             KeyPress::Char('P') => Cmd::Yank(n, Anchor::Before), // vi-put
@@ -686,30 +680,26 @@ impl InputState {
             }
             KeyPress::Char('u') => Cmd::Undo(n),
             // KeyPress::Char('U') => Cmd::???, // revert-line
-            KeyPress::Char('w') => Cmd::Move(Movement::ForwardWord(n, At::Start, Word::Vi)), // vi-next-word
-            KeyPress::Char('W') => Cmd::Move(Movement::ForwardWord(n, At::Start, Word::Big)), // vi-next-word
-            KeyPress::Char('x') => Cmd::Kill(Movement::ForwardChar(n)), // vi-delete: TODO move backward if eol
+            KeyPress::Char('w') => Cmd::Move(Movement::ForwardWord(n, At::Start, Word::Vi)), /* vi-next-word */
+            KeyPress::Char('W') => Cmd::Move(Movement::ForwardWord(n, At::Start, Word::Big)), /* vi-next-word */
+            KeyPress::Char('x') => Cmd::Kill(Movement::ForwardChar(n)), /* vi-delete: TODO move
+                                                                          * backward if eol */
             KeyPress::Char('X') => Cmd::Kill(Movement::BackwardChar(n)), // vi-rubout
-            KeyPress::Char('y') => {
-                match self.vi_cmd_motion(rdr, wrt, key, n)? {
-                    Some(mvt) => Cmd::ViYankTo(mvt),
-                    None => Cmd::Unknown,
-                }
-            }
+            KeyPress::Char('y') => match self.vi_cmd_motion(rdr, wrt, key, n)? {
+                Some(mvt) => Cmd::ViYankTo(mvt),
+                None => Cmd::Unknown,
+            },
             // KeyPress::Char('Y') => Cmd::???, // vi-yank-to
-            KeyPress::Char('h') |
-            KeyPress::Ctrl('H') |
-            KeyPress::Backspace => Cmd::Move(Movement::BackwardChar(n)),
+            KeyPress::Char('h') | KeyPress::Ctrl('H') | KeyPress::Backspace => {
+                Cmd::Move(Movement::BackwardChar(n))
+            }
             KeyPress::Ctrl('G') => Cmd::Abort,
-            KeyPress::Char('l') |
-            KeyPress::Char(' ') => Cmd::Move(Movement::ForwardChar(n)),
+            KeyPress::Char('l') | KeyPress::Char(' ') => Cmd::Move(Movement::ForwardChar(n)),
             KeyPress::Ctrl('L') => Cmd::ClearScreen,
-            KeyPress::Char('+') |
-            KeyPress::Char('j') => Cmd::Move(Movement::LineDown(n)),
+            KeyPress::Char('+') | KeyPress::Char('j') => Cmd::Move(Movement::LineDown(n)),
             // TODO: move to the start of the line.
             KeyPress::Ctrl('N') => Cmd::NextHistory,
-            KeyPress::Char('-') |
-            KeyPress::Char('k') => Cmd::Move(Movement::LineUp(n)),
+            KeyPress::Char('-') | KeyPress::Char('k') => Cmd::Move(Movement::LineUp(n)),
             // TODO: move to the start of the line.
             KeyPress::Ctrl('P') => Cmd::PreviousHistory,
             KeyPress::Ctrl('R') => {
diff --git a/src/lib.rs b/src/lib.rs
index 88e6809..015932e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -656,7 +656,7 @@ fn readline_edit<H: Helper>(
                 s.refresh_line()?;
                 continue;
             }
-            Cmd::Noop | _ => {
+            _ => {
                 // Ignore the character typed.
             }
         }
diff --git a/src/line_buffer.rs b/src/line_buffer.rs
index 2a2a6b8..eebd59f 100644
--- a/src/line_buffer.rs
+++ b/src/line_buffer.rs
@@ -489,15 +489,16 @@ impl LineBuffer {
     pub fn move_to_line_up(&mut self, n: RepeatCount) -> bool {
         match self.buf[..self.pos].rfind('\n') {
             Some(off) => {
-                let column = self.buf[off+1..self.pos].graphemes(true).count();
+                let column = self.buf[off + 1..self.pos].graphemes(true).count();
 
                 let mut dest_start = self.buf[..off].rfind('\n').unwrap_or(0);
                 let mut dest_end = off;
                 for _ in 1..n {
-                    if dest_start == 0 { break; }
-                    dest_end = dest_start-1;
-                    dest_start = self.buf[..dest_end].rfind('\n')
-                        .unwrap_or(0);
+                    if dest_start == 0 {
+                        break;
+                    }
+                    dest_end = dest_start - 1;
+                    dest_start = self.buf[..dest_end].rfind('\n').unwrap_or(0);
                 }
 
                 self.pos = self.buf[dest_start..dest_end]
@@ -505,9 +506,9 @@ impl LineBuffer {
                     .nth(column)
                     .map(|(idx, _)| dest_start + idx)
                     .unwrap_or(off); // if there's no enough columns
-                return true;
+                true
             }
-            None => return false,
+            None => false,
         }
     }
 
@@ -516,21 +517,23 @@ impl LineBuffer {
     /// Fails if the cursor is on the first line
     fn n_lines_up(&self, n: RepeatCount) -> Option<(usize, usize)> {
         let mut start = if let Some(off) = self.buf[..self.pos].rfind('\n') {
-            off+1
+            off + 1
         } else {
             return None;
         };
-        let end = self.buf[self.pos..].find('\n')
-            .map(|x| self.pos + x + 1).unwrap_or(self.buf.len());
+        let end = self.buf[self.pos..]
+            .find('\n')
+            .map(|x| self.pos + x + 1)
+            .unwrap_or_else(|| self.buf.len());
         for _ in 0..n {
-            if let Some(off) = self.buf[..start-1].rfind('\n') {
-                start = off+1
+            if let Some(off) = self.buf[..start - 1].rfind('\n') {
+                start = off + 1
             } else {
                 start = 0;
                 break;
             }
         }
-        return Some((start, end));
+        Some((start, end))
     }
 
     /// N lines down starting from the current one
@@ -551,27 +554,29 @@ impl LineBuffer {
                 break;
             };
         }
-        return Some((start, end));
+        Some((start, end))
     }
 
     /// Moves the cursor to the same column in the line above
     pub fn move_to_line_down(&mut self, n: RepeatCount) -> bool {
         match self.buf[self.pos..].find('\n') {
             Some(off) => {
-                let line_start = self.buf[..self.pos].rfind('\n')
-                    .unwrap_or(0);
-                let column = self.buf[line_start..self.pos]
-                    .graphemes(true).count();
-                let mut dest_start = self.pos + off+1;
-                let mut dest_end = self.buf[dest_start..].find('\n')
+                let line_start = self.buf[..self.pos].rfind('\n').unwrap_or(0);
+                let column = self.buf[line_start..self.pos].graphemes(true).count();
+                let mut dest_start = self.pos + off + 1;
+                let mut dest_end = self.buf[dest_start..]
+                    .find('\n')
                     .map(|v| dest_start + v)
-                    .unwrap_or(self.buf.len());
+                    .unwrap_or_else(|| self.buf.len());
                 for _ in 1..n {
-                    if dest_end == self.buf.len() { break; }
-                    dest_start = dest_end+1;
-                    dest_end = self.buf[dest_start..].find('\n')
+                    if dest_end == self.buf.len() {
+                        break;
+                    }
+                    dest_start = dest_end + 1;
+                    dest_end = self.buf[dest_start..]
+                        .find('\n')
                         .map(|v| dest_start + v)
-                        .unwrap_or(self.buf.len());
+                        .unwrap_or_else(|| self.buf.len());
                 }
                 self.pos = self.buf[dest_start..dest_end]
                     .grapheme_indices(true)
@@ -579,9 +584,9 @@ impl LineBuffer {
                     .map(|(idx, _)| dest_start + idx)
                     .unwrap_or(dest_end); // if there's no enough columns
                 debug_assert!(self.pos <= self.buf.len());
-                return true;
+                true
             }
-            None => return false,
+            None => false,
         }
     }
 
diff --git a/src/tty/unix.rs b/src/tty/unix.rs
index f165cf9..a39a0bd 100644
--- a/src/tty/unix.rs
+++ b/src/tty/unix.rs
@@ -1,14 +1,11 @@
 //! Unix specific definitions
-use std;
 use std::cmp::Ordering;
 use std::io::{self, Read, Write};
 use std::os::unix::io::{AsRawFd, RawFd};
 use std::sync;
 use std::sync::atomic;
 
-use libc;
 use log::{debug, warn};
-use nix;
 use nix::poll::{self, PollFlags};
 use nix::sys::signal;
 use nix::sys::termios;


Test patch:
diff --git a/src/test/vi_cmd.rs b/src/test/vi_cmd.rs
index c04ef85..02c5c09 100644
--- a/src/test/vi_cmd.rs
+++ b/src/test/vi_cmd.rs
@@ -426,10 +426,7 @@ fn l() {
 
 #[test]
 fn j() {
-    for key in &[
-        KeyPress::Char('j'),
-        KeyPress::Char('+'),
-    ] {
+    for key in &[KeyPress::Char('j'), KeyPress::Char('+')] {
         assert_cursor(
             EditMode::Vi,
             ("Hel", "lo,\nworld!"),
@@ -455,10 +452,7 @@ fn j() {
 
 #[test]
 fn k() {
-    for key in &[
-        KeyPress::Char('k'),
-        KeyPress::Char('-'),
-    ] {
+    for key in &[KeyPress::Char('k'), KeyPress::Char('-')] {
         assert_cursor(
             EditMode::Vi,
             ("Hello,\nworl", "d!"),
@@ -491,9 +485,7 @@ fn k() {
 
 #[test]
 fn ctrl_n() {
-    for key in &[
-        KeyPress::Ctrl('N'),
-    ] {
+    for key in &[KeyPress::Ctrl('N')] {
         assert_history(
             EditMode::Vi,
             &["line1", "line2"],
@@ -512,9 +504,7 @@ fn ctrl_n() {
 
 #[test]
 fn ctrl_p() {
-    for key in &[
-        KeyPress::Ctrl('P'),
-    ] {
+    for key in &[KeyPress::Ctrl('P')] {
         assert_history(
             EditMode::Vi,
             &["line1"],