1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-04 15:07:47 +00:00

refactor/polish ~ fix cargo clippy complaints (avoid as ...)

This commit is contained in:
Roy Ivy III 2019-12-25 23:39:46 -06:00
parent 66124454e3
commit d343d533ba
13 changed files with 38 additions and 38 deletions

View file

@ -216,7 +216,7 @@ fn convert_size_human(size: u64, multiplier: u64, _block_size: u64) -> String {
} }
fn convert_size_b(size: u64, _multiplier: u64, _block_size: u64) -> String { fn convert_size_b(size: u64, _multiplier: u64, _block_size: u64) -> String {
format!("{}", ((size as f64) / (1 as f64)).ceil()) format!("{}", ((size as f64) / (1_f64)).ceil())
} }
fn convert_size_k(size: u64, multiplier: u64, _block_size: u64) -> String { fn convert_size_k(size: u64, multiplier: u64, _block_size: u64) -> String {

View file

@ -169,7 +169,7 @@ fn expand(options: Options) {
for file in options.files.into_iter() { for file in options.files.into_iter() {
let mut fh = open(file); let mut fh = open(file);
while match fh.read_until('\n' as u8, &mut buf) { while match fh.read_until(b'\n', &mut buf) {
Ok(s) => s > 0, Ok(s) => s > 0,
Err(_) => buf.is_empty(), Err(_) => buf.is_empty(),
} { } {

View file

@ -128,7 +128,7 @@ fn mkdir(path: &Path, recursive: bool, mode: u16, verbose: bool) -> i32 {
use fs::{Permissions, set_permissions}; use fs::{Permissions, set_permissions};
use std::os::unix::fs::{PermissionsExt}; use std::os::unix::fs::{PermissionsExt};
let mode = Permissions::from_mode(mode as u32); let mode = Permissions::from_mode(u32::from(mode));
if let Err(err) = set_permissions(path, mode) { if let Err(err) = set_permissions(path, mode) {
show_error!( show_error!(

View file

@ -179,9 +179,9 @@ pub fn dry_exec(mut tmpdir: PathBuf, prefix: &str, rand: usize, suffix: &str) ->
rand::thread_rng().fill(bytes); rand::thread_rng().fill(bytes);
for byte in bytes.iter_mut() { for byte in bytes.iter_mut() {
*byte = match *byte % 62 { *byte = match *byte % 62 {
v @ 0..=9 => (v + b'0' as u8), v @ 0..=9 => (v + b'0'),
v @ 10..=35 => (v - 10 + b'a' as u8), v @ 10..=35 => (v - 10 + b'a'),
v @ 36..=61 => (v - 36 + b'A' as u8), v @ 36..=61 => (v - 36 + b'A'),
_ => unreachable!(), _ => unreachable!(),
} }
} }

View file

@ -133,9 +133,9 @@ impl<'a> MemoryDecoder<'a> {
/// Returns a u8/u16/u32/u64 from the internal buffer at position `start`. /// Returns a u8/u16/u32/u64 from the internal buffer at position `start`.
pub fn read_uint(&self, start: usize, byte_size: usize) -> u64 { pub fn read_uint(&self, start: usize, byte_size: usize) -> u64 {
match byte_size { match byte_size {
1 => self.data[start] as u64, 1 => u64::from(self.data[start]),
2 => self.byte_order.read_u16(&self.data[start..start + 2]) as u64, 2 => u64::from(self.byte_order.read_u16(&self.data[start..start + 2])),
4 => self.byte_order.read_u32(&self.data[start..start + 4]) as u64, 4 => u64::from(self.byte_order.read_u32(&self.data[start..start + 4])),
8 => self.byte_order.read_u64(&self.data[start..start + 8]), 8 => self.byte_order.read_u64(&self.data[start..start + 8]),
_ => panic!("Invalid byte_size: {}", byte_size), _ => panic!("Invalid byte_size: {}", byte_size),
} }
@ -147,7 +147,7 @@ impl<'a> MemoryDecoder<'a> {
2 => f64::from(f16::from_bits( 2 => f64::from(f16::from_bits(
self.byte_order.read_u16(&self.data[start..start + 2]), self.byte_order.read_u16(&self.data[start..start + 2]),
)), )),
4 => self.byte_order.read_f32(&self.data[start..start + 4]) as f64, 4 => f64::from(self.byte_order.read_f32(&self.data[start..start + 4])),
8 => self.byte_order.read_f64(&self.data[start..start + 8]), 8 => self.byte_order.read_f64(&self.data[start..start + 8]),
_ => panic!("Invalid byte_size: {}", byte_size), _ => panic!("Invalid byte_size: {}", byte_size),
} }

View file

@ -48,7 +48,7 @@ fn format_flo32(f: f32) -> String {
// subnormal numbers will be normal as f64, so will print with a wrong precision // subnormal numbers will be normal as f64, so will print with a wrong precision
format!("{:width$e}", f, width = width) // subnormal numbers format!("{:width$e}", f, width = width) // subnormal numbers
} else { } else {
format_float(f as f64, width, precision) format_float(f64::from(f), width, precision)
} }
} }

View file

@ -2,8 +2,8 @@ pub fn arrnum_int_mult(arr_num: &[u8], basenum: u8, base_ten_int_fact: u8) -> Ve
let mut carry: u16 = 0; let mut carry: u16 = 0;
let mut rem: u16; let mut rem: u16;
let mut new_amount: u16; let mut new_amount: u16;
let fact: u16 = base_ten_int_fact as u16; let fact: u16 = u16::from(base_ten_int_fact);
let base: u16 = basenum as u16; let base: u16 = u16::from(basenum);
let mut ret_rev: Vec<u8> = Vec::new(); let mut ret_rev: Vec<u8> = Vec::new();
let mut it = arr_num.iter().rev(); let mut it = arr_num.iter().rev();
@ -11,7 +11,7 @@ pub fn arrnum_int_mult(arr_num: &[u8], basenum: u8, base_ten_int_fact: u8) -> Ve
let i = it.next(); let i = it.next();
match i { match i {
Some(u) => { Some(u) => {
new_amount = ((*u as u16) * fact) + carry; new_amount = (u16::from(*u) * fact) + carry;
rem = new_amount % base; rem = new_amount % base;
carry = (new_amount - rem) / base; carry = (new_amount - rem) / base;
ret_rev.push(rem as u8) ret_rev.push(rem as u8)
@ -54,8 +54,8 @@ pub fn arrnum_int_div_step(
}; };
let mut bufferval: u16 = 0; let mut bufferval: u16 = 0;
let base: u16 = radix_in as u16; let base: u16 = u16::from(radix_in);
let divisor: u16 = base_ten_int_divisor as u16; let divisor: u16 = u16::from(base_ten_int_divisor);
let mut traversed = 0; let mut traversed = 0;
let mut quotient = 0; let mut quotient = 0;
@ -64,9 +64,9 @@ pub fn arrnum_int_div_step(
let mut it_f = refd_vals.iter(); let mut it_f = refd_vals.iter();
loop { loop {
let u = match it_replace.next() { let u = match it_replace.next() {
Some(u_rep) => *u_rep as u16, Some(u_rep) => u16::from(*u_rep),
None => match it_f.next() { None => match it_f.next() {
Some(u_orig) => *u_orig as u16, Some(u_orig) => u16::from(*u_orig),
None => { None => {
if !after_decimal { if !after_decimal {
break; break;
@ -167,7 +167,7 @@ pub fn arrnum_int_add(arrnum: &[u8], basenum: u8, base_ten_int_term: u8) -> Vec<
let mut carry: u16 = u16::from(base_ten_int_term); let mut carry: u16 = u16::from(base_ten_int_term);
let mut rem: u16; let mut rem: u16;
let mut new_amount: u16; let mut new_amount: u16;
let base: u16 = basenum as u16; let base: u16 = u16::from(basenum);
let mut ret_rev: Vec<u8> = Vec::new(); let mut ret_rev: Vec<u8> = Vec::new();
let mut it = arrnum.iter().rev(); let mut it = arrnum.iter().rev();
@ -175,7 +175,7 @@ pub fn arrnum_int_add(arrnum: &[u8], basenum: u8, base_ten_int_term: u8) -> Vec<
let i = it.next(); let i = it.next();
match i { match i {
Some(u) => { Some(u) => {
new_amount = (*u as u16) + carry; new_amount = u16::from(*u) + carry;
rem = new_amount % base; rem = new_amount % base;
carry = (new_amount - rem) / base; carry = (new_amount - rem) / base;
ret_rev.push(rem as u8) ret_rev.push(rem as u8)
@ -224,17 +224,17 @@ pub fn base_conv_float(src: &[u8], radix_src: u8, radix_dest: u8) -> f64 {
// of how it would work. // of how it would work.
let mut result: Vec<u8> = Vec::new(); let mut result: Vec<u8> = Vec::new();
result.push(0); result.push(0);
let mut factor: f64 = 1.; let mut factor: f64 = 1_f64;
let radix_src_float: f64 = radix_src as f64; let radix_src_float: f64 = f64::from(radix_src);
let mut i = 0; let mut i = 0;
let mut r: f64 = 0 as f64; let mut r: f64 = 0_f64;
for u in src { for u in src {
if i > 15 { if i > 15 {
break; break;
} }
i += 1; i += 1;
factor /= radix_src_float; factor /= radix_src_float;
r += factor * (*u as f64) r += factor * f64::from(*u)
} }
r r
} }
@ -287,9 +287,9 @@ pub trait RadixDef {
} }
pub struct RadixTen; pub struct RadixTen;
const ZERO_ASC: u8 = '0' as u8; const ZERO_ASC: u8 = b'0';
const UPPER_A_ASC: u8 = 'A' as u8; const UPPER_A_ASC: u8 = b'A';
const LOWER_A_ASC: u8 = 'a' as u8; const LOWER_A_ASC: u8 = b'a';
impl RadixDef for RadixTen { impl RadixDef for RadixTen {
fn get_max(&self) -> u8 { fn get_max(&self) -> u8 {

View file

@ -297,7 +297,7 @@ fn prompt(msg: &str) -> bool {
let stdin = stdin(); let stdin = stdin();
let mut stdin = stdin.lock(); let mut stdin = stdin.lock();
match stdin.read_until('\n' as u8, &mut buf) { match stdin.read_until(b'\n', &mut buf) {
Ok(x) if x > 0 => match buf[0] { Ok(x) if x > 0 => match buf[0] {
b'y' | b'Y' => true, b'y' | b'Y' => true,
_ => false, _ => false,

View file

@ -25,7 +25,7 @@ impl BirthTime for Metadata {
self.created() self.created()
.ok() .ok()
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()) .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|e| pretty_time(e.as_secs() as i64, e.subsec_nanos() as i64)) .map(|e| pretty_time(e.as_secs() as i64, i64::from(e.subsec_nanos())))
.unwrap_or_else(|| "-".to_owned()) .unwrap_or_else(|| "-".to_owned())
} }
@ -220,7 +220,7 @@ impl FsMeta for Sstatfs {
#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "linux"))] #[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "linux"))]
fn fsid(&self) -> u64 { fn fsid(&self) -> u64 {
let f_fsid: &[u32; 2] = unsafe { transmute(&self.f_fsid) }; let f_fsid: &[u32; 2] = unsafe { transmute(&self.f_fsid) };
(f_fsid[0] as u64) << 32 | f_fsid[1] as u64 (u64::from(f_fsid[0])) << 32 | u64::from(f_fsid[1])
} }
#[cfg(not(any(target_os = "macos", target_os = "freebsd", target_os = "linux")))] #[cfg(not(any(target_os = "macos", target_os = "freebsd", target_os = "linux")))]
fn fsid(&self) -> u64 { fn fsid(&self) -> u64 {

View file

@ -31,7 +31,7 @@ fn bsd_sum(mut reader: Box<dyn Read>) -> (usize, u16) {
blocks_read += 1; blocks_read += 1;
for &byte in buf[..n].iter() { for &byte in buf[..n].iter() {
checksum = (checksum >> 1) + ((checksum & 1) << 15); checksum = (checksum >> 1) + ((checksum & 1) << 15);
checksum = checksum.wrapping_add(byte as u16); checksum = checksum.wrapping_add(u16::from(byte));
} }
} }
_ => break, _ => break,
@ -51,7 +51,7 @@ fn sysv_sum(mut reader: Box<dyn Read>) -> (usize, u16) {
Ok(n) if n != 0 => { Ok(n) if n != 0 => {
blocks_read += 1; blocks_read += 1;
for &byte in buf[..n].iter() { for &byte in buf[..n].iter() {
ret = ret.wrapping_add(byte as u32); ret = ret.wrapping_add(u32::from(byte));
} }
} }
_ => break, _ => break,

View file

@ -48,7 +48,7 @@ struct Settings {
impl Default for Settings { impl Default for Settings {
fn default() -> Settings { fn default() -> Settings {
Settings { Settings {
mode: FilterMode::Lines(10, '\n' as u8), mode: FilterMode::Lines(10, b'\n'),
sleep_msec: 1000, sleep_msec: 1000,
beginning: false, beginning: false,
follow: false, follow: false,
@ -63,7 +63,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
// handle obsolete -number syntax // handle obsolete -number syntax
let options = match obsolete(&args[1..]) { let options = match obsolete(&args[1..]) {
(args, Some(n)) => { (args, Some(n)) => {
settings.mode = FilterMode::Lines(n, '\n' as u8); settings.mode = FilterMode::Lines(n, b'\n');
args args
} }
(args, None) => args, (args, None) => args,
@ -146,7 +146,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
slice = &slice[1..]; slice = &slice[1..];
} }
match parse_size(slice) { match parse_size(slice) {
Ok(m) => settings.mode = FilterMode::Lines(m, '\n' as u8), Ok(m) => settings.mode = FilterMode::Lines(m, b'\n'),
Err(e) => { Err(e) => {
show_error!("{}", e.description()); show_error!("{}", e.description());
return 1; return 1;
@ -326,7 +326,7 @@ fn obsolete(options: &[String]) -> (Vec<String>, Option<u64>) {
let current = options[a].clone(); let current = options[a].clone();
let current = current.as_bytes(); let current = current.as_bytes();
if current.len() > 1 && current[0] == '-' as u8 { if current.len() > 1 && current[0] == b'-' {
let len = current.len(); let len = current.len();
for pos in 1..len { for pos in 1..len {
// Ensure that the argument is only made out of digits // Ensure that the argument is only made out of digits

View file

@ -219,7 +219,7 @@ fn unexpand(options: Options) {
for file in options.files.into_iter() { for file in options.files.into_iter() {
let mut fh = open(file); let mut fh = open(file);
while match fh.read_until(b'\n' as u8, &mut buf) { while match fh.read_until(b'\n', &mut buf) {
Ok(s) => s > 0, Ok(s) => s > 0,
Err(_) => !buf.is_empty(), Err(_) => !buf.is_empty(),
} { } {

View file

@ -99,7 +99,7 @@ impl Uniq {
if self.zero_terminated { if self.zero_terminated {
0 0
} else { } else {
b'\n' as u8 b'\n'
} }
} }