Skip to content

Commit ac5a913

Browse files
committed
chore: fix ref_option lint
Resolve [ref_option](https://rust-lang.github.io/rust-clippy/master/index.html#ref_option) lint. Also, I optimized `is_first_filename_timestamp` a bit to make it faster and more streamlined. p.s. disclaimer: I'm the author of that lint
1 parent da61ebf commit ac5a913

File tree

11 files changed

+45
-44
lines changed

11 files changed

+45
-44
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,4 @@ unused_qualifications = "warn"
599599
[workspace.lints.clippy]
600600
all = { level = "deny", priority = -1 }
601601
#cargo = { level = "warn", priority = -1 }
602+
ref_option = "warn"

src/uu/chroot/src/chroot.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ fn handle_missing_groups(strategy: Strategy) -> Result<(), ChrootError> {
390390
/// Set supplemental groups for this process.
391391
fn set_supplemental_gids_with_strategy(
392392
strategy: Strategy,
393-
groups: &Option<Vec<String>>,
393+
groups: Option<&Vec<String>>,
394394
) -> Result<(), ChrootError> {
395395
match groups {
396396
None => handle_missing_groups(strategy),
@@ -410,27 +410,27 @@ fn set_context(options: &Options) -> UResult<()> {
410410
match &options.userspec {
411411
None | Some(UserSpec::NeitherGroupNorUser) => {
412412
let strategy = Strategy::Nothing;
413-
set_supplemental_gids_with_strategy(strategy, &options.groups)?;
413+
set_supplemental_gids_with_strategy(strategy, options.groups.as_ref())?;
414414
}
415415
Some(UserSpec::UserOnly(user)) => {
416416
let uid = name_to_uid(user)?;
417417
let gid = uid as libc::gid_t;
418418
let strategy = Strategy::FromUID(uid, false);
419-
set_supplemental_gids_with_strategy(strategy, &options.groups)?;
419+
set_supplemental_gids_with_strategy(strategy, options.groups.as_ref())?;
420420
set_gid(gid).map_err(|e| ChrootError::SetGidFailed(user.to_string(), e))?;
421421
set_uid(uid).map_err(|e| ChrootError::SetUserFailed(user.to_string(), e))?;
422422
}
423423
Some(UserSpec::GroupOnly(group)) => {
424424
let gid = name_to_gid(group)?;
425425
let strategy = Strategy::Nothing;
426-
set_supplemental_gids_with_strategy(strategy, &options.groups)?;
426+
set_supplemental_gids_with_strategy(strategy, options.groups.as_ref())?;
427427
set_gid(gid).map_err(|e| ChrootError::SetGidFailed(group.to_string(), e))?;
428428
}
429429
Some(UserSpec::UserAndGroup(user, group)) => {
430430
let uid = name_to_uid(user)?;
431431
let gid = name_to_gid(group)?;
432432
let strategy = Strategy::FromUID(uid, true);
433-
set_supplemental_gids_with_strategy(strategy, &options.groups)?;
433+
set_supplemental_gids_with_strategy(strategy, options.groups.as_ref())?;
434434
set_gid(gid).map_err(|e| ChrootError::SetGidFailed(group.to_string(), e))?;
435435
set_uid(uid).map_err(|e| ChrootError::SetUserFailed(user.to_string(), e))?;
436436
}

src/uu/cp/src/copydir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ where
224224
#[allow(clippy::too_many_arguments)]
225225
/// Copy a single entry during a directory traversal.
226226
fn copy_direntry(
227-
progress_bar: &Option<ProgressBar>,
227+
progress_bar: Option<&ProgressBar>,
228228
entry: Entry,
229229
options: &Options,
230230
symlinked_files: &mut HashSet<FileInformation>,
@@ -314,7 +314,7 @@ fn copy_direntry(
314314
/// will not cause a short-circuit.
315315
#[allow(clippy::too_many_arguments)]
316316
pub(crate) fn copy_directory(
317-
progress_bar: &Option<ProgressBar>,
317+
progress_bar: Option<&ProgressBar>,
318318
root: &Path,
319319
target: &Path,
320320
options: &Options,

src/uu/cp/src/cp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
13661366
}
13671367

13681368
if let Err(error) = copy_source(
1369-
&progress_bar,
1369+
progress_bar.as_ref(),
13701370
source,
13711371
target,
13721372
target_type,
@@ -1433,7 +1433,7 @@ fn construct_dest_path(
14331433
}
14341434
#[allow(clippy::too_many_arguments)]
14351435
fn copy_source(
1436-
progress_bar: &Option<ProgressBar>,
1436+
progress_bar: Option<&ProgressBar>,
14371437
source: &Path,
14381438
target: &Path,
14391439
target_type: TargetType,
@@ -1979,7 +1979,7 @@ fn aligned_ancestors<'a>(source: &'a Path, dest: &'a Path) -> Vec<(&'a Path, &'a
19791979

19801980
fn print_verbose_output(
19811981
parents: bool,
1982-
progress_bar: &Option<ProgressBar>,
1982+
progress_bar: Option<&ProgressBar>,
19831983
source: &Path,
19841984
dest: &Path,
19851985
) {
@@ -2207,7 +2207,7 @@ fn calculate_dest_permissions(
22072207
/// after a successful copy.
22082208
#[allow(clippy::cognitive_complexity, clippy::too_many_arguments)]
22092209
fn copy_file(
2210-
progress_bar: &Option<ProgressBar>,
2210+
progress_bar: Option<&ProgressBar>,
22112211
source: &Path,
22122212
dest: &Path,
22132213
options: &Options,

src/uu/dd/src/dd.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,13 +1106,13 @@ fn dd_copy(mut i: Input, o: Output) -> io::Result<()> {
11061106
// blocks to this output. Read/write statistics are updated on
11071107
// each iteration and cumulative statistics are reported to
11081108
// the progress reporting thread.
1109-
while below_count_limit(&i.settings.count, &rstat) {
1109+
while below_count_limit(i.settings.count, &rstat) {
11101110
// Read a block from the input then write the block to the output.
11111111
//
11121112
// As an optimization, make an educated guess about the
11131113
// best buffer size for reading based on the number of
11141114
// blocks already read and the number of blocks remaining.
1115-
let loop_bsize = calc_loop_bsize(&i.settings.count, &rstat, &wstat, i.settings.ibs, bsize);
1115+
let loop_bsize = calc_loop_bsize(i.settings.count, &rstat, &wstat, i.settings.ibs, bsize);
11161116
let rstat_update = read_helper(&mut i, &mut buf, loop_bsize)?;
11171117
if rstat_update.is_empty() {
11181118
break;
@@ -1295,7 +1295,7 @@ fn calc_bsize(ibs: usize, obs: usize) -> usize {
12951295
// Calculate the buffer size appropriate for this loop iteration, respecting
12961296
// a count=N if present.
12971297
fn calc_loop_bsize(
1298-
count: &Option<Num>,
1298+
count: Option<Num>,
12991299
rstat: &ReadStat,
13001300
wstat: &WriteStat,
13011301
ibs: usize,
@@ -1308,7 +1308,7 @@ fn calc_loop_bsize(
13081308
cmp::min(ideal_bsize as u64, rremain * ibs as u64) as usize
13091309
}
13101310
Some(Num::Bytes(bmax)) => {
1311-
let bmax: u128 = (*bmax).into();
1311+
let bmax: u128 = bmax.into();
13121312
let bremain: u128 = bmax - wstat.bytes_total;
13131313
cmp::min(ideal_bsize as u128, bremain) as usize
13141314
}
@@ -1318,10 +1318,10 @@ fn calc_loop_bsize(
13181318

13191319
// Decide if the current progress is below a count=N limit or return
13201320
// true if no such limit is set.
1321-
fn below_count_limit(count: &Option<Num>, rstat: &ReadStat) -> bool {
1321+
fn below_count_limit(count: Option<Num>, rstat: &ReadStat) -> bool {
13221322
match count {
1323-
Some(Num::Blocks(n)) => rstat.reads_complete + rstat.reads_partial < *n,
1324-
Some(Num::Bytes(n)) => rstat.bytes_total < *n,
1323+
Some(Num::Blocks(n)) => rstat.reads_complete + rstat.reads_partial < n,
1324+
Some(Num::Bytes(n)) => rstat.bytes_total < n,
13251325
None => true,
13261326
}
13271327
}

src/uu/pr/src/pr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ fn write_columns(
10851085
for (i, cell) in row.iter().enumerate() {
10861086
if cell.is_none() && options.merge_files_print.is_some() {
10871087
out.write_all(
1088-
get_line_for_printing(options, &blank_line, columns, i, &line_width, indexes)
1088+
get_line_for_printing(options, &blank_line, columns, i, line_width, indexes)
10891089
.as_bytes(),
10901090
)?;
10911091
} else if cell.is_none() {
@@ -1095,7 +1095,7 @@ fn write_columns(
10951095
let file_line = cell.unwrap();
10961096

10971097
out.write_all(
1098-
get_line_for_printing(options, file_line, columns, i, &line_width, indexes)
1098+
get_line_for_printing(options, file_line, columns, i, line_width, indexes)
10991099
.as_bytes(),
11001100
)?;
11011101
lines_printed += 1;
@@ -1116,7 +1116,7 @@ fn get_line_for_printing(
11161116
file_line: &FileLine,
11171117
columns: usize,
11181118
index: usize,
1119-
line_width: &Option<usize>,
1119+
line_width: Option<usize>,
11201120
indexes: usize,
11211121
) -> String {
11221122
let blank_line = String::new();

src/uu/split/src/platform/unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Drop for FilterWriter {
121121

122122
/// Instantiate either a file writer or a "write to shell process's stdin" writer
123123
pub fn instantiate_current_writer(
124-
filter: &Option<String>,
124+
filter: Option<&str>,
125125
filename: &str,
126126
is_new: bool,
127127
) -> Result<BufWriter<Box<dyn Write>>> {

src/uu/split/src/platform/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use uucore::fs;
1212
/// Unlike the unix version of this function, this _always_ returns
1313
/// a file writer
1414
pub fn instantiate_current_writer(
15-
_filter: &Option<String>,
15+
_filter: Option<&str>,
1616
filename: &str,
1717
is_new: bool,
1818
) -> Result<BufWriter<Box<dyn Write>>> {

src/uu/split/src/split.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5555
let (args, obs_lines) = handle_obsolete(args);
5656
let matches = uu_app().try_get_matches_from(args)?;
5757

58-
match Settings::from(&matches, &obs_lines) {
58+
match Settings::from(&matches, obs_lines.as_deref()) {
5959
Ok(settings) => split(&settings),
6060
Err(e) if e.requires_usage() => Err(UUsageError::new(1, format!("{e}"))),
6161
Err(e) => Err(USimpleError::new(1, format!("{e}"))),
@@ -460,7 +460,7 @@ impl SettingsError {
460460

461461
impl Settings {
462462
/// Parse a strategy from the command-line arguments.
463-
fn from(matches: &ArgMatches, obs_lines: &Option<String>) -> Result<Self, SettingsError> {
463+
fn from(matches: &ArgMatches, obs_lines: Option<&str>) -> Result<Self, SettingsError> {
464464
let strategy = Strategy::from(matches, obs_lines).map_err(SettingsError::Strategy)?;
465465
let suffix = Suffix::from(matches, &strategy).map_err(SettingsError::Suffix)?;
466466

@@ -539,7 +539,7 @@ impl Settings {
539539
));
540540
}
541541

542-
platform::instantiate_current_writer(&self.filter, filename, is_new)
542+
platform::instantiate_current_writer(self.filter.as_deref(), filename, is_new)
543543
}
544544
}
545545

@@ -605,14 +605,14 @@ fn get_input_size<R>(
605605
input: &String,
606606
reader: &mut R,
607607
buf: &mut Vec<u8>,
608-
io_blksize: &Option<u64>,
608+
io_blksize: Option<u64>,
609609
) -> io::Result<u64>
610610
where
611611
R: BufRead,
612612
{
613613
// Set read limit to io_blksize if specified
614614
let read_limit: u64 = if let Some(custom_blksize) = io_blksize {
615-
*custom_blksize
615+
custom_blksize
616616
} else {
617617
// otherwise try to get it from filesystem, or use default
618618
uucore::fs::sane_blksize::sane_blksize_from_path(Path::new(input))
@@ -1082,7 +1082,7 @@ where
10821082
{
10831083
// Get the size of the input in bytes
10841084
let initial_buf = &mut Vec::new();
1085-
let mut num_bytes = get_input_size(&settings.input, reader, initial_buf, &settings.io_blksize)?;
1085+
let mut num_bytes = get_input_size(&settings.input, reader, initial_buf, settings.io_blksize)?;
10861086
let mut reader = initial_buf.chain(reader);
10871087

10881088
// If input file is empty and we would not have determined the Kth chunk
@@ -1228,7 +1228,7 @@ where
12281228
// Get the size of the input in bytes and compute the number
12291229
// of bytes per chunk.
12301230
let initial_buf = &mut Vec::new();
1231-
let num_bytes = get_input_size(&settings.input, reader, initial_buf, &settings.io_blksize)?;
1231+
let num_bytes = get_input_size(&settings.input, reader, initial_buf, settings.io_blksize)?;
12321232
let reader = initial_buf.chain(reader);
12331233

12341234
// If input file is empty and we would not have determined the Kth chunk

src/uu/split/src/strategy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ pub enum StrategyError {
214214

215215
impl Strategy {
216216
/// Parse a strategy from the command-line arguments.
217-
pub fn from(matches: &ArgMatches, obs_lines: &Option<String>) -> Result<Self, StrategyError> {
217+
pub fn from(matches: &ArgMatches, obs_lines: Option<&str>) -> Result<Self, StrategyError> {
218218
fn get_and_parse(
219219
matches: &ArgMatches,
220220
option: &str,

src/uu/touch/src/touch.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,20 @@ fn get_year(s: &str) -> u8 {
156156
fn is_first_filename_timestamp(
157157
reference: Option<&OsString>,
158158
date: Option<&str>,
159-
timestamp: &Option<String>,
159+
timestamp: Option<&str>,
160160
files: &[&String],
161161
) -> bool {
162-
match std::env::var("_POSIX2_VERSION") {
163-
Ok(s) if s == "199209" => {
164-
if timestamp.is_none() && reference.is_none() && date.is_none() && files.len() >= 2 {
165-
let s = files[0];
166-
all_digits(s)
167-
&& (s.len() == 8 || (s.len() == 10 && (69..=99).contains(&get_year(s))))
168-
} else {
169-
false
170-
}
171-
}
172-
_ => false,
162+
if timestamp.is_none()
163+
&& reference.is_none()
164+
&& date.is_none()
165+
&& files.len() >= 2
166+
// env check is last as the slowest op
167+
&& matches!(std::env::var("_POSIX2_VERSION").as_deref(), Ok("199209"))
168+
{
169+
let s = files[0];
170+
all_digits(s) && (s.len() == 8 || (s.len() == 10 && (69..=99).contains(&get_year(s))))
171+
} else {
172+
false
173173
}
174174
}
175175

@@ -213,7 +213,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
213213
.get_one::<String>(options::sources::TIMESTAMP)
214214
.map(|t| t.to_owned());
215215

216-
if is_first_filename_timestamp(reference, date.as_deref(), &timestamp, &filenames) {
216+
if is_first_filename_timestamp(reference, date.as_deref(), timestamp.as_deref(), &filenames) {
217217
timestamp = if filenames[0].len() == 10 {
218218
Some(shr2(filenames[0]))
219219
} else {

0 commit comments

Comments
 (0)