Skip to content

Commit d9e7071

Browse files
committed
style: partially migrate if-let to match or let-else
Following <https://users.rust-lang.org/t/rust-2024-compatibility-lint-and-the-if-let-temporary-scope/125969/5>, I have manually audited all `if_let_rescope` cases and found out that certain existing cases can be rewritten to make it clearer semantically. So these migrations have been made here in advance so what remains is false positives of this lint. The crux of determining false positives here is that, according to <rust-lang/rust#133167>, `if let Some()` and other similar cases where the `else` drop is not significant are not affected by the Edition change.
1 parent 39526f9 commit d9e7071

File tree

3 files changed

+65
-74
lines changed

3 files changed

+65
-74
lines changed

src/diskio/immediate.rs

+29-33
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,20 @@ impl Executor for ImmediateUnpacker {
8181
// If there is a pending error, return it, otherwise stash the
8282
// Item for eventual return when the file is finished.
8383
let mut guard = self.incremental_state.lock().unwrap();
84-
if let Some(ref mut state) = *guard {
85-
if state.err.is_some() {
86-
let err = state.err.take().unwrap();
87-
item.result = err;
88-
item.finish = item
89-
.start
90-
.map(|s| Instant::now().saturating_duration_since(s));
91-
*guard = None;
92-
Box::new(Some(CompletedIo::Item(item)).into_iter())
93-
} else {
94-
state.item = Some(item);
95-
Box::new(None.into_iter())
96-
}
84+
let Some(ref mut state) = *guard else {
85+
unreachable!()
86+
};
87+
if state.err.is_some() {
88+
let err = state.err.take().unwrap();
89+
item.result = err;
90+
item.finish = item
91+
.start
92+
.map(|s| Instant::now().saturating_duration_since(s));
93+
*guard = None;
94+
Box::new(Some(CompletedIo::Item(item)).into_iter())
9795
} else {
98-
unreachable!();
96+
state.item = Some(item);
97+
Box::new(None.into_iter())
9998
}
10099
};
101100
}
@@ -181,9 +180,7 @@ impl IncrementalFileWriter {
181180
if (self.state.lock().unwrap()).is_none() {
182181
return false;
183182
}
184-
let chunk = if let FileBuffer::Immediate(v) = chunk {
185-
v
186-
} else {
183+
let FileBuffer::Immediate(chunk) = chunk else {
187184
unreachable!()
188185
};
189186
match self.write(chunk) {
@@ -203,25 +200,24 @@ impl IncrementalFileWriter {
203200

204201
fn write(&mut self, chunk: Vec<u8>) -> std::result::Result<bool, io::Error> {
205202
let mut state = self.state.lock().unwrap();
206-
if let Some(ref mut state) = *state {
207-
if let Some(ref mut file) = self.file.as_mut() {
208-
// Length 0 vector is used for clean EOF signalling.
209-
if chunk.is_empty() {
210-
trace_scoped!("close", "name:": self.path_display);
211-
drop(std::mem::take(&mut self.file));
212-
state.finished = true;
213-
} else {
214-
trace_scoped!("write_segment", "name": self.path_display, "len": chunk.len());
215-
file.write_all(&chunk)?;
216-
217-
state.completed_chunks.push(chunk.len());
218-
}
219-
Ok(true)
203+
let Some(ref mut state) = *state else {
204+
unreachable!()
205+
};
206+
if let Some(ref mut file) = self.file.as_mut() {
207+
// Length 0 vector is used for clean EOF signalling.
208+
if chunk.is_empty() {
209+
trace_scoped!("close", "name:": self.path_display);
210+
drop(std::mem::take(&mut self.file));
211+
state.finished = true;
220212
} else {
221-
Ok(false)
213+
trace_scoped!("write_segment", "name": self.path_display, "len": chunk.len());
214+
file.write_all(&chunk)?;
215+
216+
state.completed_chunks.push(chunk.len());
222217
}
218+
Ok(true)
223219
} else {
224-
unreachable!();
220+
Ok(false)
225221
}
226222
}
227223
}

src/dist/component/package.rs

+35-36
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,9 @@ fn filter_result(op: &mut CompletedIo) -> io::Result<()> {
229229
// mkdir of e.g. ~/.rustup already existing is just fine;
230230
// for others it would be better to know whether it is
231231
// expected to exist or not -so put a flag in the state.
232-
if let Kind::Directory = op.kind {
233-
Ok(())
234-
} else {
235-
Err(e)
232+
match op.kind {
233+
Kind::Directory => Ok(()),
234+
_ => Err(e),
236235
}
237236
}
238237
_ => Err(e),
@@ -454,40 +453,40 @@ fn unpack_without_first_dir<R: Read>(
454453

455454
let item = loop {
456455
// Create the full path to the entry if it does not exist already
457-
if let Some(parent) = item.full_path.to_owned().parent() {
458-
match directories.get_mut(parent) {
459-
None => {
460-
// Tar has item before containing directory
461-
// Complain about this so we can see if these exist.
462-
writeln!(
463-
process.stderr().lock(),
464-
"Unexpected: missing parent '{}' for '{}'",
465-
parent.display(),
466-
entry.path()?.display()
467-
)?;
468-
directories.insert(parent.to_owned(), DirStatus::Pending(vec![item]));
469-
item = Item::make_dir(parent.to_owned(), 0o755);
470-
// Check the parent's parent
471-
continue;
472-
}
473-
Some(DirStatus::Exists) => {
474-
break Some(item);
475-
}
476-
Some(DirStatus::Pending(pending)) => {
477-
// Parent dir is being made
478-
pending.push(item);
479-
if incremental_file_sender.is_none() {
480-
// take next item from tar
481-
continue 'entries;
482-
} else {
483-
// don't submit a new item for processing, but do be ready to feed data to the incremental file.
484-
break None;
485-
}
456+
let full_path = item.full_path.to_owned();
457+
let Some(parent) = full_path.parent() else {
458+
// We should never see a path with no parent.
459+
unreachable!()
460+
};
461+
match directories.get_mut(parent) {
462+
None => {
463+
// Tar has item before containing directory
464+
// Complain about this so we can see if these exist.
465+
writeln!(
466+
process.stderr().lock(),
467+
"Unexpected: missing parent '{}' for '{}'",
468+
parent.display(),
469+
entry.path()?.display()
470+
)?;
471+
directories.insert(parent.to_owned(), DirStatus::Pending(vec![item]));
472+
item = Item::make_dir(parent.to_owned(), 0o755);
473+
// Check the parent's parent
474+
continue;
475+
}
476+
Some(DirStatus::Exists) => {
477+
break Some(item);
478+
}
479+
Some(DirStatus::Pending(pending)) => {
480+
// Parent dir is being made
481+
pending.push(item);
482+
if incremental_file_sender.is_none() {
483+
// take next item from tar
484+
continue 'entries;
485+
} else {
486+
// don't submit a new item for processing, but do be ready to feed data to the incremental file.
487+
break None;
486488
}
487489
}
488-
} else {
489-
// We should never see a path with no parent.
490-
panic!();
491490
}
492491
};
493492

src/dist/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,7 @@ impl FromStr for ParsedToolchainDesc {
335335
}
336336
});
337337

338-
if let Some(d) = d {
339-
Ok(d)
340-
} else {
341-
Err(RustupError::InvalidToolchainName(desc.to_string()).into())
342-
}
338+
d.ok_or_else(|| RustupError::InvalidToolchainName(desc.to_string()).into())
343339
}
344340
}
345341

0 commit comments

Comments
 (0)