script: Wrap unsafe code in components/script/bindings in unsafe {} (#38544)

Clippy now checks to see if unsafe code is wrapped in unsafe blocks. We
have this lint disabled for `script` and `script_bindings` because of a
lot of legacy code that doesn't do this. The lint is useful though as it
makes it more obvious what code is unsafe. This is an incremental step
toward being able to turn this lint on for `script`.

This has the benefit of silencing warnings that show up in some IDEs
that use rust-analyzer.

Testing: This should not change behavior at all and is thus covered by
existing tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-08-08 19:18:30 +02:00 committed by GitHub
parent fef104cff7
commit 931025c16e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 147 additions and 114 deletions

View file

@ -84,16 +84,18 @@ pub(crate) unsafe fn get_property_jsval(
Err(_) => return Ok(()),
};
let mut found = false;
if JS_HasProperty(cx, object, cname.as_ptr(), &mut found) && found {
JS_GetProperty(cx, object, cname.as_ptr(), rval);
if JS_IsExceptionPending(cx) {
return Err(Error::JSFailed);
unsafe {
if JS_HasProperty(cx, object, cname.as_ptr(), &mut found) && found {
JS_GetProperty(cx, object, cname.as_ptr(), rval);
if JS_IsExceptionPending(cx) {
return Err(Error::JSFailed);
}
Ok(())
} else if JS_IsExceptionPending(cx) {
Err(Error::JSFailed)
} else {
Ok(())
}
Ok(())
} else if JS_IsExceptionPending(cx) {
Err(Error::JSFailed)
} else {
Ok(())
}
}
@ -109,13 +111,14 @@ where
{
debug!("Getting property {}.", name);
rooted!(in(cx) let mut result = UndefinedValue());
get_property_jsval(cx, object, name, result.handle_mut())?;
unsafe { get_property_jsval(cx, object, name, result.handle_mut())? };
if result.is_undefined() {
debug!("No property {}.", name);
return Ok(None);
}
debug!("Converting property {}.", name);
match T::from_jsval(cx, result.handle(), option) {
let value = unsafe { T::from_jsval(cx, result.handle(), option) };
match value {
Ok(ConversionResult::Success(value)) => Ok(Some(value)),
Ok(ConversionResult::Failure(_)) => Ok(None),
Err(()) => Err(Error::JSFailed),