refactor: fix some lints from Clippy tool

This commit is contained in:
Rafał Harabień 2018-12-09 00:22:21 +01:00
parent 96339ff7e6
commit a3ab0098da
4 changed files with 59 additions and 61 deletions

View File

@ -499,17 +499,17 @@ fn format_bpb(options: &FormatVolumeOptions) -> io::Result<(BiosParameterBlock,
if let Some(volume_label_from_opts) = options.volume_label { if let Some(volume_label_from_opts) = options.volume_label {
volume_label.copy_from_slice(&volume_label_from_opts); volume_label.copy_from_slice(&volume_label_from_opts);
} else { } else {
volume_label.copy_from_slice("NO NAME ".as_bytes()); volume_label.copy_from_slice(b"NO NAME ");
} }
// setup fs_type_label field // setup fs_type_label field
let mut fs_type_label = [0u8; 8]; let mut fs_type_label = [0u8; 8];
let fs_type_label_str = match fat_type { let fs_type_label_str = match fat_type {
FatType::Fat12 => "FAT12 ", FatType::Fat12 => b"FAT12 ",
FatType::Fat16 => "FAT16 ", FatType::Fat16 => b"FAT16 ",
FatType::Fat32 => "FAT32 ", FatType::Fat32 => b"FAT32 ",
}; };
fs_type_label.copy_from_slice(fs_type_label_str.as_bytes()); fs_type_label.copy_from_slice(fs_type_label_str);
// create Bios Parameter Block struct // create Bios Parameter Block struct
let bpb = BiosParameterBlock { let bpb = BiosParameterBlock {
@ -554,7 +554,7 @@ pub(crate) fn format_boot_sector(options: &FormatVolumeOptions) -> io::Result<(B
let mut boot: BootSector = Default::default(); let mut boot: BootSector = Default::default();
let (bpb, fat_type) = format_bpb(options)?; let (bpb, fat_type) = format_bpb(options)?;
boot.bpb = bpb; boot.bpb = bpb;
boot.oem_name.copy_from_slice("MSWIN4.1".as_bytes()); boot.oem_name.copy_from_slice(b"MSWIN4.1");
// Boot code copied from FAT32 boot sector initialized by mkfs.fat // Boot code copied from FAT32 boot sector initialized by mkfs.fat
boot.bootjmp = [0xEB, 0x58, 0x90]; boot.bootjmp = [0xEB, 0x58, 0x90];
let boot_code: [u8; 129] = [ let boot_code: [u8; 129] = [

View File

@ -84,7 +84,7 @@ impl<'a, T: ReadWriteSeek> Seek for DirRawStream<'a, T> {
fn split_path<'c>(path: &'c str) -> (&'c str, Option<&'c str>) { fn split_path<'c>(path: &'c str) -> (&'c str, Option<&'c str>) {
// remove trailing slash and split into 2 components - top-most parent and rest // remove trailing slash and split into 2 components - top-most parent and rest
let mut path_split = path.trim_matches('/').splitn(2, "/"); let mut path_split = path.trim_matches('/').splitn(2, '/');
let comp = path_split.next().unwrap(); // SAFE: splitn always returns at least one element let comp = path_split.next().unwrap(); // SAFE: splitn always returns at least one element
let rest_opt = path_split.next(); let rest_opt = path_split.next();
(comp, rest_opt) (comp, rest_opt)
@ -262,7 +262,7 @@ impl<'a, T: ReadWriteSeek + 'a> Dir<'a, T> {
let e = r?; let e = r?;
let name = e.short_file_name_as_bytes(); let name = e.short_file_name_as_bytes();
// ignore special entries "." and ".." // ignore special entries "." and ".."
if name != ".".as_bytes() && name != "..".as_bytes() { if name != b"." && name != b".." {
return Ok(false); return Ok(false);
} }
} }
@ -324,7 +324,7 @@ impl<'a, T: ReadWriteSeek + 'a> Dir<'a, T> {
let (name, rest_opt) = split_path(dst_path); let (name, rest_opt) = split_path(dst_path);
if let Some(rest) = rest_opt { if let Some(rest) = rest_opt {
let e = dst_dir.find_entry(name, Some(true), None)?; let e = dst_dir.find_entry(name, Some(true), None)?;
return self.rename(src_path, &mut e.to_dir(), rest); return self.rename(src_path, &e.to_dir(), rest);
} }
// move/rename file // move/rename file
self.rename_internal(src_path, dst_dir, dst_path) self.rename_internal(src_path, dst_dir, dst_path)
@ -415,7 +415,7 @@ impl<'a, T: ReadWriteSeek + 'a> Dir<'a, T> {
() ()
} }
fn alloc_and_write_lfn_entries(&self, lfn_utf16: &LfnUtf16, short_name: &[u8]) -> io::Result<(DirRawStream<'a, T>, u64)> { fn alloc_and_write_lfn_entries(&self, lfn_utf16: &LfnUtf16, short_name: &[u8; 11]) -> io::Result<(DirRawStream<'a, T>, u64)> {
// get short name checksum // get short name checksum
let lfn_chsum = lfn_checksum(short_name); let lfn_chsum = lfn_checksum(short_name);
// create LFN entries generator // create LFN entries generator
@ -445,14 +445,14 @@ impl<'a, T: ReadWriteSeek + 'a> Dir<'a, T> {
let abs_pos = stream.abs_pos().map(|p| p - DIR_ENTRY_SIZE); let abs_pos = stream.abs_pos().map(|p| p - DIR_ENTRY_SIZE);
// return new logical entry descriptor // return new logical entry descriptor
let short_name = ShortName::new(raw_entry.name()); let short_name = ShortName::new(raw_entry.name());
return Ok(DirEntry { Ok(DirEntry {
data: raw_entry, data: raw_entry,
short_name, short_name,
lfn_utf16, lfn_utf16,
fs: self.fs, fs: self.fs,
entry_pos: abs_pos.unwrap(), // SAFE: abs_pos is absent only for empty file entry_pos: abs_pos.unwrap(), // SAFE: abs_pos is absent only for empty file
offset_range: (start_pos, end_pos), offset_range: (start_pos, end_pos),
}); })
} }
} }
@ -527,7 +527,7 @@ impl<'a, T: ReadWriteSeek> DirIter<'a, T> {
return Ok(Some(DirEntry { return Ok(Some(DirEntry {
data, data,
short_name, short_name,
lfn_utf16: lfn_buf.to_vec(), lfn_utf16: lfn_buf.into_vec(),
fs: self.fs, fs: self.fs,
entry_pos: abs_pos.unwrap(), // SAFE: abs_pos is empty only for empty file entry_pos: abs_pos.unwrap(), // SAFE: abs_pos is empty only for empty file
offset_range: (begin_offset, offset), offset_range: (begin_offset, offset),
@ -576,7 +576,7 @@ impl<'a, T: ReadWriteSeek> Iterator for DirIter<'a, T> {
fn validate_long_name(name: &str) -> io::Result<()> { fn validate_long_name(name: &str) -> io::Result<()> {
// check if length is valid // check if length is valid
if name.len() == 0 { if name.is_empty() {
return Err(io::Error::new(ErrorKind::Other, "File name is empty")); return Err(io::Error::new(ErrorKind::Other, "File name is empty"));
} }
if name.len() > 255 { if name.len() > 255 {
@ -595,10 +595,10 @@ fn validate_long_name(name: &str) -> io::Result<()> {
Ok(()) Ok(())
} }
fn lfn_checksum(short_name: &[u8]) -> u8 { fn lfn_checksum(short_name: &[u8; 11]) -> u8 {
let mut chksum = num::Wrapping(0u8); let mut chksum = num::Wrapping(0u8);
for i in 0..11 { for b in short_name {
chksum = (chksum << 7) + (chksum >> 1) + num::Wrapping(short_name[i]); chksum = (chksum << 7) + (chksum >> 1) + num::Wrapping(*b);
} }
chksum.0 chksum.0
} }
@ -625,7 +625,7 @@ impl LongNameBuilder {
self.index = 0; self.index = 0;
} }
fn to_vec(mut self) -> Vec<u16> { fn into_vec(mut self) -> Vec<u16> {
// Check if last processed entry had index 1 // Check if last processed entry had index 1
if self.index == 1 { if self.index == 1 {
self.truncate(); self.truncate();
@ -651,7 +651,7 @@ impl LongNameBuilder {
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
// Check if any LFN entry has been processed // Check if any LFN entry has been processed
// Note: index 0 is not a valid index in LFN and can be seen only after struct initialization // Note: index 0 is not a valid index in LFN and can be seen only after struct initialization
return self.index == 0; self.index == 0
} }
fn process(&mut self, data: &DirLfnEntryData) { fn process(&mut self, data: &DirLfnEntryData) {
@ -688,7 +688,7 @@ impl LongNameBuilder {
data.copy_name_to_slice(&mut self.buf[pos..pos + 13]); data.copy_name_to_slice(&mut self.buf[pos..pos + 13]);
} }
fn validate_chksum(&mut self, short_name: &[u8]) { fn validate_chksum(&mut self, short_name: &[u8; 11]) {
if self.is_empty() { if self.is_empty() {
// Nothing to validate - no LFN entries has been processed // Nothing to validate - no LFN entries has been processed
return; return;
@ -710,10 +710,10 @@ impl LongNameBuilder {
LongNameBuilder {} LongNameBuilder {}
} }
fn clear(&mut self) {} fn clear(&mut self) {}
fn to_vec(self) {} fn into_vec(self) {}
fn truncate(&mut self) {} fn truncate(&mut self) {}
fn process(&mut self, _data: &DirLfnEntryData) {} fn process(&mut self, _data: &DirLfnEntryData) {}
fn validate_chksum(&mut self, _short_name: &[u8]) {} fn validate_chksum(&mut self, _short_name: &[u8; 11]) {}
} }
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
@ -892,7 +892,7 @@ impl ShortNameGenerator {
} }
// check for long prefix form collision (TEXTFI~1.TXT) // check for long prefix form collision (TEXTFI~1.TXT)
let prefix_len = cmp::min(self.basename_len, 6) as usize; let prefix_len = cmp::min(self.basename_len, 6) as usize;
let num_suffix = if short_name[prefix_len] as char == '~' { let num_suffix = if short_name[prefix_len] == b'~' {
(short_name[prefix_len + 1] as char).to_digit(10) (short_name[prefix_len + 1] as char).to_digit(10)
} else { } else {
None None
@ -905,7 +905,7 @@ impl ShortNameGenerator {
// check for short prefix + checksum form collision (TE021F~1.TXT) // check for short prefix + checksum form collision (TE021F~1.TXT)
let prefix_len = cmp::min(self.basename_len, 2) as usize; let prefix_len = cmp::min(self.basename_len, 2) as usize;
let num_suffix = if short_name[prefix_len + 4] as char == '~' { let num_suffix = if short_name[prefix_len + 4] == b'~' {
(short_name[prefix_len + 4 + 1] as char).to_digit(10) (short_name[prefix_len + 4 + 1] as char).to_digit(10)
} else { } else {
None None
@ -970,7 +970,7 @@ impl ShortNameGenerator {
buf[..prefix_len].copy_from_slice(&self.short_name[..prefix_len]); buf[..prefix_len].copy_from_slice(&self.short_name[..prefix_len]);
prefix_len prefix_len
}; };
buf[prefix_len] = '~' as u8; buf[prefix_len] = b'~';
buf[prefix_len + 1] = char::from_digit(num, 10).unwrap() as u8; // SAFE buf[prefix_len + 1] = char::from_digit(num, 10).unwrap() as u8; // SAFE
buf[8..].copy_from_slice(&self.short_name[8..]); buf[8..].copy_from_slice(&self.short_name[8..]);
buf buf
@ -981,7 +981,7 @@ impl ShortNameGenerator {
let c2 = char::from_digit((x as u32 >> 8) & 0xF, 16).unwrap().to_ascii_uppercase() as u8; let c2 = char::from_digit((x as u32 >> 8) & 0xF, 16).unwrap().to_ascii_uppercase() as u8;
let c3 = char::from_digit((x as u32 >> 4) & 0xF, 16).unwrap().to_ascii_uppercase() as u8; let c3 = char::from_digit((x as u32 >> 4) & 0xF, 16).unwrap().to_ascii_uppercase() as u8;
let c4 = char::from_digit((x as u32 >> 0) & 0xF, 16).unwrap().to_ascii_uppercase() as u8; let c4 = char::from_digit((x as u32 >> 0) & 0xF, 16).unwrap().to_ascii_uppercase() as u8;
return [c1, c2, c3, c4]; [c1, c2, c3, c4]
} }
} }
@ -998,17 +998,15 @@ mod tests {
#[test] #[test]
fn test_generate_short_name() { fn test_generate_short_name() {
assert_eq!(&ShortNameGenerator::new("Foo").generate().unwrap(), "FOO ".as_bytes()); assert_eq!(&ShortNameGenerator::new("Foo").generate().unwrap(), b"FOO ");
assert_eq!(&ShortNameGenerator::new("Foo.b").generate().unwrap(), "FOO B ".as_bytes()); assert_eq!(&ShortNameGenerator::new("Foo.b").generate().unwrap(), b"FOO B ");
assert_eq!(&ShortNameGenerator::new("Foo.baR").generate().unwrap(), "FOO BAR".as_bytes()); assert_eq!(&ShortNameGenerator::new("Foo.baR").generate().unwrap(), b"FOO BAR");
assert_eq!(&ShortNameGenerator::new("Foo+1.baR").generate().unwrap(), "FOO_1~1 BAR".as_bytes()); assert_eq!(&ShortNameGenerator::new("Foo+1.baR").generate().unwrap(), b"FOO_1~1 BAR");
assert_eq!( assert_eq!(
&ShortNameGenerator::new("ver +1.2.text").generate().unwrap(), &ShortNameGenerator::new("ver +1.2.text").generate().unwrap(), b"VER_12~1TEX"
"VER_12~1TEX".as_bytes()
); );
assert_eq!( assert_eq!(
&ShortNameGenerator::new(".bashrc.swp").generate().unwrap(), &ShortNameGenerator::new(".bashrc.swp").generate().unwrap(), b"BASHRC~1SWP"
"BASHRC~1SWP".as_bytes()
); );
} }
@ -1029,22 +1027,22 @@ mod tests {
let mut buf: [u8; 11]; let mut buf: [u8; 11];
let mut gen = ShortNameGenerator::new("TextFile.Mine.txt"); let mut gen = ShortNameGenerator::new("TextFile.Mine.txt");
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "TEXTFI~1TXT".as_bytes()); assert_eq!(&buf, b"TEXTFI~1TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "TEXTFI~2TXT".as_bytes()); assert_eq!(&buf, b"TEXTFI~2TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "TEXTFI~3TXT".as_bytes()); assert_eq!(&buf, b"TEXTFI~3TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "TEXTFI~4TXT".as_bytes()); assert_eq!(&buf, b"TEXTFI~4TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "TE527D~1TXT".as_bytes()); assert_eq!(&buf, b"TE527D~1TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "TE527D~2TXT".as_bytes()); assert_eq!(&buf, b"TE527D~2TXT");
for i in 3..10 { for i in 3..10 {
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
@ -1058,7 +1056,7 @@ mod tests {
gen.add_existing(&buf); gen.add_existing(&buf);
} }
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "TE527E~1TXT".as_bytes()); assert_eq!(&buf, b"TE527E~1TXT");
} }
#[test] #[test]
@ -1066,24 +1064,24 @@ mod tests {
let mut buf: [u8; 11]; let mut buf: [u8; 11];
let mut gen = ShortNameGenerator::new("x.txt"); let mut gen = ShortNameGenerator::new("x.txt");
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "X TXT".as_bytes()); assert_eq!(&buf, b"X TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "X~1 TXT".as_bytes()); assert_eq!(&buf, b"X~1 TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "X~2 TXT".as_bytes()); assert_eq!(&buf, b"X~2 TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "X~3 TXT".as_bytes()); assert_eq!(&buf, b"X~3 TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "X~4 TXT".as_bytes()); assert_eq!(&buf, b"X~4 TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "X40DA~1 TXT".as_bytes()); assert_eq!(&buf, b"X40DA~1 TXT");
gen.add_existing(&buf); gen.add_existing(&buf);
buf = gen.generate().unwrap(); buf = gen.generate().unwrap();
assert_eq!(&buf, "X40DA~2 TXT".as_bytes()); assert_eq!(&buf, b"X40DA~2 TXT");
} }
} }

View File

@ -51,7 +51,7 @@ pub(crate) struct ShortName {
} }
impl ShortName { impl ShortName {
const PADDING: u8 = ' ' as u8; const PADDING: u8 = b' ';
pub(crate) fn new(raw_name: &[u8; 11]) -> Self { pub(crate) fn new(raw_name: &[u8; 11]) -> Self {
// get name components length by looking for space character // get name components length by looking for space character
@ -64,7 +64,7 @@ impl ShortName {
let mut name = [Self::PADDING; 12]; let mut name = [Self::PADDING; 12];
name[..name_len].copy_from_slice(&raw_name[..name_len]); name[..name_len].copy_from_slice(&raw_name[..name_len]);
let total_len = if ext_len > 0 { let total_len = if ext_len > 0 {
name[name_len] = '.' as u8; name[name_len] = b'.';
name[name_len + 1..name_len + 1 + ext_len].copy_from_slice(&raw_name[8..8 + ext_len]); name[name_len + 1..name_len + 1 + ext_len].copy_from_slice(&raw_name[8..8 + ext_len]);
// Return total name length // Return total name length
name_len + 1 + ext_len name_len + 1 + ext_len
@ -529,10 +529,10 @@ impl<'a, T: ReadWriteSeek> DirEntry<'a, T> {
/// Returns long file name or if it doesn't exist fallbacks to short file name. /// Returns long file name or if it doesn't exist fallbacks to short file name.
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
pub fn file_name(&self) -> String { pub fn file_name(&self) -> String {
if self.lfn_utf16.len() > 0 { if self.lfn_utf16.is_empty() {
String::from_utf16_lossy(&self.lfn_utf16)
} else {
self.data.lowercase_name().to_string(self.fs.options.oem_cp_converter) self.data.lowercase_name().to_string(self.fs.options.oem_cp_converter)
} else {
String::from_utf16_lossy(&self.lfn_utf16)
} }
} }
@ -642,9 +642,9 @@ mod tests {
#[test] #[test]
fn short_name_with_ext() { fn short_name_with_ext() {
let mut raw_short_name = [0u8; 11]; let mut raw_short_name = [0u8; 11];
raw_short_name.copy_from_slice("FOO BAR".as_bytes()); raw_short_name.copy_from_slice(b"FOO BAR");
assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "FOO.BAR"); assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "FOO.BAR");
raw_short_name.copy_from_slice("LOOK AT M E".as_bytes()); raw_short_name.copy_from_slice(b"LOOK AT M E");
assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "LOOK AT.M E"); assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "LOOK AT.M E");
raw_short_name[0] = 0x99; raw_short_name[0] = 0x99;
raw_short_name[10] = 0x99; raw_short_name[10] = 0x99;
@ -661,16 +661,16 @@ mod tests {
#[test] #[test]
fn short_name_without_ext() { fn short_name_without_ext() {
let mut raw_short_name = [0u8; 11]; let mut raw_short_name = [0u8; 11];
raw_short_name.copy_from_slice("FOO ".as_bytes()); raw_short_name.copy_from_slice(b"FOO ");
assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "FOO"); assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "FOO");
raw_short_name.copy_from_slice("LOOK AT ".as_bytes()); raw_short_name.copy_from_slice(b"LOOK AT ");
assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "LOOK AT"); assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "LOOK AT");
} }
#[test] #[test]
fn short_name_eq_ignore_case() { fn short_name_eq_ignore_case() {
let mut raw_short_name = [0u8; 11]; let mut raw_short_name = [0u8; 11];
raw_short_name.copy_from_slice("LOOK AT M E".as_bytes()); raw_short_name.copy_from_slice(b"LOOK AT M E");
raw_short_name[0] = 0x99; raw_short_name[0] = 0x99;
raw_short_name[10] = 0x99; raw_short_name[10] = 0x99;
assert_eq!( assert_eq!(
@ -688,14 +688,14 @@ mod tests {
let raw_short_name = [0x05; 11]; let raw_short_name = [0x05; 11];
assert_eq!( assert_eq!(
ShortName::new(&raw_short_name).as_bytes(), ShortName::new(&raw_short_name).as_bytes(),
[0xE5, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, '.' as u8, 0x05, 0x05, 0x05] [0xE5, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, b'.', 0x05, 0x05, 0x05]
); );
} }
#[test] #[test]
fn lowercase_short_name() { fn lowercase_short_name() {
let mut raw_short_name = [0u8; 11]; let mut raw_short_name = [0u8; 11];
raw_short_name.copy_from_slice("FOO RS ".as_bytes()); raw_short_name.copy_from_slice(b"FOO RS ");
let mut raw_entry = DirFileEntryData { let mut raw_entry = DirFileEntryData {
name: raw_short_name, name: raw_short_name,
reserved_0: (1 << 3) | (1 << 4), reserved_0: (1 << 3) | (1 << 4),

View File

@ -456,7 +456,7 @@ impl<T: ReadWriteSeek> Iterator for ClusterIterator<T> {
}, },
} }
} }
self.cluster.map(|n| Ok(n)) self.cluster.map(Ok)
} }
} }