Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clippy: Fix unnecessary_cast warnings in components/script #31823

Merged
merged 5 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/script/canvas_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ impl CanvasState {
) -> (Rect<f64>, Rect<f64>) {
let image_rect = Rect::new(
Point2D::new(0f64, 0f64),
Size2D::new(image_size.width as f64, image_size.height as f64),
Size2D::new(image_size.width, image_size.height),
);

// The source rectangle is the rectangle whose corners are the four points (sx, sy),
Expand Down
5 changes: 2 additions & 3 deletions components/script/dom/bindings/buffer_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,13 @@ where
}

unsafe {
let mapping_slice_ptr =
mapping.lock().unwrap().borrow_mut()[offset as usize..m_end as usize].as_mut_ptr();
let mapping_slice_ptr = mapping.lock().unwrap().borrow_mut()[offset..m_end].as_mut_ptr();

// rooted! is needed to ensure memory safety and prevent potential garbage collection issues.
// https://github.com/mozilla-spidermonkey/spidermonkey-embedding-examples/blob/esr78/docs/GC%20Rooting%20Guide.md#performance-tweaking
rooted!(in(*cx) let array_buffer = NewExternalArrayBuffer(
*cx,
range_size as usize,
range_size,
mapping_slice_ptr as _,
Some(free_func),
Arc::into_raw(mapping) as _,
Expand Down
10 changes: 5 additions & 5 deletions components/script/dom/bindings/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ pub unsafe fn jsstring_to_str(cx: *mut JSContext, s: *mut JSString) -> DOMString
let mut length = 0;
let chars = JS_GetTwoByteStringCharsAndLength(cx, ptr::null(), s, &mut length);
assert!(!chars.is_null());
let potentially_ill_formed_utf16 = slice::from_raw_parts(chars, length as usize);
let mut s = String::with_capacity(length as usize);
let potentially_ill_formed_utf16 = slice::from_raw_parts(chars, length);
let mut s = String::with_capacity(length);
for item in char::decode_utf16(potentially_ill_formed_utf16.iter().cloned()) {
match item {
Ok(c) => s.push(c),
Expand Down Expand Up @@ -282,7 +282,7 @@ impl FromJSValConvertible for USVString {
let mut length = 0;
let chars = JS_GetTwoByteStringCharsAndLength(cx, ptr::null(), jsstr, &mut length);
assert!(!chars.is_null());
let char_vec = slice::from_raw_parts(chars as *const u16, length as usize);
let char_vec = slice::from_raw_parts(chars as *const u16, length);
Ok(ConversionResult::Success(USVString(
String::from_utf16_lossy(char_vec),
)))
Expand Down Expand Up @@ -324,15 +324,15 @@ impl FromJSValConvertible for ByteString {
let chars = JS_GetLatin1StringCharsAndLength(cx, ptr::null(), string, &mut length);
assert!(!chars.is_null());

let char_slice = slice::from_raw_parts(chars as *mut u8, length as usize);
let char_slice = slice::from_raw_parts(chars as *mut u8, length);
return Ok(ConversionResult::Success(ByteString::new(
char_slice.to_vec(),
)));
}

let mut length = 0;
let chars = JS_GetTwoByteStringCharsAndLength(cx, ptr::null(), string, &mut length);
let char_vec = slice::from_raw_parts(chars, length as usize);
let char_vec = slice::from_raw_parts(chars, length);

if char_vec.iter().any(|&c| c > 0xFF) {
throw_type_error(cx, "Invalid ByteString");
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/bindings/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ fn max_day_in_month(year_num: i32, month_num: u32) -> Option<u32> {

/// <https://html.spec.whatwg.org/multipage/#week-number-of-the-last-day>
fn max_week_in_year(year: i32) -> u32 {
Utc.with_ymd_and_hms(year as i32, 1, 1, 0, 0, 0)
Utc.with_ymd_and_hms(year, 1, 1, 0, 0, 0)
.earliest()
.map(|date_time| match date_time.weekday() {
Weekday::Thu => 53,
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/bindings/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ pub unsafe extern "C" fn resolve_global(
let mut length = 0;
let ptr = JS_GetLatin1StringCharsAndLength(cx, ptr::null(), string, &mut length);
assert!(!ptr.is_null());
let bytes = slice::from_raw_parts(ptr, length as usize);
let bytes = slice::from_raw_parts(ptr, length);

if let Some(init_fun) = InterfaceObjectMap::MAP.get(bytes) {
init_fun(SafeJSContext::from_ptr(cx), Handle::from_raw(obj));
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/canvasgradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl CanvasGradientMethods for CanvasGradient {
};

self.stops.borrow_mut().push(CanvasGradientStop {
offset: (*offset) as f64,
offset: (*offset),
color,
});
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions components/script/dom/globalscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ impl GlobalScope {

let stream = ReadableStream::new_with_external_underlying_source(
self,
ExternalUnderlyingSource::Blob(size as usize),
ExternalUnderlyingSource::Blob(size),
);

let recv = self.send_msg(file_id);
Expand Down Expand Up @@ -3171,7 +3171,7 @@ impl GlobalScope {
}
}
for i in (0..gamepad_list.Length()).rev() {
if gamepad_list.Item(i as u32).is_none() {
if gamepad_list.Item(i).is_none() {
gamepad_list.remove_gamepad(i as usize);
} else {
break;
Expand Down Expand Up @@ -3211,7 +3211,7 @@ impl GlobalScope {
if !window.Navigator().has_gamepad_gesture() && contains_user_gesture(update_type) {
window.Navigator().set_has_gamepad_gesture(true);
for i in 0..gamepad_list.Length() {
if let Some(gamepad) = gamepad_list.Item(i as u32) {
if let Some(gamepad) = gamepad_list.Item(i) {
gamepad.set_exposed(true);
gamepad.update_timestamp(*current_time);
let new_gamepad = Trusted::new(&*gamepad);
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/htmlareaelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ impl Area {
.iter()
.enumerate()
.map(|(index, point)| match index % 2 {
0 => point + p.x as f32,
_ => point + p.y as f32,
0 => point + p.x,
_ => point + p.y,
});
Area::Polygon {
points: iter.collect::<Vec<_>>(),
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/htmlformelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl HTMLFormElementMethods for HTMLFormElement {

// https://html.spec.whatwg.org/multipage/#dom-form-length
fn Length(&self) -> u32 {
self.Elements().Length() as u32
self.Elements().Length()
}

// https://html.spec.whatwg.org/multipage/#dom-form-item
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/htmlinputelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2155,7 +2155,7 @@ impl HTMLInputElement {
.map(|time| time.and_utc().timestamp_millis() as f64),
InputType::Time => match value.parse_time_string() {
Some((hours, minutes, seconds)) => {
Some((seconds as f64 + 60.0 * minutes as f64 + 3600.0 * hours as f64) * 1000.0)
Some((seconds + 60.0 * minutes as f64 + 3600.0 * hours as f64) * 1000.0)
},
_ => None,
},
Expand Down Expand Up @@ -2543,7 +2543,7 @@ impl VirtualMethods for HTMLInputElement {
let TextIndexResponse(index) =
window.text_index_query(self.upcast::<Node>(), point_in_target);
if let Some(i) = index {
self.textinput.borrow_mut().set_edit_point_index(i as usize);
self.textinput.borrow_mut().set_edit_point_index(i);
// trigger redraw
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
event.PreventDefault();
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/htmlmediaelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2334,7 +2334,7 @@ impl HTMLMediaElementMethods for HTMLMediaElement {
if let Some(ref player) = *self.player.borrow() {
if let Ok(ranges) = player.lock().unwrap().buffered() {
for range in ranges {
let _ = buffered.add(range.start as f64, range.end as f64);
let _ = buffered.add(range.start, range.end);
}
}
}
Expand Down Expand Up @@ -2504,7 +2504,7 @@ impl MicrotaskRunnable for MediaElementMicrotask {
elem.resource_selection_algorithm_sync(base_url.clone());
}
},
&MediaElementMicrotask::PauseIfNotInDocumentTask { ref elem } => {
MediaElementMicrotask::PauseIfNotInDocumentTask { elem } => {
if !elem.upcast::<Node>().is_connected() {
elem.internal_pause_steps();
}
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2803,7 +2803,7 @@ impl NodeMethods for Node {
.ranges
.drain_to_preceding_text_sibling(&sibling, &node, length);
self.ranges
.move_to_text_child_at(self, index as u32, &node, length as u32);
.move_to_text_child_at(self, index as u32, &node, length);
let sibling_cdata = sibling.downcast::<CharacterData>().unwrap();
length += sibling_cdata.Length();
cdata.append_data(&sibling_cdata.data());
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/nodelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl ChildrenList {
pub fn item(&self, index: u32) -> Option<DomRoot<Node>> {
// This always start traversing the children from the closest element
// among parent's first and last children and the last visited one.
let len = self.len() as u32;
let len = self.len();
if index >= len {
return None;
}
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/paintworkletglobalscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ impl PaintWorkletGlobalScope {
) -> DrawAPaintImageResult {
debug!("Returning an invalid image.");
DrawAPaintImageResult {
width: size.width as u32,
height: size.height as u32,
width: size.width,
height: size.height,
format: PixelFormat::BGRA8,
image_key: None,
missing_image_urls,
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/readablestream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl ExternalUnderlyingSourceController {
fn get_chunk_with_length(&self, length: usize) -> Vec<u8> {
let mut buffer = self.buffer.borrow_mut();
let buffer_len = buffer.len();
assert!(buffer_len >= length as usize);
assert!(buffer_len >= length);
buffer.split_off(buffer_len - length)
}

Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/texttracklist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl TextTrackList {
pub fn item(&self, idx: usize) -> Option<DomRoot<TextTrack>> {
self.dom_tracks
.borrow()
.get(idx as usize)
.get(idx)
.map(|t| DomRoot::from_ref(&**t))
}

Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/webgl_validations/tex_image_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ impl<'a> WebGLValidator for TexStorageValidator<'a> {
return Err(TexImageValidationError::InvalidTextureFormat);
}

let max_level = log2(cmp::max(width, height) as u32) + 1;
let max_level = log2(cmp::max(width, height)) + 1;
if level > max_level {
context.webgl_error(InvalidOperation);
return Err(TexImageValidationError::LevelTooHigh);
Expand Down
10 changes: 1 addition & 9 deletions components/script/dom/webglrenderingcontext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2664,15 +2664,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
// NB: TexImage2D depth is always equal to 1
handle_potential_webgl_error!(
self,
texture.initialize(
target,
width as u32,
height as u32,
1,
internal_format,
level as u32,
None
)
texture.initialize(target, width, height, 1, internal_format, level, None)
);

let msg = WebGLCommand::CopyTexImage2D(
Expand Down
2 changes: 1 addition & 1 deletion components/script/layout_dom/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl<'dom, LayoutDataType: LayoutDataTrait> ServoThreadSafeLayoutNode<'dom, Layo
/// Creates a new `ServoThreadSafeLayoutNode` from the given `ServoLayoutNode`.
pub fn new(node: ServoLayoutNode<'dom, LayoutDataType>) -> Self {
ServoThreadSafeLayoutNode {
node: node,
node,
pseudo: PseudoElementType::Normal,
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/script/script_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ fn fetch_single_module_script(
data: vec![],
metadata: None,
url: url.clone(),
destination: destination,
destination,
options,
status: Ok(()),
resource_timing: ResourceFetchTiming::new(ResourceTimingType::Resource),
Expand Down
6 changes: 1 addition & 5 deletions components/script/script_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,7 @@ pub fn get_reports(cx: *mut RawJSContext, path_seg: String) -> Vec<Report> {
let mut report = |mut path_suffix, kind, size| {
let mut path = path![path_seg, "js"];
path.append(&mut path_suffix);
reports.push(Report {
path,
kind,
size: size as usize,
})
reports.push(Report { path, kind, size })
};

// A note about possibly confusing terminology: the JS GC "heap" is allocated via
Expand Down