Skip to content

Commit 91235c7

Browse files
committed
Rewrite some patterns with let-else and ok_or_else (#2404)
This Pull Request updates the codebase to the newest version of rustc (1.65.0). It changes the following: - Bumps `rust-version` to 1.65.0. - Rewrites some snippets to use the new let else, ok_or_else and some other utils. - Removes the `rustdoc::missing_doc_code_examples` allow lint from our codebase. (Context: rust-lang/rust#101732)
1 parent dc3b09a commit 91235c7

File tree

36 files changed

+259
-518
lines changed

36 files changed

+259
-518
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ members = [
1616
[workspace.package]
1717
edition = "2021"
1818
version = "0.16.0"
19-
rust-version = "1.64"
19+
rust-version = "1.65"
2020
authors = ["boa-dev"]
2121
repository = "https://github.com/boa-dev/boa"
2222
license = "Unlicense/MIT"

boa_ast/src/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@
4949
nonstandard_style,
5050
missing_docs
5151
)]
52-
#![allow(
53-
clippy::module_name_repetitions,
54-
clippy::too_many_lines,
55-
rustdoc::missing_doc_code_examples
56-
)]
52+
#![allow(clippy::module_name_repetitions, clippy::too_many_lines)]
5753

5854
mod position;
5955
mod punctuator;

boa_engine/src/bigint.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,10 @@ impl JsBigInt {
149149

150150
#[inline]
151151
pub fn pow(x: &Self, y: &Self) -> JsResult<Self> {
152-
let y = if let Some(y) = y.inner.to_biguint() {
153-
y
154-
} else {
155-
return Err(JsNativeError::range()
156-
.with_message("BigInt negative exponent")
157-
.into());
158-
};
152+
let y = y
153+
.inner
154+
.to_biguint()
155+
.ok_or_else(|| JsNativeError::range().with_message("BigInt negative exponent"))?;
159156

160157
let num_bits = (x.inner.bits() as f64
161158
* y.to_f64().expect("Unable to convert from BigUInt to f64"))

boa_engine/src/builtins/array/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,7 @@ impl Array {
300300
/// by `Array.prototype.concat`.
301301
fn is_concat_spreadable(o: &JsValue, context: &mut Context) -> JsResult<bool> {
302302
// 1. If Type(O) is not Object, return false.
303-
let o = if let Some(o) = o.as_object() {
304-
o
305-
} else {
303+
let Some(o) = o.as_object() else {
306304
return Ok(false);
307305
};
308306

@@ -461,9 +459,7 @@ impl Array {
461459
let next = iterator_record.step(context)?;
462460

463461
// iv. If next is false, then
464-
let next = if let Some(next) = next {
465-
next
466-
} else {
462+
let Some(next) = next else {
467463
// 1. Perform ? Set(A, "length", 𝔽(k), true).
468464
a.set("length", k, true, context)?;
469465

boa_engine/src/builtins/array_buffer/mod.rs

+21-41
Original file line numberDiff line numberDiff line change
@@ -148,33 +148,25 @@ impl ArrayBuffer {
148148
) -> JsResult<JsValue> {
149149
// 1. Let O be the this value.
150150
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
151-
let obj = if let Some(obj) = this.as_object() {
152-
obj
153-
} else {
154-
return Err(JsNativeError::typ()
155-
.with_message("ArrayBuffer.byteLength called with non-object value")
156-
.into());
157-
};
151+
let obj = this.as_object().ok_or_else(|| {
152+
JsNativeError::typ().with_message("ArrayBuffer.byteLength called with non-object value")
153+
})?;
158154
let obj = obj.borrow();
159-
let o = if let Some(o) = obj.as_array_buffer() {
160-
o
161-
} else {
162-
return Err(JsNativeError::typ()
163-
.with_message("ArrayBuffer.byteLength called with invalid object")
164-
.into());
165-
};
155+
let buf = obj.as_array_buffer().ok_or_else(|| {
156+
JsNativeError::typ().with_message("ArrayBuffer.byteLength called with invalid object")
157+
})?;
166158

167159
// TODO: Shared Array Buffer
168160
// 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
169161

170162
// 4. If IsDetachedBuffer(O) is true, return +0𝔽.
171-
if Self::is_detached_buffer(o) {
163+
if Self::is_detached_buffer(buf) {
172164
return Ok(0.into());
173165
}
174166

175167
// 5. Let length be O.[[ArrayBufferByteLength]].
176168
// 6. Return 𝔽(length).
177-
Ok(o.array_buffer_byte_length.into())
169+
Ok(buf.array_buffer_byte_length.into())
178170
}
179171

180172
/// `25.1.5.3 ArrayBuffer.prototype.slice ( start, end )`
@@ -186,34 +178,26 @@ impl ArrayBuffer {
186178
fn slice(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
187179
// 1. Let O be the this value.
188180
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
189-
let obj = if let Some(obj) = this.as_object() {
190-
obj
191-
} else {
192-
return Err(JsNativeError::typ()
193-
.with_message("ArrayBuffer.slice called with non-object value")
194-
.into());
195-
};
181+
let obj = this.as_object().ok_or_else(|| {
182+
JsNativeError::typ().with_message("ArrayBuffer.slice called with non-object value")
183+
})?;
196184
let obj_borrow = obj.borrow();
197-
let o = if let Some(o) = obj_borrow.as_array_buffer() {
198-
o
199-
} else {
200-
return Err(JsNativeError::typ()
201-
.with_message("ArrayBuffer.slice called with invalid object")
202-
.into());
203-
};
185+
let buf = obj_borrow.as_array_buffer().ok_or_else(|| {
186+
JsNativeError::typ().with_message("ArrayBuffer.slice called with invalid object")
187+
})?;
204188

205189
// TODO: Shared Array Buffer
206190
// 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
207191

208192
// 4. If IsDetachedBuffer(O) is true, throw a TypeError exception.
209-
if Self::is_detached_buffer(o) {
193+
if Self::is_detached_buffer(buf) {
210194
return Err(JsNativeError::typ()
211195
.with_message("ArrayBuffer.slice called with detached buffer")
212196
.into());
213197
}
214198

215199
// 5. Let len be O.[[ArrayBufferByteLength]].
216-
let len = o.array_buffer_byte_length as i64;
200+
let len = buf.array_buffer_byte_length as i64;
217201

218202
// 6. Let relativeStart be ? ToIntegerOrInfinity(start).
219203
let relative_start = args.get_or_undefined(0).to_integer_or_infinity(context)?;
@@ -298,14 +282,14 @@ impl ArrayBuffer {
298282

299283
// 22. NOTE: Side-effects of the above steps may have detached O.
300284
// 23. If IsDetachedBuffer(O) is true, throw a TypeError exception.
301-
if Self::is_detached_buffer(o) {
285+
if Self::is_detached_buffer(buf) {
302286
return Err(JsNativeError::typ()
303287
.with_message("ArrayBuffer detached while ArrayBuffer.slice was running")
304288
.into());
305289
}
306290

307291
// 24. Let fromBuf be O.[[ArrayBufferData]].
308-
let from_buf = o
292+
let from_buf = buf
309293
.array_buffer_data
310294
.as_ref()
311295
.expect("ArrayBuffer cannot be detached here");
@@ -389,13 +373,9 @@ impl ArrayBuffer {
389373

390374
// 2. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
391375
// 3. Let srcBlock be srcBuffer.[[ArrayBufferData]].
392-
let src_block = if let Some(b) = &self.array_buffer_data {
393-
b
394-
} else {
395-
return Err(JsNativeError::syntax()
396-
.with_message("Cannot clone detached array buffer")
397-
.into());
398-
};
376+
let src_block = self.array_buffer_data.as_deref().ok_or_else(|| {
377+
JsNativeError::syntax().with_message("Cannot clone detached array buffer")
378+
})?;
399379

400380
{
401381
// 4. Let targetBlock be targetBuffer.[[ArrayBufferData]].

0 commit comments

Comments
 (0)