Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
refactor(pal): address clippy lints
Browse files Browse the repository at this point in the history
- `string_lit_as_bytes`
- `identity_op`
- `len_zero`
- `iter_nth_zero`
- `collapsible_if` (evaded)
- `let_and_return`
- `len_without_is_empty` (ignored)
- `or_fun_call`
- `redundant_clone`
- `option_as_ref_deref`

Unaddressed:
- `len_zero` - This is a bug in clippy:
  <rust-lang/rust-clippy#3807>
  • Loading branch information
yvt committed May 29, 2020
1 parent 1dd3119 commit 5f33906
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tcw3/pal/macro/src/accel/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn gen_key_binding(
// Some characters are included in both of a normal keyboard and a numerical
// keypad. `Key` distinguishes between them.
let needs_keypad_disambiguation = if charcode < 128 {
"0123456789*+,-./".as_bytes().contains(&(charcode as u8))
b"0123456789*+,-./".contains(&(charcode as u8))
} else {
false
};
Expand Down
6 changes: 3 additions & 3 deletions tcw3/pal/macro/src/keycode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub enum Key {

bitflags::bitflags! {
pub struct ModFlags: u8 {
const SHIFT = 1 << 0;
const SHIFT = 1;
const CONTROL = 1 << 1;
const ALT = 1 << 2;
const SUPER = 1 << 3;
Expand Down Expand Up @@ -110,12 +110,12 @@ impl std::str::FromStr for KeyPattern {
return Err(format!("Unknown key: {:?}", s));
}

if s.len() == 0 {
if s.is_empty() {
return Err("Key symbol is missing".to_owned());
}

// For convenience, convert the character to lower case
Key::Char(s.chars().nth(0).unwrap().to_ascii_lowercase())
Key::Char(s.chars().next().unwrap().to_ascii_lowercase())
};

Ok(Self { key, mod_flags })
Expand Down
1 change: 1 addition & 0 deletions tcw3/pal/src/iface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ bitflags! {
///
/// The implementations of these methods are not allowed to use [`Wm`]'s methods
/// to manipulate the current text input context.
#[allow(clippy::len_without_is_empty)]
pub trait TextInputCtxEdit<T: Wm> {
/// Get the current selection.
///
Expand Down
6 changes: 5 additions & 1 deletion tcw3/pal/src/macos/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,14 @@ impl iface::TextLayout for TextLayout {
let text = &self.text.as_bytes();

if forward {
// If `forward == true` and `i` is already at the end of the text,
// return `i`
if i >= text.len() {
return i;
}
} else {
// If `forward == false` and `i` is already at the start of the text,
// return `i`
if i == 0 {
return 0;
}
Expand Down Expand Up @@ -851,7 +855,7 @@ fn ctrun_get_status(this: &CTRun) -> CTRunStatus {

#[allow(dead_code)]
fn ctrun_get_advances(this: &CTRun, start: isize, out_advances: &mut [MaybeUninit<CGSize>]) {
if out_advances.len() == 0 {
if out_advances.is_empty() {
return;
}

Expand Down
20 changes: 9 additions & 11 deletions tcw3/pal/src/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,15 @@ impl AccelTable {
&& charcode_unmod == binding.charcode
})
.map(|binding| binding.action)
.nth(0)
.next() // get the first matching one (if any)
}

fn find_action_with_sel(&self, sel: &[u8]) -> Option<iface::ActionId> {
self.sel
.iter()
.filter(|binding| binding.sel.as_bytes() == sel)
.map(|binding| binding.action)
.nth(0)
.next() // get the first matching one (if any)
}
}

Expand Down Expand Up @@ -1244,7 +1244,7 @@ unsafe extern "C" fn tcw_wnd_get_selected_range(ud: TCWListenerUserData) -> NSRa
log::trace!("tcw_wnd_get_selected_range → {}..+{}", start, len);
NSRange::new(start as _, len as _)
})
.unwrap_or(nsrange_not_found())
.unwrap_or_else(nsrange_not_found)
}

#[no_mangle]
Expand Down Expand Up @@ -1280,7 +1280,7 @@ unsafe extern "C" fn tcw_wnd_get_marked_range(ud: TCWListenerUserData) -> NSRang
log::trace!("tcw_wnd_get_marked_range → {}..+{}", start, len);
NSRange::new(start as _, len as _)
})
.unwrap_or(nsrange_not_found())
.unwrap_or_else(nsrange_not_found)
}

#[no_mangle]
Expand Down Expand Up @@ -1314,8 +1314,8 @@ unsafe extern "C" fn tcw_wnd_get_text(
let (range_u8, range_actual_u16, text) =
edit_convert_range_to_utf8_with_text(&mut *edit, start..start.saturating_add(len));

log::trace!("... actual range (UTF-8) = {:?}", range_u8.clone());
log::trace!("... actual range (UTF-16) = {:?}", range_actual_u16.clone());
log::trace!("... actual range (UTF-8) = {:?}", range_u8);
log::trace!("... actual range (UTF-16) = {:?}", range_actual_u16);

if let Some(actual_range_cell) = actual_range {
*actual_range_cell = NSRange::new(
Expand All @@ -1330,7 +1330,7 @@ unsafe extern "C" fn tcw_wnd_get_text(

NSString::alloc(nil).init_str(slice)
})
.unwrap_or_else(|| std::ptr::null_mut())
.unwrap_or_else(std::ptr::null_mut)
}

fn empty_nsrect() -> NSRect {
Expand Down Expand Up @@ -1401,11 +1401,9 @@ unsafe extern "C" fn tcw_wnd_get_text_rect(

// Convert `Box2<f32>` to `NSRect`. Our Objective-C handler method
// handles the conversion to screen coordinates.
let bounds = ns_rect_from_box2(cg_bounds.cast::<f64>().unwrap());

bounds
ns_rect_from_box2(cg_bounds.cast::<f64>().unwrap())
})
.unwrap_or(empty_nsrect())
.unwrap_or_else(empty_nsrect)
}

#[no_mangle]
Expand Down
4 changes: 2 additions & 2 deletions tcw3/pal/src/testing/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ impl RecordSend {
.target(&self.target)
.level(self.level)
.args(format_args!("{}", self.args))
.module_path(self.module_path.as_ref().map(String::as_str))
.file(self.file.as_ref().map(String::as_str))
.module_path(self.module_path.as_deref())
.file(self.file.as_deref())
.line(self.line)
.build())
}
Expand Down

0 comments on commit 5f33906

Please sign in to comment.