Skip to content

borrow_as_ptr and ptr_as_ptr false positives in windows::core::implement macro invocation #15398

@PaulDance

Description

@PaulDance

Summary

These lints trigger on the invocation of the external macro. As suggested in microsoft/windows-rs#3690 (comment), I'm also opening an issue here just in case. I haven't investigated any of windows-rs's or Clippy's side of this yet, so there still remains to see where the responsibility should lie.

Lint Name

borrow_as_ptr and ptr_as_ptr

Reproducer

Using the following crate files:

# `Cargo.toml`
[package]
name = "tmp"
version = "0.1.0"
edition = "2024"

[dependencies]
windows = { version = "0.61.3", features = [
    "std",
    "Win32",
    "Win32_System",
    "Win32_System_Com",
]}
windows-core = "0.61.2"

[lints]
clippy.borrow_as_ptr = "warn"
clippy.ptr_as_ptr = "warn"

and

// `src/lib.rs`
use std::ffi::c_void;
use windows::Win32::System::Com::{IClassFactory, IClassFactory_Impl};
use windows::core::{BOOL, GUID, IUnknown, Ref, Result, implement};

#[implement(IClassFactory)]
struct A;

impl IClassFactory_Impl for A_Impl {
    fn CreateInstance(
        &self,
        _outer: Ref<'_, IUnknown>,
        _iid: *const GUID,
        _object: *mut *mut c_void,
    ) -> Result<()> {
        Ok(())
    }

    fn LockServer(&self, _lock: BOOL) -> Result<()> {
        Ok(())
    }
}

then

$ cargo clippy --target x86_64-pc-windows-msvc
warning: `as` casting between raw pointers without changing their constness
 --> src/lib.rs:5:13
  |
5 | #[implement(IClassFactory)]
  |             ^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `(IClassFactory as IClassFactory).cast::<IClassFactory>()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr
  = note: requested on the command line with `-W clippy::ptr-as-ptr`

warning: borrow as raw pointer
 --> src/lib.rs:5:13
  |
5 | #[implement(IClassFactory)]
  |             ^^^^^^^^^^^^^ help: try: `&raw const IClassFactory`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_as_ptr
  = note: requested on the command line with `-W clippy::borrow-as-ptr`

warning: `as` casting between raw pointers without changing their constness
 --> src/lib.rs:5:13
  |
5 | #[implement(IClassFactory)]
  |             ^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `IClassFactory.cast::<IClassFactory>()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr

warning: `tmp` (lib) generated 3 warnings (run `cargo clippy --fix --lib -p tmp` to apply 3 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s

The lints are triggered inside the code generated by the implement macro, while they shouldn't.

Version

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 20.1.5

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions