Skip to content

Commit 7b64fa1

Browse files
Tptdavidhewitt
authored andcommitted
Makes Clippy beta happy (#5032)
* Use std::ptr::eq where relevant * allow(clippy::ptr_eq) in pyo3-ffi * Add ref for allow(clippy::large_enum_variant)
1 parent 1b01bac commit 7b64fa1

File tree

16 files changed

+82
-38
lines changed

16 files changed

+82
-38
lines changed

pyo3-ffi/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@
327327
non_snake_case,
328328
non_upper_case_globals,
329329
clippy::upper_case_acronyms,
330-
clippy::missing_safety_doc
330+
clippy::missing_safety_doc,
331+
clippy::ptr_eq
331332
)]
332333
#![warn(elided_lifetimes_in_paths, unused_lifetimes)]
333334
// This crate is a hand-maintained translation of CPython's headers, so requiring "unsafe"

pyo3-macros-backend/src/method.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub struct PyArg<'a> {
5353
pub ty: &'a syn::Type,
5454
}
5555

56+
#[allow(clippy::large_enum_variant)] // See #5039
5657
#[derive(Clone, Debug)]
5758
pub enum FnArg<'a> {
5859
Regular(RegularArg<'a>),

src/err/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod impls;
2222
use crate::conversion::IntoPyObject;
2323
use err_state::{PyErrState, PyErrStateLazyFnOutput, PyErrStateNormalized};
2424
use std::convert::Infallible;
25+
use std::ptr;
2526

2627
/// Represents a Python exception.
2728
///
@@ -364,7 +365,10 @@ impl PyErr {
364365
pub fn take(py: Python<'_>) -> Option<PyErr> {
365366
let state = PyErrStateNormalized::take(py)?;
366367
let pvalue = state.pvalue.bind(py);
367-
if pvalue.get_type().as_ptr() == PanicException::type_object_raw(py).cast() {
368+
if ptr::eq(
369+
pvalue.get_type().as_ptr(),
370+
PanicException::type_object_raw(py).cast(),
371+
) {
368372
let msg: String = pvalue
369373
.str()
370374
.map(|py_str| py_str.to_string_lossy().into())

src/impl_/pyclass.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::{
1919
ffi::{CStr, CString},
2020
marker::PhantomData,
2121
os::raw::{c_int, c_void},
22+
ptr,
2223
ptr::NonNull,
2324
sync::Mutex,
2425
thread,
@@ -950,7 +951,7 @@ pub unsafe extern "C" fn alloc_with_freelist<T: PyClassWithFreeList>(
950951
let self_type = T::type_object_raw(py);
951952
// If this type is a variable type or the subtype is not equal to this type, we cannot use the
952953
// freelist
953-
if nitems == 0 && subtype == self_type {
954+
if nitems == 0 && ptr::eq(subtype, self_type) {
954955
let mut free_list = T::get_free_list(py).lock().unwrap();
955956
if let Some(obj) = free_list.pop() {
956957
drop(free_list);
@@ -1143,7 +1144,6 @@ impl<T> PyClassThreadChecker<T> for ThreadCheckerImpl {
11431144
}
11441145

11451146
/// Trait denoting that this class is suitable to be used as a base type for PyClass.
1146-
11471147
#[cfg_attr(
11481148
all(diagnostic_namespace, Py_LIMITED_API),
11491149
diagnostic::on_unimplemented(

src/impl_/pyclass_init.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::types::PyType;
55
use crate::{ffi, Borrowed, PyErr, PyResult, Python};
66
use crate::{ffi::PyTypeObject, sealed::Sealed, type_object::PyTypeInfo};
77
use std::marker::PhantomData;
8+
use std::ptr;
89

910
/// Initializer for Python types.
1011
///
@@ -38,7 +39,7 @@ impl<T: PyTypeInfo> PyObjectInit<T> for PyNativeTypeInitializer<T> {
3839
subtype: *mut PyTypeObject,
3940
) -> PyResult<*mut ffi::PyObject> {
4041
// HACK (due to FIXME below): PyBaseObject_Type's tp_new isn't happy with NULL arguments
41-
let is_base_object = type_object == std::ptr::addr_of_mut!(ffi::PyBaseObject_Type);
42+
let is_base_object = ptr::eq(type_object, ptr::addr_of!(ffi::PyBaseObject_Type));
4243
let subtype_borrowed: Borrowed<'_, '_, PyType> = unsafe {
4344
subtype
4445
.cast::<ffi::PyObject>()

src/instance.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{IntoPy, ToPyObject};
1616
use std::marker::PhantomData;
1717
use std::mem::ManuallyDrop;
1818
use std::ops::Deref;
19+
use std::ptr;
1920
use std::ptr::NonNull;
2021

2122
/// Owned or borrowed gil-bound Python smart pointer
@@ -1335,7 +1336,7 @@ impl<T> Py<T> {
13351336
/// This is equivalent to the Python expression `self is other`.
13361337
#[inline]
13371338
pub fn is<U: AsPyPointer>(&self, o: &U) -> bool {
1338-
self.as_ptr() == o.as_ptr()
1339+
ptr::eq(self.as_ptr(), o.as_ptr())
13391340
}
13401341

13411342
/// Gets the reference count of the `ffi::PyObject` pointer.
@@ -1407,7 +1408,7 @@ impl<T> Py<T> {
14071408
///
14081409
/// This is equivalent to the Python expression `self is None`.
14091410
pub fn is_none(&self, _py: Python<'_>) -> bool {
1410-
unsafe { ffi::Py_None() == self.as_ptr() }
1411+
unsafe { ptr::eq(ffi::Py_None(), self.as_ptr()) }
14111412
}
14121413

14131414
/// Returns whether the object is considered to be true.

src/internal_tricks.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub(crate) fn ptr_from_mut<T>(t: &mut T) -> *mut T {
3434
// TODO: use ptr::fn_addr_eq on MSRV 1.85
3535
pub(crate) fn clear_eq(f: Option<ffi::inquiry>, g: ffi::inquiry) -> bool {
3636
#[cfg(fn_ptr_eq)]
37+
#[allow(clippy::incompatible_msrv)]
3738
{
3839
let Some(f) = f else { return false };
3940
std::ptr::fn_addr_eq(f, g)
@@ -48,6 +49,7 @@ pub(crate) fn clear_eq(f: Option<ffi::inquiry>, g: ffi::inquiry) -> bool {
4849
// TODO: use ptr::fn_addr_eq on MSRV 1.85
4950
pub(crate) fn traverse_eq(f: Option<ffi::traverseproc>, g: ffi::traverseproc) -> bool {
5051
#[cfg(fn_ptr_eq)]
52+
#[allow(clippy::incompatible_msrv)]
5153
{
5254
let Some(f) = f else { return false };
5355
std::ptr::fn_addr_eq(f, g)

src/pycell/impl_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ where
241241
let actual_type = PyType::from_borrowed_type_ptr(py, ffi::Py_TYPE(slf));
242242

243243
// For `#[pyclass]` types which inherit from PyAny, we can just call tp_free
244-
if type_ptr == std::ptr::addr_of_mut!(ffi::PyBaseObject_Type) {
244+
if std::ptr::eq(type_ptr, std::ptr::addr_of!(ffi::PyBaseObject_Type)) {
245245
let tp_free = actual_type
246246
.get_slot(TP_FREE)
247247
.expect("PyBaseObject_Type should have tp_free");

src/type_object.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::ffi_ptr_ext::FfiPtrExt;
44
use crate::types::any::PyAnyMethods;
55
use crate::types::{PyAny, PyType};
66
use crate::{ffi, Bound, Python};
7+
use std::ptr;
78

89
/// `T: PyLayout<U>` represents that `T` is a concrete representation of `U` in the Python heap.
910
/// E.g., `PyClassObject` is a concrete representation of all `pyclass`es, and `ffi::PyObject`
@@ -85,7 +86,12 @@ pub unsafe trait PyTypeInfo: Sized {
8586
/// Checks if `object` is an instance of this type.
8687
#[inline]
8788
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool {
88-
unsafe { ffi::Py_TYPE(object.as_ptr()) == Self::type_object_raw(object.py()) }
89+
unsafe {
90+
ptr::eq(
91+
ffi::Py_TYPE(object.as_ptr()),
92+
Self::type_object_raw(object.py()),
93+
)
94+
}
8995
}
9096

9197
/// Deprecated name for [`PyTypeInfo::is_exact_type_of`].

src/types/any.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{err, ffi, Borrowed, BoundObject, IntoPyObjectExt, Python};
1616
use std::cell::UnsafeCell;
1717
use std::cmp::Ordering;
1818
use std::os::raw::c_int;
19+
use std::ptr;
1920

2021
/// Represents any Python object.
2122
///
@@ -964,7 +965,7 @@ macro_rules! implement_binop {
964965
impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
965966
#[inline]
966967
fn is<T: AsPyPointer>(&self, other: &T) -> bool {
967-
self.as_ptr() == other.as_ptr()
968+
ptr::eq(self.as_ptr(), other.as_ptr())
968969
}
969970

970971
fn hasattr<N>(&self, attr_name: N) -> PyResult<bool>
@@ -1368,7 +1369,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
13681369

13691370
#[inline]
13701371
fn is_none(&self) -> bool {
1371-
unsafe { ffi::Py_None() == self.as_ptr() }
1372+
unsafe { ptr::eq(ffi::Py_None(), self.as_ptr()) }
13721373
}
13731374

13741375
fn is_ellipsis(&self) -> bool {

0 commit comments

Comments
 (0)