Skip to content

Commit 372754a

Browse files
committed
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 - 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
1 parent 375fa2b commit 372754a

20 files changed

+97
-98
lines changed

rust_src/remacs-lib/files.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ fn generate_temporary_filename(name: &mut String) {
7676
}
7777
for byte in bytes.iter_mut() {
7878
*byte = match *byte % 62 {
79-
v @ 0...9 => v + b'0',
80-
v @ 10...35 => v - 10 + b'a',
81-
v @ 36...61 => v - 36 + b'A',
79+
v @ 0..=9 => v + b'0',
80+
v @ 10..=35 => v - 10 + b'a',
81+
v @ 36..=61 => v - 36 + b'A',
8282
_ => unreachable!(),
8383
}
8484
}

rust_src/src/buffers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl LispBufferRef {
180180
let mut name: LispStringRef = copy_sequence(name.into()).force_string();
181181
name.set_intervals(ptr::null_mut());
182182
b.name_ = name.into();
183-
b.undo_list_ = if name.byte_at(0) != b' ' { Qnil } else { Qt };
183+
b.undo_list_ = (name.byte_at(0) == b' ').into();
184184

185185
b.reset();
186186
b.reset_local_variables(true);
@@ -1409,7 +1409,7 @@ fn get_truename_buffer_1(filename: LispSymbolOrString) -> LispObject {
14091409
/// for positions far away from POS).
14101410
#[lisp_fn]
14111411
pub fn overlay_recenter(pos: LispNumber) {
1412-
let p = clip_to_bounds(std::isize::MIN, pos.to_fixnum(), std::isize::MAX);
1412+
let p = clip_to_bounds(isize::min_value(), pos.to_fixnum(), isize::max_value());
14131413
unsafe {
14141414
recenter_overlay_lists(ThreadState::current_buffer_unchecked().as_mut(), p);
14151415
}

rust_src/src/character.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn multibyte_char_to_unibyte(ch: Codepoint) -> EmacsInt {
8888
// a latin1 char, so let's let it slide.
8989
ch.into()
9090
} else {
91-
ch.to_byte8().map(EmacsInt::from).unwrap_or(-1)
91+
ch.to_byte8().map_or(-1, EmacsInt::from)
9292
}
9393
}
9494

rust_src/src/cmds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ fn internal_self_insert(mut c: Codepoint, n: usize) -> EmacsInt {
436436
} else if n > 1 {
437437
let mut strn: Vec<libc::c_uchar> = match n.checked_mul(len) {
438438
Some(size_bytes) => Vec::with_capacity(size_bytes),
439-
None => unsafe { memory_full(std::usize::MAX) },
439+
None => unsafe { memory_full(usize::max_value()) },
440440
};
441441
for _ in 0..n {
442442
strn.extend_from_slice(&str[0..len]);

rust_src/src/decompress.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn zlib_available_p() -> bool {
2222
true
2323
}
2424

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

2828
match magic_number {

rust_src/src/dired_unix.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ impl StringExt for String {
9292
// So here make sure just one '/' between dir&file.
9393
let last_char = dir.as_str().chars().last().unwrap();
9494
if last_char == '/' {
95-
dir + &self
95+
dir + self
9696
} else {
97-
dir + "/" + &self
97+
dir + "/" + self
9898
}
9999
}
100100
}
@@ -245,9 +245,6 @@ fn read_dir(dname: &str, fnames: &mut Vec<String>, match_re: Option<LispObject>)
245245
fnames.push(dotdot);
246246
}
247247

248-
// NOTE: The allow can be dropped when the issue
249-
// https://github.com/rust-lang/rust-clippy/issues/3913 is resolved
250-
#[allow(clippy::identity_conversion)]
251248
for fname in fs::read_dir(dir_p)? {
252249
let fname = fname?;
253250
let f_enc = match fname.file_name().into_string() {

rust_src/src/editfns.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,13 @@ pub fn byte_to_string(byte: EmacsInt) -> LispObject {
458458
/// Return the first character in STRING.
459459
#[lisp_fn]
460460
pub fn string_to_char(string: LispStringRef) -> EmacsInt {
461-
if !string.is_empty() {
462-
if string.is_multibyte() {
463-
let (cp, _) = multibyte_char_at(string.as_slice());
464-
EmacsInt::from(cp)
465-
} else {
466-
EmacsInt::from(string.byte_at(0))
467-
}
468-
} else {
461+
if string.is_empty() {
469462
0
463+
} else if string.is_multibyte() {
464+
let (cp, _) = multibyte_char_at(string.as_slice());
465+
EmacsInt::from(cp)
466+
} else {
467+
EmacsInt::from(string.byte_at(0))
470468
}
471469
}
472470

rust_src/src/filelock.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use crate::{
2525
remacs_sys::{Qnil, Qstringp, Qt},
2626
threads::ThreadState,
2727
};
28-
use LockState::*;
2928

3029
/// An arbitrary limit on lock contents length when it is stored in the
3130
/// contents of the lock file. 8 K should be plenty big enough in practice.
@@ -47,7 +46,7 @@ struct LockInfo {
4746
impl LockInfo {
4847
/// Parses a string like `[email protected]:BOOT_TIME` into a [`LockInfo`].
4948
/// Returns [`None`] if the parse fails.
50-
fn parse(data: &str) -> Option<LockInfo> {
49+
fn parse(data: &str) -> Option<Self> {
5150
// Treat "\357\200\242" (U+F022 in UTF-8) as if it were ":" (Bug#24656).
5251
// This works around a bug in the Linux CIFS kernel client, which can
5352
// mistakenly transliterate ':' to U+F022 in symlink contents.
@@ -86,7 +85,7 @@ impl LockInfo {
8685
)
8786
};
8887

89-
Some(LockInfo {
88+
Some(Self {
9089
user,
9190
host,
9291
pid,
@@ -244,22 +243,22 @@ fn current_lock_owner(path: &Path) -> Result<LockState> {
244243
if info.host.as_bytes() == system_name().as_slice() {
245244
if info.pid == std::process::id() as i32 {
246245
// We own it.
247-
LockedByUs
246+
LockState::LockedByUs
248247
} else if process_exists(info.pid) && boot_time_within_one_second(&info) {
249248
// An existing process on this machine owns it.
250-
LockedBy(info)
249+
LockState::LockedBy(info)
251250
} else {
252251
// The owner process is dead or has a strange pid, so try to
253252
// zap the lockfile.
254253
remove_file(path)?;
255-
NotLocked
254+
LockState::NotLocked
256255
}
257256
} else {
258-
LockedBy(info)
257+
LockState::LockedBy(info)
259258
}
260259
}
261260

262-
None => NotLocked,
261+
None => LockState::NotLocked,
263262
};
264263

265264
Ok(result)
@@ -329,9 +328,9 @@ pub fn file_locked_p(filename: LispStringRef) -> LispObject {
329328
let path = make_lock_name(expand_file_name(filename, None));
330329

331330
match current_lock_owner(&path) {
332-
Ok(NotLocked) | Err(_) => Qnil,
333-
Ok(LockedByUs) => Qt,
334-
Ok(LockedBy(info)) => LispObject::from(info.user.as_str()),
331+
Ok(LockState::NotLocked) | Err(_) => Qnil,
332+
Ok(LockState::LockedByUs) => Qt,
333+
Ok(LockState::LockedBy(info)) => LispObject::from(info.user.as_str()),
335334
}
336335
}
337336

rust_src/src/fns.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,10 @@ pub fn copy_sequence(mut arg: LispObject) -> LispObject {
591591
let mut new = unsafe { make_uninit_bool_vector(nbits).force_bool_vector() };
592592
new.as_mut_slice().copy_from_slice(boolvec.as_slice());
593593
new.into()
594-
} else if !(arg.is_cons() || arg.is_vector() || arg.is_string()) {
595-
wrong_type!(Qsequencep, arg);
596-
} else {
594+
} else if arg.is_cons() || arg.is_vector() || arg.is_string() {
597595
unsafe { lisp_concat(1, &mut arg, arg.get_type(), false) }
596+
} else {
597+
wrong_type!(Qsequencep, arg);
598598
}
599599
}
600600

rust_src/src/fonts.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl LispFontObjectRef {
117117
unsafe { font_add_log(c_str.as_ptr(), self.into(), result.into()) }
118118
}
119119

120-
pub fn close(mut self, mut _frame: LispFrameRef) {
120+
pub fn close(mut self, mut frame: LispFrameRef) {
121121
if data::aref(self.into(), FONT_TYPE_INDEX.into()).is_nil() {
122122
// Already closed
123123
return;
@@ -130,11 +130,11 @@ impl LispFontObjectRef {
130130
#[cfg(feature = "window-system")]
131131
{
132132
#[cfg(feature = "window-system-x11")]
133-
let mut display_info = &mut *(*_frame.output_data.x).display_info;
133+
let mut display_info = &mut *(*frame.output_data.x).display_info;
134134
#[cfg(feature = "window-system-nextstep")]
135-
let mut display_info = &mut *(*_frame.output_data.ns).display_info;
135+
let mut display_info = &mut *(*frame.output_data.ns).display_info;
136136
#[cfg(feature = "window-system-w32")]
137-
let mut display_info = &mut *(*_frame.output_data.w32).display_info;
137+
let mut display_info = &mut *(*frame.output_data.w32).display_info;
138138
debug_assert!(display_info.n_fonts > 0);
139139
display_info.n_fonts -= 1;
140140
}

rust_src/src/frames.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -633,12 +633,12 @@ pub fn frame_focus(frame: LispFrameLiveOrSelected) -> LispObject {
633633
///
634634
/// If there is no window system support, this function does nothing.
635635
#[lisp_fn(min = "1", name = "x-focus-frame", c_name = "x_focus_frame")]
636-
pub fn x_focus_frame_lisp(_frame: LispFrameLiveOrSelected, _noactivate: bool) {
636+
pub fn x_focus_frame_lisp(frame: LispFrameLiveOrSelected, noactivate: bool) {
637637
#[cfg(feature = "window-system")]
638638
{
639-
let mut frame_ref: LispFrameRef = _frame.into();
639+
let mut frame_ref: LispFrameRef = frame.into();
640640
unsafe {
641-
x_focus_frame(frame_ref.as_mut(), _noactivate);
641+
x_focus_frame(frame_ref.as_mut(), noactivate);
642642
}
643643
}
644644
}

rust_src/src/keyboard.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,10 @@ pub fn input_pending_p(check_timers: bool) -> bool {
360360
// Process non-user-visible events (Bug#10195).
361361
process_special_events();
362362

363-
let val = if !check_timers {
364-
0
365-
} else {
363+
let val = if check_timers {
366364
READABLE_EVENTS_DO_TIMERS_NOW
365+
} else {
366+
0
367367
};
368368

369369
get_input_pending(val | READABLE_EVENTS_FILTER_EVENTS)

rust_src/src/lread.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ pub unsafe extern "C" fn readbyte_from_stdio() -> i32 {
207207

208208
unblock_input();
209209

210-
if c != libc::EOF {
211-
c
212-
} else {
210+
if c == libc::EOF {
213211
-1
212+
} else {
213+
c
214214
}
215215
}
216216

rust_src/src/marker.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -561,17 +561,20 @@ fn set_marker_internal_else(
561561
// Don't believe BYTEPOS if it comes from a different buffer,
562562
// since that buffer might have a very different correspondence
563563
// between character and byte positions.
564-
if bytepos == -1
564+
bytepos = if bytepos == -1
565565
|| !position
566566
.as_marker()
567567
.map_or(false, |m| m.buffer() == Some(buf))
568568
{
569-
bytepos = buf.charpos_to_bytepos(charpos);
569+
buf.charpos_to_bytepos(charpos)
570570
} else {
571-
let beg = buf.buffer_beg_byte(restricted);
572-
let end = buf.buffer_end_byte(restricted);
573-
bytepos = clip_to_bounds(beg, bytepos as EmacsInt, end);
574-
}
571+
clip_to_bounds(
572+
buf.buffer_beg_byte(restricted),
573+
bytepos as EmacsInt,
574+
buf.buffer_end_byte(restricted),
575+
)
576+
};
577+
575578
attach_marker(marker.as_mut(), buf.as_mut(), charpos, bytepos);
576579
}
577580

rust_src/src/multibyte.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ pub struct Codepoint(u32);
9696
impl Codepoint {
9797
// Equivalent to BYTE8_TO_CHAR
9898
/// Create a codepoint from a raw byte. If non-ascii, byte8 encode it.
99-
pub fn from_raw(byte: u8) -> Codepoint {
100-
match Codepoint::from(byte) {
99+
pub fn from_raw(byte: u8) -> Self {
100+
match Self::from(byte) {
101101
cp if cp.is_ascii() => cp,
102-
cp => Codepoint::from(cp.0 + BYTE8_OFFSET),
102+
cp => Self::from(cp.0 + BYTE8_OFFSET),
103103
}
104104
}
105105

@@ -145,9 +145,10 @@ impl Codepoint {
145145
/// Note that this does not check if the codepoint is within the
146146
/// appropriate range.
147147
pub fn to_byte8_unchecked(self) -> u8 {
148-
match self.is_byte8() {
149-
true => (self.0 - BYTE8_OFFSET) as u8,
150-
false => (self.0 & 0xFF) as u8,
148+
if self.is_byte8() {
149+
(self.0 - BYTE8_OFFSET) as u8
150+
} else {
151+
(self.0 & 0xFF) as u8
151152
}
152153
}
153154

@@ -165,16 +166,17 @@ impl Codepoint {
165166
}
166167

167168
// Equivalent to UNIBYTE_TO_CHAR
168-
pub fn unibyte_to_char(self) -> Codepoint {
169-
match self.is_ascii() {
170-
true => self,
171-
false => Codepoint::from_raw(self.0 as u8),
169+
pub fn unibyte_to_char(self) -> Self {
170+
if self.is_ascii() {
171+
self
172+
} else {
173+
Self::from_raw(self.0 as u8)
172174
}
173175
}
174176

175177
// Equivalent to MAKE_CHAR_MULTIBYTE
176178
/// Transform an 8-bit codepoint to its byte8 encoded form.
177-
pub fn to_multibyte(self) -> Codepoint {
179+
pub fn to_multibyte(self) -> Self {
178180
debug_assert!(self.is_single_byte());
179181
self.unibyte_to_char()
180182
}
@@ -211,7 +213,7 @@ impl Codepoint {
211213
to[0] = 0xF8;
212214
5
213215
} else if cp <= MAX_CHAR {
214-
let b = Codepoint::from(cp).to_byte8_unchecked();
216+
let b = Self::from(cp).to_byte8_unchecked();
215217
to[1] = 0x80 | (b & 0x3F);
216218
to[0] = 0xC0 | ((b >> 6) & 1);
217219
2
@@ -222,11 +224,11 @@ impl Codepoint {
222224

223225
/// If character code C has modifier masks, reflect them to the character
224226
/// code if possible. Return the resulting code.
225-
pub fn resolve_modifier_mask(self) -> Codepoint {
227+
pub fn resolve_modifier_mask(self) -> Self {
226228
let mut cp = self.0;
227229
// A non-ASCII character can't reflect modifier bits to the code.
228-
if !Codepoint::from(cp & !char_bits::CHAR_MODIFIER_MASK).is_ascii() {
229-
return Codepoint::from(cp);
230+
if !Self::from(cp & !char_bits::CHAR_MODIFIER_MASK).is_ascii() {
231+
return Self::from(cp);
230232
}
231233
let ascii = (cp & 0x7F) as u8;
232234
// For Meta, Shift, and Control modifiers, we need special care.
@@ -253,7 +255,7 @@ impl Codepoint {
253255
cp &= 0x1F | (!0x7F & !char_bits::CHAR_CTL);
254256
}
255257
}
256-
Codepoint::from(cp)
258+
Self::from(cp)
257259
}
258260
}
259261

@@ -322,7 +324,7 @@ impl From<Codepoint> for LispObject {
322324
impl From<LispObject> for Codepoint {
323325
fn from(o: LispObject) -> Self {
324326
match o.as_fixnum() {
325-
Some(i) if 0 <= i && i <= EmacsInt::from(MAX_CHAR) => Codepoint::from(i as u32),
327+
Some(i) if 0 <= i && i <= EmacsInt::from(MAX_CHAR) => Self::from(i as u32),
326328
_ => wrong_type!(Qcharacterp, o),
327329
}
328330
}
@@ -1080,10 +1082,10 @@ pub unsafe extern "C" fn str_to_unibyte(
10801082
srcslice = &srcslice[cplen..];
10811083
dstslice[i as usize] = if cp.val() > MAX_5_BYTE_CHAR {
10821084
cp.to_byte8_unchecked()
1083-
} else if !cp.is_ascii() {
1084-
return i;
1085-
} else {
1085+
} else if cp.is_ascii() {
10861086
cp.0 as c_uchar
1087+
} else {
1088+
return i;
10871089
};
10881090
}
10891091
chars

rust_src/src/numbers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ pub fn random(limit: LispObject) -> LispObject {
325325
let mut seed = [0; 32];
326326
let mut values: Vec<u8> = s.as_slice().to_vec();
327327
values.resize(32, 0);
328-
seed.copy_from_slice(&values.as_slice());
328+
seed.copy_from_slice(values.as_slice());
329329
*rng = StdRng::from_seed(seed);
330330
}
331331

0 commit comments

Comments
 (0)