From 372754abf8dfa8de74f23b9a25470790c3452f8c Mon Sep 17 00:00:00 2001 From: Nick Drozd Date: Tue, 11 Jun 2019 19:50:04 -0500 Subject: [PATCH] Yet more Clippy There are a few changes packed into this one. - Cut unnecessary negations (mostly shuffling if/else blocks) - Use isize::min_value() instead of std::isize::MIN, etc - https://github.com/rust-lang/rust-clippy/issues/2380 - Rename some shadowing variables - Cut some unnecessary references - Use ::default instead of Default::default - Update inclusive range syntax - Cut open import - Self - dyn trait --- rust_src/remacs-lib/files.rs | 6 +++--- rust_src/src/buffers.rs | 4 ++-- rust_src/src/character.rs | 2 +- rust_src/src/cmds.rs | 2 +- rust_src/src/decompress.rs | 2 +- rust_src/src/dired_unix.rs | 7 ++---- rust_src/src/editfns.rs | 14 ++++++------ rust_src/src/filelock.rs | 21 +++++++++--------- rust_src/src/fns.rs | 6 +++--- rust_src/src/fonts.rs | 8 +++---- rust_src/src/frames.rs | 6 +++--- rust_src/src/keyboard.rs | 6 +++--- rust_src/src/lread.rs | 6 +++--- rust_src/src/marker.rs | 15 +++++++------ rust_src/src/multibyte.rs | 42 +++++++++++++++++++----------------- rust_src/src/numbers.rs | 2 +- rust_src/src/remacs_sys.rs | 2 +- rust_src/src/symbols.rs | 22 +++++++++---------- rust_src/src/time.rs | 2 +- rust_src/src/windows.rs | 20 ++++++++--------- 20 files changed, 97 insertions(+), 98 deletions(-) diff --git a/rust_src/remacs-lib/files.rs b/rust_src/remacs-lib/files.rs index 5061b67fedc..a56f133788c 100644 --- a/rust_src/remacs-lib/files.rs +++ b/rust_src/remacs-lib/files.rs @@ -76,9 +76,9 @@ fn generate_temporary_filename(name: &mut String) { } for byte in bytes.iter_mut() { *byte = match *byte % 62 { - v @ 0...9 => v + b'0', - v @ 10...35 => v - 10 + b'a', - v @ 36...61 => v - 36 + b'A', + v @ 0..=9 => v + b'0', + v @ 10..=35 => v - 10 + b'a', + v @ 36..=61 => v - 36 + b'A', _ => unreachable!(), } } diff --git a/rust_src/src/buffers.rs b/rust_src/src/buffers.rs index 009727afcbc..859a8cfabe5 100644 --- a/rust_src/src/buffers.rs +++ b/rust_src/src/buffers.rs @@ -180,7 +180,7 @@ impl LispBufferRef { let mut name: LispStringRef = copy_sequence(name.into()).force_string(); name.set_intervals(ptr::null_mut()); b.name_ = name.into(); - b.undo_list_ = if name.byte_at(0) != b' ' { Qnil } else { Qt }; + b.undo_list_ = (name.byte_at(0) == b' ').into(); b.reset(); b.reset_local_variables(true); @@ -1409,7 +1409,7 @@ fn get_truename_buffer_1(filename: LispSymbolOrString) -> LispObject { /// for positions far away from POS). #[lisp_fn] pub fn overlay_recenter(pos: LispNumber) { - let p = clip_to_bounds(std::isize::MIN, pos.to_fixnum(), std::isize::MAX); + let p = clip_to_bounds(isize::min_value(), pos.to_fixnum(), isize::max_value()); unsafe { recenter_overlay_lists(ThreadState::current_buffer_unchecked().as_mut(), p); } diff --git a/rust_src/src/character.rs b/rust_src/src/character.rs index c9b4b8f6ddc..7c623c99f70 100644 --- a/rust_src/src/character.rs +++ b/rust_src/src/character.rs @@ -88,7 +88,7 @@ pub fn multibyte_char_to_unibyte(ch: Codepoint) -> EmacsInt { // a latin1 char, so let's let it slide. ch.into() } else { - ch.to_byte8().map(EmacsInt::from).unwrap_or(-1) + ch.to_byte8().map_or(-1, EmacsInt::from) } } diff --git a/rust_src/src/cmds.rs b/rust_src/src/cmds.rs index ef7708e4711..fc90ba3762e 100644 --- a/rust_src/src/cmds.rs +++ b/rust_src/src/cmds.rs @@ -436,7 +436,7 @@ fn internal_self_insert(mut c: Codepoint, n: usize) -> EmacsInt { } else if n > 1 { let mut strn: Vec = match n.checked_mul(len) { Some(size_bytes) => Vec::with_capacity(size_bytes), - None => unsafe { memory_full(std::usize::MAX) }, + None => unsafe { memory_full(usize::max_value()) }, }; for _ in 0..n { strn.extend_from_slice(&str[0..len]); diff --git a/rust_src/src/decompress.rs b/rust_src/src/decompress.rs index c7f4a9fabbd..4957a1364f0 100644 --- a/rust_src/src/decompress.rs +++ b/rust_src/src/decompress.rs @@ -22,7 +22,7 @@ pub fn zlib_available_p() -> bool { true } -fn create_buffer_decoder<'a>(buffer: &'a [u8]) -> Box { +fn create_buffer_decoder<'a>(buffer: &'a [u8]) -> Box { let magic_number = buffer[0]; match magic_number { diff --git a/rust_src/src/dired_unix.rs b/rust_src/src/dired_unix.rs index 110a5d25b5d..81b8024c323 100644 --- a/rust_src/src/dired_unix.rs +++ b/rust_src/src/dired_unix.rs @@ -92,9 +92,9 @@ impl StringExt for String { // So here make sure just one '/' between dir&file. let last_char = dir.as_str().chars().last().unwrap(); if last_char == '/' { - dir + &self + dir + self } else { - dir + "/" + &self + dir + "/" + self } } } @@ -245,9 +245,6 @@ fn read_dir(dname: &str, fnames: &mut Vec, match_re: Option) fnames.push(dotdot); } - // NOTE: The allow can be dropped when the issue - // https://github.com/rust-lang/rust-clippy/issues/3913 is resolved - #[allow(clippy::identity_conversion)] for fname in fs::read_dir(dir_p)? { let fname = fname?; let f_enc = match fname.file_name().into_string() { diff --git a/rust_src/src/editfns.rs b/rust_src/src/editfns.rs index 665cf9483d9..acc63e7f730 100644 --- a/rust_src/src/editfns.rs +++ b/rust_src/src/editfns.rs @@ -458,15 +458,13 @@ pub fn byte_to_string(byte: EmacsInt) -> LispObject { /// Return the first character in STRING. #[lisp_fn] pub fn string_to_char(string: LispStringRef) -> EmacsInt { - if !string.is_empty() { - if string.is_multibyte() { - let (cp, _) = multibyte_char_at(string.as_slice()); - EmacsInt::from(cp) - } else { - EmacsInt::from(string.byte_at(0)) - } - } else { + if string.is_empty() { 0 + } else if string.is_multibyte() { + let (cp, _) = multibyte_char_at(string.as_slice()); + EmacsInt::from(cp) + } else { + EmacsInt::from(string.byte_at(0)) } } diff --git a/rust_src/src/filelock.rs b/rust_src/src/filelock.rs index 4bceab7913b..768c3603e8e 100644 --- a/rust_src/src/filelock.rs +++ b/rust_src/src/filelock.rs @@ -25,7 +25,6 @@ use crate::{ remacs_sys::{Qnil, Qstringp, Qt}, threads::ThreadState, }; -use LockState::*; /// An arbitrary limit on lock contents length when it is stored in the /// contents of the lock file. 8 K should be plenty big enough in practice. @@ -47,7 +46,7 @@ struct LockInfo { impl LockInfo { /// Parses a string like `USER@HOST.PID:BOOT_TIME` into a [`LockInfo`]. /// Returns [`None`] if the parse fails. - fn parse(data: &str) -> Option { + fn parse(data: &str) -> Option { // Treat "\357\200\242" (U+F022 in UTF-8) as if it were ":" (Bug#24656). // This works around a bug in the Linux CIFS kernel client, which can // mistakenly transliterate ':' to U+F022 in symlink contents. @@ -86,7 +85,7 @@ impl LockInfo { ) }; - Some(LockInfo { + Some(Self { user, host, pid, @@ -244,22 +243,22 @@ fn current_lock_owner(path: &Path) -> Result { if info.host.as_bytes() == system_name().as_slice() { if info.pid == std::process::id() as i32 { // We own it. - LockedByUs + LockState::LockedByUs } else if process_exists(info.pid) && boot_time_within_one_second(&info) { // An existing process on this machine owns it. - LockedBy(info) + LockState::LockedBy(info) } else { // The owner process is dead or has a strange pid, so try to // zap the lockfile. remove_file(path)?; - NotLocked + LockState::NotLocked } } else { - LockedBy(info) + LockState::LockedBy(info) } } - None => NotLocked, + None => LockState::NotLocked, }; Ok(result) @@ -329,9 +328,9 @@ pub fn file_locked_p(filename: LispStringRef) -> LispObject { let path = make_lock_name(expand_file_name(filename, None)); match current_lock_owner(&path) { - Ok(NotLocked) | Err(_) => Qnil, - Ok(LockedByUs) => Qt, - Ok(LockedBy(info)) => LispObject::from(info.user.as_str()), + Ok(LockState::NotLocked) | Err(_) => Qnil, + Ok(LockState::LockedByUs) => Qt, + Ok(LockState::LockedBy(info)) => LispObject::from(info.user.as_str()), } } diff --git a/rust_src/src/fns.rs b/rust_src/src/fns.rs index 7b7ed6ef095..fe22e9f72ff 100644 --- a/rust_src/src/fns.rs +++ b/rust_src/src/fns.rs @@ -591,10 +591,10 @@ pub fn copy_sequence(mut arg: LispObject) -> LispObject { let mut new = unsafe { make_uninit_bool_vector(nbits).force_bool_vector() }; new.as_mut_slice().copy_from_slice(boolvec.as_slice()); new.into() - } else if !(arg.is_cons() || arg.is_vector() || arg.is_string()) { - wrong_type!(Qsequencep, arg); - } else { + } else if arg.is_cons() || arg.is_vector() || arg.is_string() { unsafe { lisp_concat(1, &mut arg, arg.get_type(), false) } + } else { + wrong_type!(Qsequencep, arg); } } diff --git a/rust_src/src/fonts.rs b/rust_src/src/fonts.rs index 063a02027c0..152311f9e9b 100644 --- a/rust_src/src/fonts.rs +++ b/rust_src/src/fonts.rs @@ -117,7 +117,7 @@ impl LispFontObjectRef { unsafe { font_add_log(c_str.as_ptr(), self.into(), result.into()) } } - pub fn close(mut self, mut _frame: LispFrameRef) { + pub fn close(mut self, mut frame: LispFrameRef) { if data::aref(self.into(), FONT_TYPE_INDEX.into()).is_nil() { // Already closed return; @@ -130,11 +130,11 @@ impl LispFontObjectRef { #[cfg(feature = "window-system")] { #[cfg(feature = "window-system-x11")] - let mut display_info = &mut *(*_frame.output_data.x).display_info; + let mut display_info = &mut *(*frame.output_data.x).display_info; #[cfg(feature = "window-system-nextstep")] - let mut display_info = &mut *(*_frame.output_data.ns).display_info; + let mut display_info = &mut *(*frame.output_data.ns).display_info; #[cfg(feature = "window-system-w32")] - let mut display_info = &mut *(*_frame.output_data.w32).display_info; + let mut display_info = &mut *(*frame.output_data.w32).display_info; debug_assert!(display_info.n_fonts > 0); display_info.n_fonts -= 1; } diff --git a/rust_src/src/frames.rs b/rust_src/src/frames.rs index f83aa594a68..246b604a255 100644 --- a/rust_src/src/frames.rs +++ b/rust_src/src/frames.rs @@ -633,12 +633,12 @@ pub fn frame_focus(frame: LispFrameLiveOrSelected) -> LispObject { /// /// If there is no window system support, this function does nothing. #[lisp_fn(min = "1", name = "x-focus-frame", c_name = "x_focus_frame")] -pub fn x_focus_frame_lisp(_frame: LispFrameLiveOrSelected, _noactivate: bool) { +pub fn x_focus_frame_lisp(frame: LispFrameLiveOrSelected, noactivate: bool) { #[cfg(feature = "window-system")] { - let mut frame_ref: LispFrameRef = _frame.into(); + let mut frame_ref: LispFrameRef = frame.into(); unsafe { - x_focus_frame(frame_ref.as_mut(), _noactivate); + x_focus_frame(frame_ref.as_mut(), noactivate); } } } diff --git a/rust_src/src/keyboard.rs b/rust_src/src/keyboard.rs index 7ff07ed4c01..11a7aeda8a9 100644 --- a/rust_src/src/keyboard.rs +++ b/rust_src/src/keyboard.rs @@ -360,10 +360,10 @@ pub fn input_pending_p(check_timers: bool) -> bool { // Process non-user-visible events (Bug#10195). process_special_events(); - let val = if !check_timers { - 0 - } else { + let val = if check_timers { READABLE_EVENTS_DO_TIMERS_NOW + } else { + 0 }; get_input_pending(val | READABLE_EVENTS_FILTER_EVENTS) diff --git a/rust_src/src/lread.rs b/rust_src/src/lread.rs index 3f34596aef4..ba81e7b3fb6 100644 --- a/rust_src/src/lread.rs +++ b/rust_src/src/lread.rs @@ -207,10 +207,10 @@ pub unsafe extern "C" fn readbyte_from_stdio() -> i32 { unblock_input(); - if c != libc::EOF { - c - } else { + if c == libc::EOF { -1 + } else { + c } } diff --git a/rust_src/src/marker.rs b/rust_src/src/marker.rs index 95652837bcc..49f1a60237f 100644 --- a/rust_src/src/marker.rs +++ b/rust_src/src/marker.rs @@ -561,17 +561,20 @@ fn set_marker_internal_else( // Don't believe BYTEPOS if it comes from a different buffer, // since that buffer might have a very different correspondence // between character and byte positions. - if bytepos == -1 + bytepos = if bytepos == -1 || !position .as_marker() .map_or(false, |m| m.buffer() == Some(buf)) { - bytepos = buf.charpos_to_bytepos(charpos); + buf.charpos_to_bytepos(charpos) } else { - let beg = buf.buffer_beg_byte(restricted); - let end = buf.buffer_end_byte(restricted); - bytepos = clip_to_bounds(beg, bytepos as EmacsInt, end); - } + clip_to_bounds( + buf.buffer_beg_byte(restricted), + bytepos as EmacsInt, + buf.buffer_end_byte(restricted), + ) + }; + attach_marker(marker.as_mut(), buf.as_mut(), charpos, bytepos); } diff --git a/rust_src/src/multibyte.rs b/rust_src/src/multibyte.rs index f46ebfdba53..32d49918fa8 100644 --- a/rust_src/src/multibyte.rs +++ b/rust_src/src/multibyte.rs @@ -96,10 +96,10 @@ pub struct Codepoint(u32); impl Codepoint { // Equivalent to BYTE8_TO_CHAR /// Create a codepoint from a raw byte. If non-ascii, byte8 encode it. - pub fn from_raw(byte: u8) -> Codepoint { - match Codepoint::from(byte) { + pub fn from_raw(byte: u8) -> Self { + match Self::from(byte) { cp if cp.is_ascii() => cp, - cp => Codepoint::from(cp.0 + BYTE8_OFFSET), + cp => Self::from(cp.0 + BYTE8_OFFSET), } } @@ -145,9 +145,10 @@ impl Codepoint { /// Note that this does not check if the codepoint is within the /// appropriate range. pub fn to_byte8_unchecked(self) -> u8 { - match self.is_byte8() { - true => (self.0 - BYTE8_OFFSET) as u8, - false => (self.0 & 0xFF) as u8, + if self.is_byte8() { + (self.0 - BYTE8_OFFSET) as u8 + } else { + (self.0 & 0xFF) as u8 } } @@ -165,16 +166,17 @@ impl Codepoint { } // Equivalent to UNIBYTE_TO_CHAR - pub fn unibyte_to_char(self) -> Codepoint { - match self.is_ascii() { - true => self, - false => Codepoint::from_raw(self.0 as u8), + pub fn unibyte_to_char(self) -> Self { + if self.is_ascii() { + self + } else { + Self::from_raw(self.0 as u8) } } // Equivalent to MAKE_CHAR_MULTIBYTE /// Transform an 8-bit codepoint to its byte8 encoded form. - pub fn to_multibyte(self) -> Codepoint { + pub fn to_multibyte(self) -> Self { debug_assert!(self.is_single_byte()); self.unibyte_to_char() } @@ -211,7 +213,7 @@ impl Codepoint { to[0] = 0xF8; 5 } else if cp <= MAX_CHAR { - let b = Codepoint::from(cp).to_byte8_unchecked(); + let b = Self::from(cp).to_byte8_unchecked(); to[1] = 0x80 | (b & 0x3F); to[0] = 0xC0 | ((b >> 6) & 1); 2 @@ -222,11 +224,11 @@ impl Codepoint { /// If character code C has modifier masks, reflect them to the character /// code if possible. Return the resulting code. - pub fn resolve_modifier_mask(self) -> Codepoint { + pub fn resolve_modifier_mask(self) -> Self { let mut cp = self.0; // A non-ASCII character can't reflect modifier bits to the code. - if !Codepoint::from(cp & !char_bits::CHAR_MODIFIER_MASK).is_ascii() { - return Codepoint::from(cp); + if !Self::from(cp & !char_bits::CHAR_MODIFIER_MASK).is_ascii() { + return Self::from(cp); } let ascii = (cp & 0x7F) as u8; // For Meta, Shift, and Control modifiers, we need special care. @@ -253,7 +255,7 @@ impl Codepoint { cp &= 0x1F | (!0x7F & !char_bits::CHAR_CTL); } } - Codepoint::from(cp) + Self::from(cp) } } @@ -322,7 +324,7 @@ impl From for LispObject { impl From for Codepoint { fn from(o: LispObject) -> Self { match o.as_fixnum() { - Some(i) if 0 <= i && i <= EmacsInt::from(MAX_CHAR) => Codepoint::from(i as u32), + Some(i) if 0 <= i && i <= EmacsInt::from(MAX_CHAR) => Self::from(i as u32), _ => wrong_type!(Qcharacterp, o), } } @@ -1080,10 +1082,10 @@ pub unsafe extern "C" fn str_to_unibyte( srcslice = &srcslice[cplen..]; dstslice[i as usize] = if cp.val() > MAX_5_BYTE_CHAR { cp.to_byte8_unchecked() - } else if !cp.is_ascii() { - return i; - } else { + } else if cp.is_ascii() { cp.0 as c_uchar + } else { + return i; }; } chars diff --git a/rust_src/src/numbers.rs b/rust_src/src/numbers.rs index 1078ccfb6c0..620af4c7e10 100644 --- a/rust_src/src/numbers.rs +++ b/rust_src/src/numbers.rs @@ -325,7 +325,7 @@ pub fn random(limit: LispObject) -> LispObject { let mut seed = [0; 32]; let mut values: Vec = s.as_slice().to_vec(); values.resize(32, 0); - seed.copy_from_slice(&values.as_slice()); + seed.copy_from_slice(values.as_slice()); *rng = StdRng::from_seed(seed); } diff --git a/rust_src/src/remacs_sys.rs b/rust_src/src/remacs_sys.rs index 4bf2ccf7b28..223b5c03740 100755 --- a/rust_src/src/remacs_sys.rs +++ b/rust_src/src/remacs_sys.rs @@ -130,7 +130,7 @@ extern "C" { } // Max value for the first argument of wait_reading_process_output. -pub const WAIT_READING_MAX: i64 = std::i64::MAX; +pub const WAIT_READING_MAX: i64 = i64::max_value(); // In order to use `lazy_static!` with LispSubr, it must be Sync. Raw // pointers are not Sync, but it isn't a problem to define Sync if we diff --git a/rust_src/src/symbols.rs b/rust_src/src/symbols.rs index 65f89a9d0b2..fdf1dfc8f19 100644 --- a/rust_src/src/symbols.rs +++ b/rust_src/src/symbols.rs @@ -192,14 +192,14 @@ impl LispSymbolRef { LispSymbolIter { current: self } } - pub fn get_next(self) -> Option { + pub fn get_next(self) -> Option { // `iter().next()` returns the _current_ symbol: we want // another `next()` on the iterator to really get the next // symbol. we use `nth(1)` as a shortcut here. self.iter().nth(1) } - pub fn set_next(mut self, next: Option) { + pub fn set_next(mut self, next: Option) { let mut s = unsafe { self.u.s.as_mut() }; s.next = match next { Some(sym) => sym.as_ptr() as *mut Lisp_Symbol, @@ -514,12 +514,6 @@ pub fn local_variable_p(mut symbol: LispSymbolRef, buffer: LispBufferOrCurrent) /// If the current binding is global (the default), the value is nil. #[lisp_fn] pub fn variable_binding_locus(mut symbol: LispSymbolRef) -> LispObject { - // Make sure the current binding is actually swapped in. - unsafe { - symbol.find_value(); - } - symbol = symbol.get_indirect_variable(); - fn localized_handler(sym: LispSymbolRef) -> LispObject { // For a local variable, record both the symbol and which // buffer's or frame's value we are saving. @@ -534,16 +528,22 @@ pub fn variable_binding_locus(mut symbol: LispSymbolRef) -> LispObject { } } + // Make sure the current binding is actually swapped in. + unsafe { + symbol.find_value(); + } + symbol = symbol.get_indirect_variable(); + match symbol.get_redirect() { symbol_redirect::SYMBOL_PLAINVAL => Qnil, symbol_redirect::SYMBOL_FORWARDED => unsafe { let fwd = symbol.get_fwd(); if is_kboard_objfwd(fwd) { Fframe_terminal(selected_frame().into()) - } else if !is_buffer_objfwd(fwd) { - Qnil - } else { + } else if is_buffer_objfwd(fwd) { localized_handler(symbol) + } else { + Qnil } }, symbol_redirect::SYMBOL_LOCALIZED => localized_handler(symbol), diff --git a/rust_src/src/time.rs b/rust_src/src/time.rs index 62a3600ad8c..fa1becb1e46 100644 --- a/rust_src/src/time.rs +++ b/rust_src/src/time.rs @@ -386,7 +386,7 @@ pub unsafe extern "C" fn lisp_time_struct( invalid_time(); } - let mut t: lisp_time = Default::default(); + let mut t: lisp_time = lisp_time::default(); let val = decode_time_components(high, low, usec, psec, &mut t, ptr::null_mut()); check_time_validity(val); if !plen.is_null() { diff --git a/rust_src/src/windows.rs b/rust_src/src/windows.rs index 51561fc9d04..2f2d42f7d87 100644 --- a/rust_src/src/windows.rs +++ b/rust_src/src/windows.rs @@ -95,7 +95,7 @@ impl LispWindowRef { if mode_line_height >= 0 { mode_line_height - } else if matrix_mode_line_height != 0 { + } else if matrix_mode_line_height < 0 { self.mode_line_height = matrix_mode_line_height; matrix_mode_line_height } else { @@ -494,10 +494,10 @@ impl LispWindowRef { /// Width of the bottom divider of the window pub fn right_divider_width(self) -> i32 { - if !self.is_rightmost() { - self.get_frame().right_divider_width - } else { + if self.is_rightmost() { 0 + } else { + self.get_frame().right_divider_width } } @@ -784,10 +784,10 @@ pub extern "C" fn decode_any_window(window: LispObject) -> LispWindowRef { #[lisp_fn(min = "0")] pub fn window_normal_size(window: LispWindowValidOrSelected, horizontal: bool) -> EmacsDouble { let win: LispWindowRef = window.into(); - let frac = if !horizontal { - win.normal_lines - } else { + let frac = if horizontal { win.normal_cols + } else { + win.normal_lines }; EmacsDouble::from(frac) } @@ -1985,11 +1985,11 @@ pub fn set_window_fringes_lisp( let updated_window = unsafe { set_window_fringes(window.as_mut(), left_width, right_width, outside_margins) }; - if !updated_window.is_null() { + if updated_window.is_null() { + false + } else { unsafe { apply_window_adjustment(updated_window) }; true - } else { - false } }