Repository: dtolnay/cxx (language: rust)

Issue:
Improvement of strange lint warnings
I have `#![warn(clippy::pedantic)]` in my crate's `lib.rs`. In CXX bridges, this can lead to warnings about things that are not my business. If you could please use `#[allow(clippy::pedantic)]` in the relevant, not all-encompassing locations, this would solve these warnings. If you aren't sure where exactly, here's concrete warnings I get:

- Without `#![allow(clippy::ptr_as_ptr)]` in my bridge, I get `` `as` casting between raw pointers without changing its mutability `` on non-`self` parameters in the `extern "C++"` block that have the pattern `&T` or `Pin<&mut T>`, where `T` is a C++ bridge type. See https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr.
- Without `#![allow(clippy::expl_impl_clone_on_copy)]` in my bridge, I get `` you are implementing `Clone` explicitly on a `Copy` type `` in `enum`s that work together with an `extern "C++"` type of the same name (in [that](https://cxx.rs/shared.html#extern-enums) sense). See https://rust-lang.github.io/rust-clippy/master/index.html#expl_impl_clone_on_copy.

---

Not related to `#![warn(clippy::pedantic)]` is the following kind of warning: Without `#![allow(dead_code)]` in my bridge, I get `` associated constant `FOO_BAR` is never used `` on `enum`s. Is there a better `allow`able warning possible, something with "unused"? The worst is, I can't use `#[allow(dead_code)]` on the whole enum, but only on every single currently unused variant, which are many in that large `enum`. (Hence, I'm temporarily using it on the whole bridge, which works again.)

---

Related: #1061


Discussion / hints:
The `clippy::use_self` lint also seems to be emitted when defining a shared `enum` type that has an existing c++ enum implementation:

Repro:

Rust side
```rust
#[cxx::bridge]
mod ffi {
    #[derive(Debug)]
    enum SomeErrorType {
        EMPTY,
    }

    unsafe extern "C++" {
        include!("mylibrary.h");
        type SomeErrorType;
    }
}
```

Cpp side
```cpp
enum class SomeErrorType : unsigned char {
  EMPTY,
};
```

Fix patch:
diff --git a/macro/src/derive.rs b/macro/src/derive.rs
index afe85d1c2..e1d8d69e7 100644
--- a/macro/src/derive.rs
+++ b/macro/src/derive.rs
@@ -123,6 +123,7 @@ fn struct_clone(strct: &Struct, span: Span) -> TokenStream {
     };
 
     quote_spanned! {span=>
+        #[allow(clippy::expl_impl_clone_on_copy)]
         impl #generics ::cxx::core::clone::Clone for #ident #generics {
             fn clone(&self) -> Self {
                 #body
@@ -230,6 +231,7 @@ fn enum_clone(enm: &Enum, span: Span) -> TokenStream {
     let ident = &enm.name.rust;
 
     quote_spanned! {span=>
+        #[allow(clippy::expl_impl_clone_on_copy)]
         impl ::cxx::core::clone::Clone for #ident {
             fn clone(&self) -> Self {
                 *self
diff --git a/macro/src/expand.rs b/macro/src/expand.rs
index 573b0486b..ba36cb4c7 100644
--- a/macro/src/expand.rs
+++ b/macro/src/expand.rs
@@ -139,7 +139,13 @@ fn expand(ffi: Module, doc: Doc, attrs: OtherAttrs, apis: &[Api], types: &Types)
         #attrs
         #[deny(improper_ctypes, improper_ctypes_definitions)]
         #[allow(clippy::unknown_clippy_lints)]
-        #[allow(non_camel_case_types, non_snake_case, clippy::upper_case_acronyms)]
+        #[allow(
+            non_camel_case_types,
+            non_snake_case,
+            clippy::ptr_as_ptr,
+            clippy::upper_case_acronyms,
+            clippy::use_self,
+        )]
         #vis #mod_token #ident #expanded
     }
 }


Test patch: