Skip to content

Commit

Permalink
Use safe NonZero constructor instead of an explicit null check
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Jan 22, 2018
1 parent 10ec5a2 commit b78ac6b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 10 deletions.
3 changes: 1 addition & 2 deletions components/script/dom/bindings/iterable.rs
Expand Up @@ -105,8 +105,7 @@ impl<T: DomObject + JSTraceable + Iterable> IterableIterator<T> {
};
self.index.set(index + 1);
result.map(|_| {
assert!(!rval.is_null());
unsafe { NonNull::new_unchecked(rval.get()) }
NonNull::new(rval.get()).expect("got a null pointer")
})
}
}
Expand Down
3 changes: 1 addition & 2 deletions components/script/dom/imagedata.rs
Expand Up @@ -160,7 +160,6 @@ impl ImageDataMethods for ImageData {
#[allow(unsafe_code)]
// https://html.spec.whatwg.org/multipage/#dom-imagedata-data
unsafe fn Data(&self, _: *mut JSContext) -> NonNull<JSObject> {
assert!(!self.data.get().is_null());
NonNull::new_unchecked(self.data.get())
NonNull::new(self.data.get()).expect("got a null pointer")
}
}
6 changes: 2 additions & 4 deletions components/script/dom/testbinding.rs
Expand Up @@ -154,8 +154,7 @@ impl TestBindingMethods for TestBinding {
#[allow(unsafe_code)]
unsafe fn ArrayAttribute(&self, cx: *mut JSContext) -> NonNull<JSObject> {
rooted!(in(cx) let array = JS_NewUint8ClampedArray(cx, 16));
assert!(!array.is_null());
NonNull::new_unchecked(array.get())
NonNull::new(array.get()).expect("got a null pointer")
}
#[allow(unsafe_code)]
unsafe fn AnyAttribute(&self, _: *mut JSContext) -> JSVal { NullValue() }
Expand All @@ -164,8 +163,7 @@ impl TestBindingMethods for TestBinding {
#[allow(unsafe_code)]
unsafe fn ObjectAttribute(&self, cx: *mut JSContext) -> NonNull<JSObject> {
rooted!(in(cx) let obj = JS_NewPlainObject(cx));
assert!(!obj.is_null());
NonNull::new_unchecked(obj.get())
NonNull::new(obj.get()).expect("got a null pointer")
}
#[allow(unsafe_code)]
unsafe fn SetObjectAttribute(&self, _: *mut JSContext, _: *mut JSObject) {}
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/xmlhttprequest.rs
Expand Up @@ -1123,8 +1123,8 @@ impl XMLHttpRequest {
unsafe fn arraybuffer_response(&self, cx: *mut JSContext) -> Option<NonNull<JSObject>> {
// Step 1
let created = self.response_arraybuffer.get();
if !created.is_null() {
return Some(NonNull::new_unchecked(created));
if let Some(nonnull) = NonNull::new(created) {
return Some(nonnull)
}

// Step 2
Expand Down

0 comments on commit b78ac6b

Please sign in to comment.