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 several warnings in components/script/dom/bindings #31945

Merged
merged 4 commits into from
Apr 3, 2024

Conversation

azharcodeit
Copy link
Contributor

@azharcodeit azharcodeit commented Mar 29, 2024

Fixed warnings triggered by following checks:


  • These changes do not require tests because they do not modify functionality

@@ -521,8 +522,7 @@ impl<T> Clone for Dom<T> {
impl<T> Clone for LayoutDom<'_, T> {
#[inline]
fn clone(&self) -> Self {
assert_in_layout();
LayoutDom { value: self.value }
*self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change was talked about in #31894 (comment) but it wasn't applied in the end. In that discussion, @mrobinson proposed doing this instead to avoid removing the assert:

Suggested change
*self
assert_in_layout();
{ *self }

Copy link
Contributor Author

@azharcodeit azharcodeit Mar 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @eerii, very helpful!

I double checked for this particular warning, it still persisted after proposed change.

Since assert_in_layout() needed to be kept, I decided to allow it.

Considering those changes and formatting, I decided to create a new commit rather than committing your suggestion, if you don't mind :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, sorry for not realizing before! The error still persist because Clone has the assert but Copy doesn't, see #31894 (comment). Maybe the Copy trait should be removed like it says in the comment? Probably a good idea to wait to hear from a maintainer to see what to do, since this is a bit trickier than it seemed.

And no problem at all about the commit!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does sound like the appropriate thing to do here might be to remove the Copy trait implementation...but only if that's possible. Does Servo still compile with that removed?

Copy link
Contributor Author

@azharcodeit azharcodeit Mar 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrobinson removing the Copy trait implementation (impl<T> Copy for LayoutDom<'_, T> {}) caused following compile errors:

error[E0204]: the trait `std::marker::Copy` cannot be implemented for this type
  --> components/script/layout_dom/document.rs:34:54
   |
23 |     document: LayoutDom<'dom, Document>,
   |     ----------------------------------- this field does not implement `std::marker::Copy`
...
34 | impl<'dom, LayoutDataType: LayoutDataTrait> Copy for ServoLayoutDocument<'dom, LayoutDataType> {}
   |                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0204]: the trait `std::marker::Copy` cannot be implemented for this type
  --> components/script/layout_dom/element.rs:68:54
   |
55 |     element: LayoutDom<'dom, Element>,
   |     --------------------------------- this field does not implement `std::marker::Copy`
...
68 | impl<'dom, LayoutDataType: LayoutDataTrait> Copy for ServoLayoutElement<'dom, LayoutDataType> {}
   |                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0204]: the trait `std::marker::Copy` cannot be implemented for this type
  --> components/script/layout_dom/node.rs:73:54
   |
51 |     pub(super) node: LayoutDom<'dom, Node>,
   |     -------------------------------------- this field does not implement `std::marker::Copy`
...
73 | impl<'dom, LayoutDataType: LayoutDataTrait> Copy for ServoLayoutNode<'dom, LayoutDataType> {}
   |                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0204]: the trait `std::marker::Copy` cannot be implemented for this type
  --> components/script/layout_dom/shadow_root.rs:30:54
   |
19 |     shadow_root: LayoutDom<'dom, ShadowRoot>,
   |     ---------------------------------------- this field does not implement `std::marker::Copy`
...
30 | impl<'dom, LayoutDataType: LayoutDataTrait> Copy for ServoShadowRoot<'dom, LayoutDataType> {}
   |                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0204`.
error: could not compile `script` (lib) due to 4 previous errors

@azharcodeit azharcodeit force-pushed the clippy/dom-bindings branch 2 times, most recently from a10dcda to 5668655 Compare March 30, 2024 15:23
components/script/dom/bindings/root.rs Outdated Show resolved Hide resolved
@@ -418,7 +418,7 @@ where
/// without causing conflicts , unexpected behavior.
/// <https://github.com/servo/mozjs/blob/main/mozjs-sys/mozjs/js/public/ArrayBuffer.h#L89>
unsafe extern "C" fn free_func(_contents: *mut c_void, free_user_data: *mut c_void) {
let _ = Arc::from_raw(free_user_data as _);
let _ = Arc::from_raw(free_user_data as *const _);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the warning, why is *const _ better than _?

Copy link
Contributor Author

@azharcodeit azharcodeit Mar 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Loirooriol, I used *const, because from_raw function receives *const T type as an argument

pub unsafe fn from_raw(ptr: *const T) -> Self {
        unsafe { Arc::from_raw_in(ptr, Global) }
    }

the warning was:

warning: creating a `Arc` from a void raw pointer
   --> components/script/dom/bindings/buffer_source.rs:421:17
    |
421 |         let _ = Arc::from_raw(free_user_data as _);
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: cast this to a pointer of the appropriate type
   --> components/script/dom/bindings/buffer_source.rs:421:31
    |
421 |         let _ = Arc::from_raw(free_user_data as _);
    |                               ^^^^^^^^^^^^^^^^^^^
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_raw_with_void_ptr
    = note: `#[warn(clippy::from_raw_with_void_ptr)]` on by default

@Loirooriol Loirooriol added this pull request to the merge queue Apr 3, 2024
Merged via the queue into servo:main with commit 37cf4cf Apr 3, 2024
10 checks passed
@azharcodeit azharcodeit deleted the clippy/dom-bindings branch April 6, 2024 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants