Skip to content

Commit

Permalink
Merge pull request #791 from gwenn/clippy
Browse files Browse the repository at this point in the history
Clippy
  • Loading branch information
gwenn authored Aug 10, 2024
2 parents 522b644 + 8e7b411 commit 67934ba
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 49 deletions.
8 changes: 4 additions & 4 deletions src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum Event {
impl Event {
/// See [`KeyEvent::normalize`]
pub(crate) fn normalize(mut self) -> Self {
if let Event::KeySeq(ref mut keys) = self {
if let Self::KeySeq(ref mut keys) = self {
for key in keys.iter_mut() {
*key = KeyEvent::normalize(*key);
}
Expand All @@ -32,7 +32,7 @@ impl Event {
/// Return `i`th key event
#[must_use]
pub fn get(&self, i: usize) -> Option<&KeyEvent> {
if let Event::KeySeq(ref ks) = self {
if let Self::KeySeq(ref ks) = self {
ks.get(i)
} else {
None
Expand All @@ -41,8 +41,8 @@ impl Event {
}

impl From<KeyEvent> for Event {
fn from(k: KeyEvent) -> Event {
Event::KeySeq(vec![k])
fn from(k: KeyEvent) -> Self {
Self::KeySeq(vec![k])
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,7 @@ impl Completer for FilenameCompleter {
/// Remove escape char
#[must_use]
pub fn unescape(input: &str, esc_char: Option<char>) -> Cow<'_, str> {
let esc_char = if let Some(c) = esc_char {
c
} else {
let Some(esc_char) = esc_char else {
return Borrowed(input);
};
if !input.chars().any(|c| c == esc_char) {
Expand Down Expand Up @@ -310,9 +308,7 @@ pub fn escape(
if n == 0 {
return input; // no need to escape
}
let esc_char = if let Some(c) = esc_char {
c
} else {
let Some(esc_char) = esc_char else {
if cfg!(windows) && quote == Quote::None {
input.insert(0, '"'); // force double quote
return input;
Expand Down Expand Up @@ -617,7 +613,7 @@ mod tests {
assert_eq!(Some(s), lcp);
}

let c3 = String::from("");
let c3 = String::new();
candidates.push(c3);
{
let lcp = super::longest_common_prefix(&candidates);
Expand Down
20 changes: 10 additions & 10 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum ReadlineError {
/// Unix Error from syscall
#[cfg(unix)]
Errno(nix::Error),
/// Error generated on WINDOW_BUFFER_SIZE_EVENT / SIGWINCH signal
/// Error generated on `WINDOW_BUFFER_SIZE_EVENT` / `SIGWINCH` signal
WindowResized,
/// Like Utf8Error on unix
#[cfg(windows)]
Expand Down Expand Up @@ -90,58 +90,58 @@ impl From<io::Error> for ReadlineError {
if err.kind() == io::ErrorKind::Interrupted {
if let Some(e) = err.get_ref() {
if e.downcast_ref::<WindowResizedError>().is_some() {
return ReadlineError::WindowResized;
return Self::WindowResized;
}
}
}
ReadlineError::Io(err)
Self::Io(err)
}
}

impl From<io::ErrorKind> for ReadlineError {
fn from(kind: io::ErrorKind) -> Self {
ReadlineError::Io(io::Error::from(kind))
Self::Io(io::Error::from(kind))
}
}

#[cfg(unix)]
impl From<nix::Error> for ReadlineError {
fn from(err: nix::Error) -> Self {
ReadlineError::Errno(err)
Self::Errno(err)
}
}

#[cfg(windows)]
impl From<char::DecodeUtf16Error> for ReadlineError {
fn from(err: char::DecodeUtf16Error) -> Self {
ReadlineError::Io(io::Error::new(io::ErrorKind::InvalidData, err))
Self::Io(io::Error::new(io::ErrorKind::InvalidData, err))
}
}

#[cfg(windows)]
impl From<std::string::FromUtf8Error> for ReadlineError {
fn from(err: std::string::FromUtf8Error) -> Self {
ReadlineError::Io(io::Error::new(io::ErrorKind::InvalidData, err))
Self::Io(io::Error::new(io::ErrorKind::InvalidData, err))
}
}

#[cfg(unix)]
impl From<fmt::Error> for ReadlineError {
fn from(err: fmt::Error) -> Self {
ReadlineError::Io(io::Error::new(io::ErrorKind::Other, err))
Self::Io(io::Error::new(io::ErrorKind::Other, err))
}
}

#[cfg(windows)]
impl From<clipboard_win::ErrorCode> for ReadlineError {
fn from(err: clipboard_win::ErrorCode) -> Self {
ReadlineError::SystemError(err)
Self::SystemError(err)
}
}

#[cfg(feature = "with-sqlite-history")]
impl From<rusqlite::Error> for ReadlineError {
fn from(err: rusqlite::Error) -> Self {
ReadlineError::SQLiteError(err)
Self::SQLiteError(err)
}
}
4 changes: 2 additions & 2 deletions src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub struct HistoryHinter {}

impl HistoryHinter {
/// Create a new `HistoryHinter`
pub fn new() -> HistoryHinter {
HistoryHinter::default()
pub fn new() -> Self {
Self::default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum Cmd {
Abort, // Miscellaneous Command
/// accept-line
///
/// See also AcceptOrInsertLine
/// See also `AcceptOrInsertLine`
AcceptLine,
/// beginning-of-history
BeginningOfHistory,
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,8 @@ fn complete_line<H: Helper>(

/// Completes the current hint
fn complete_hint_line<H: Helper>(s: &mut State<'_, '_, H>) -> Result<()> {
let hint = match s.hint.as_ref() {
Some(hint) => hint,
None => return Ok(()),
let Some(hint) = s.hint.as_ref() else {
return Ok(());
};
s.line.move_end();
if let Some(text) = hint.completion() {
Expand Down
4 changes: 2 additions & 2 deletions src/line_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,8 +1189,8 @@ mod test {
}

impl Listener {
fn new() -> Listener {
Listener { deleted_str: None }
fn new() -> Self {
Self { deleted_str: None }
}

fn assert_deleted_str_eq(&self, expected: &str) {
Expand Down
2 changes: 1 addition & 1 deletion src/sqlite_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl SQLiteHistory {

fn new(config: Config, path: Option<PathBuf>) -> Result<Self> {
let conn = conn(path.as_ref())?;
let mut sh = SQLiteHistory {
let mut sh = Self {
max_len: config.max_history_size(),
ignore_space: config.history_ignore_space(),
// not strictly consecutive...
Expand Down
2 changes: 1 addition & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod vi_insert;
fn init_editor(mode: EditMode, keys: &[KeyEvent]) -> DefaultEditor {
let config = Config::builder().edit_mode(mode).build();
let mut editor = DefaultEditor::with_config(config).unwrap();
editor.term.keys.extend(keys.iter().cloned());
editor.term.keys.extend(keys.iter().copied());
editor
}

Expand Down
4 changes: 2 additions & 2 deletions src/tty/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ impl Term for DummyTerminal {
bell_style: BellStyle,
_enable_bracketed_paste: bool,
_enable_signals: bool,
) -> Result<DummyTerminal> {
Ok(DummyTerminal {
) -> Result<Self> {
Ok(Self {
keys: vec![],
cursor: 0,
color_mode,
Expand Down
16 changes: 7 additions & 9 deletions src/tty/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Read for TtyIn {
let res = unsafe {
libc::read(
self.fd,
buf.as_mut_ptr() as *mut libc::c_void,
buf.as_mut_ptr().cast::<libc::c_void>(),
buf.len() as libc::size_t,
)
};
Expand Down Expand Up @@ -1021,9 +1021,7 @@ impl Renderer for PosixRenderer {
// we have to generate our own newline on line wrap
if end_pos.col == 0
&& end_pos.row > 0
&& !hint
.map(|h| h.ends_with('\n'))
.unwrap_or_else(|| line.ends_with('\n'))
&& !hint.map_or_else(|| line.ends_with('\n'), |h| h.ends_with('\n'))
{
self.buffer.push('\n');
}
Expand Down Expand Up @@ -1207,7 +1205,7 @@ struct SigWinCh {
}
impl SigWinCh {
#[cfg(not(feature = "signal-hook"))]
fn install_sigwinch_handler() -> Result<SigWinCh> {
fn install_sigwinch_handler() -> Result<Self> {
use nix::sys::signal;
let (pipe, pipe_write) = UnixStream::pair()?;
pipe.set_nonblocking(true)?;
Expand All @@ -1218,18 +1216,18 @@ impl SigWinCh {
signal::SigSet::empty(),
);
let original = unsafe { signal::sigaction(signal::SIGWINCH, &sigwinch)? };
Ok(SigWinCh {
Ok(Self {
pipe: pipe.into_raw_fd(),
original,
})
}

#[cfg(feature = "signal-hook")]
fn install_sigwinch_handler() -> Result<SigWinCh> {
fn install_sigwinch_handler() -> Result<Self> {
let (pipe, pipe_write) = UnixStream::pair()?;
pipe.set_nonblocking(true)?;
let id = signal_hook::low_level::pipe::register(libc::SIGWINCH, pipe_write)?;
Ok(SigWinCh {
Ok(Self {
pipe: pipe.into_raw_fd(),
id,
})
Expand Down Expand Up @@ -1440,6 +1438,7 @@ impl Term for PosixTerminal {
}

fn create_external_printer(&mut self) -> Result<ExternalPrinter> {
use nix::unistd::pipe;
if let Some(ref writer) = self.pipe_writer {
return Ok(ExternalPrinter {
writer: writer.clone(),
Expand All @@ -1450,7 +1449,6 @@ impl Term for PosixTerminal {
if self.unsupported || !self.is_input_tty() || !self.is_output_tty() {
return Err(nix::Error::ENOTTY.into());
}
use nix::unistd::pipe;
let (sender, receiver) = mpsc::sync_channel(1); // TODO validate: bound
let (r, w) = pipe()?;
let reader = Arc::new(Mutex::new((r.into(), receiver)));
Expand Down
12 changes: 5 additions & 7 deletions src/tty/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ pub struct ConsoleRawReader {
}

impl ConsoleRawReader {
fn create(conin: HANDLE, pipe_reader: Option<Rc<AsyncPipe>>) -> ConsoleRawReader {
ConsoleRawReader { conin, pipe_reader }
fn create(conin: HANDLE, pipe_reader: Option<Rc<AsyncPipe>>) -> Self {
Self { conin, pipe_reader }
}

fn select(&mut self) -> Result<Event> {
Expand Down Expand Up @@ -273,9 +273,7 @@ fn read_input(handle: HANDLE, max_count: u32) -> Result<KeyEvent> {
} else {
decode_utf16([surrogate, utf16].iter().copied()).next()
};
let rc = if let Some(rc) = orc {
rc
} else {
let Some(rc) = orc else {
return Err(error::ReadlineError::Eof);
};
let c = rc?;
Expand All @@ -296,10 +294,10 @@ pub struct ConsoleRenderer {
}

impl ConsoleRenderer {
fn new(conout: HANDLE, colors_enabled: bool, bell_style: BellStyle) -> ConsoleRenderer {
fn new(conout: HANDLE, colors_enabled: bool, bell_style: BellStyle) -> Self {
// Multi line editing is enabled by ENABLE_WRAP_AT_EOL_OUTPUT mode
let (cols, _) = get_win_size(conout);
ConsoleRenderer {
Self {
conout,
cols,
buffer: String::with_capacity(1024),
Expand Down

0 comments on commit 67934ba

Please sign in to comment.