Skip to content

Mute clippy transmute warnings #320

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Sh3Rm4n
Copy link

@Sh3Rm4n Sh3Rm4n commented Apr 22, 2025

  • clippy::transmute_ptr_to_ref for QGadget
  • clippy::useless_transmute for QObject

Closes #291

* `clippy::transmute_ptr_to_ref` for QGadget
* `clippy::useless_transmute` for QObject
Copy link
Member

@ogoffart ogoffart left a comment

Choose a reason for hiding this comment

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

thanks for the PR.

But i'm not sure if this is correct, see inline comments.

quote! {
{
let mut n = #n;
(&mut n as *mut #ty as *mut ::std::os::raw::c_void)
Copy link
Member

Choose a reason for hiding this comment

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

that doesn't look right. It seems it's taking the address of n on the stack.

Copy link
Author

@Sh3Rm4n Sh3Rm4n Apr 22, 2025

Choose a reason for hiding this comment

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

I'm not sure either. This could be catched by the compiler? The code I've tested did not generate any errors. The let binding was necessary to get a mutable reference &mut. The compiler suggests to annotate the function argument with mut. I'll check if that is possible.

Copy link
Member

Choose a reason for hiding this comment

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

In this case it might not be catched by the compiler since the user of the pointer uses unsafe code.

Copy link
Author

Choose a reason for hiding this comment

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

Good point. I now use marked the function argument as mut so that a &mut reference can be taken.

@@ -686,7 +686,7 @@ pub fn generate(input: TokenStream, is_qobject: bool, qt_version: QtVersion) ->
} else {
quote! {
#[allow(unused_variables)]
let mut obj = ::std::mem::transmute::<*mut ::std::os::raw::c_void, &mut #name #ty_generics>(o);
let mut obj = &*o;
Copy link
Member

Choose a reason for hiding this comment

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

i would think you still need to cast to the right type explicitly.

Copy link
Member

Choose a reason for hiding this comment

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

I think that's why the CI is failling.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah this solution looks wrong. I kept the transmute to cast to the right type as *mut, but dereferenced it now to &mut outside of the transmute.

@Sh3Rm4n
Copy link
Author

Sh3Rm4n commented Apr 22, 2025

Overall these changes are all inside of unsafe blocks, and I'm not to confidident, if I got the semantics right without invoking UB. The alternative would be to just mute the clippy warnings with [allow(...). WDYT @ogoffart?

@Sh3Rm4n Sh3Rm4n force-pushed the fix/clippy-warnings-upstream branch from b6d98e5 to fdf587c Compare April 22, 2025 13:50
@Sh3Rm4n
Copy link
Author

Sh3Rm4n commented Apr 23, 2025

Alright, I'm not sure what caused this failure https://github.com/woboq/qmetaobject-rs/actions/runs/14596502246/job/40950448403, I could reproduce it locally, but also after reverting my changes (if I haven't done anything incorrectly).

As my goal is to mute the clippy warnigs, let's see if [allow(...)] makes the test run again.

@ogoffart
Copy link
Member

I think this could be done without transmute, only with pointer cast

@Sh3Rm4n
Copy link
Author

Sh3Rm4n commented Apr 24, 2025

Alright, with the last CI run https://github.com/woboq/qmetaobject-rs/actions/runs/14612927424/job/41009015057 and the current change master...Sh3Rm4n:qmetaobject-rs:4622085296b20e2fb27bf081d64c9216a1502c04 (which does no code changes) I think the CI failure is not related to this PR

@ogoffart
Copy link
Member

Indeed, the CI seems broken in master :-(

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.

QObject and QGadget derives produce clippy warnings
2 participants