Skip to content

Commit

Permalink
Yet more Clippy
Browse files Browse the repository at this point in the history
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
    - rust-lang/rust-clippy#2380
- Rename some shadowing variables
- Cut some unnecessary references
- Use <struct>::default instead of Default::default
- Update inclusive range syntax
- Cut open import
- Self
- dyn trait
  • Loading branch information
nickdrozd committed Jun 12, 2019
1 parent 375fa2b commit 372754a
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 98 deletions.
6 changes: 3 additions & 3 deletions rust_src/remacs-lib/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust_src/src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion rust_src/src/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion rust_src/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ fn internal_self_insert(mut c: Codepoint, n: usize) -> EmacsInt {
} else if n > 1 {
let mut strn: Vec<libc::c_uchar> = 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]);
Expand Down
2 changes: 1 addition & 1 deletion rust_src/src/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn zlib_available_p() -> bool {
true
}

fn create_buffer_decoder<'a>(buffer: &'a [u8]) -> Box<Read + 'a> {
fn create_buffer_decoder<'a>(buffer: &'a [u8]) -> Box<dyn Read + 'a> {
let magic_number = buffer[0];

match magic_number {
Expand Down
7 changes: 2 additions & 5 deletions rust_src/src/dired_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -245,9 +245,6 @@ fn read_dir(dname: &str, fnames: &mut Vec<String>, match_re: Option<LispObject>)
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() {
Expand Down
14 changes: 6 additions & 8 deletions rust_src/src/editfns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
21 changes: 10 additions & 11 deletions rust_src/src/filelock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<LockInfo> {
fn parse(data: &str) -> Option<Self> {
// 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.
Expand Down Expand Up @@ -86,7 +85,7 @@ impl LockInfo {
)
};

Some(LockInfo {
Some(Self {
user,
host,
pid,
Expand Down Expand Up @@ -244,22 +243,22 @@ fn current_lock_owner(path: &Path) -> Result<LockState> {
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)
Expand Down Expand Up @@ -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()),
}
}

Expand Down
6 changes: 3 additions & 3 deletions rust_src/src/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
8 changes: 4 additions & 4 deletions rust_src/src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions rust_src/src/frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions rust_src/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions rust_src/src/lread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
15 changes: 9 additions & 6 deletions rust_src/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
42 changes: 22 additions & 20 deletions rust_src/src/multibyte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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
}
}

Expand All @@ -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()
}
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -253,7 +255,7 @@ impl Codepoint {
cp &= 0x1F | (!0x7F & !char_bits::CHAR_CTL);
}
}
Codepoint::from(cp)
Self::from(cp)
}
}

Expand Down Expand Up @@ -322,7 +324,7 @@ impl From<Codepoint> for LispObject {
impl From<LispObject> 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),
}
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rust_src/src/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub fn random(limit: LispObject) -> LispObject {
let mut seed = [0; 32];
let mut values: Vec<u8> = 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);
}

Expand Down
Loading

0 comments on commit 372754a

Please sign in to comment.