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

+2-1
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

+1
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

+5-1
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

+2-2
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

+2-1
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

+3-2
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

+2
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

+1-1
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

+7-1
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

+3-2
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 {

src/types/boolobject.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use super::any::PyAnyMethods;
1111
use crate::conversion::IntoPyObject;
1212
use crate::BoundObject;
1313
use std::convert::Infallible;
14+
use std::ptr;
1415

1516
/// Represents a Python `bool`.
1617
///
@@ -61,7 +62,7 @@ pub trait PyBoolMethods<'py>: crate::sealed::Sealed {
6162
impl<'py> PyBoolMethods<'py> for Bound<'py, PyBool> {
6263
#[inline]
6364
fn is_true(&self) -> bool {
64-
self.as_ptr() == unsafe { crate::ffi::Py_True() }
65+
unsafe { ptr::eq(self.as_ptr(), ffi::Py_True()) }
6566
}
6667
}
6768

src/types/sequence.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ impl PyTypeCheck for PySequence {
397397
mod tests {
398398
use crate::types::{PyAnyMethods, PyList, PySequence, PySequenceMethods, PyTuple};
399399
use crate::{ffi, IntoPyObject, PyObject, Python};
400+
use std::ptr;
400401

401402
fn get_object() -> PyObject {
402403
// Convenience function for getting a single unique object
@@ -548,7 +549,7 @@ mod tests {
548549
let ob = v.into_pyobject(py).unwrap();
549550
let seq = ob.downcast::<PySequence>().unwrap();
550551
assert!(seq.set_item(1, &obj).is_ok());
551-
assert!(seq.get_item(1).unwrap().as_ptr() == obj.as_ptr());
552+
assert!(ptr::eq(seq.get_item(1).unwrap().as_ptr(), obj.as_ptr()));
552553
});
553554

554555
Python::with_gil(move |py| {

src/types/weakref/anyref.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ mod tests {
422422
use super::*;
423423
use crate::ffi;
424424
use crate::{py_result_ext::PyResultExt, types::PyType};
425+
use std::ptr;
425426

426427
fn get_type(py: Python<'_>) -> PyResult<Bound<'_, PyType>> {
427428
py.run(ffi::c_str!("class A:\n pass\n"), None, None)?;
@@ -450,8 +451,10 @@ mod tests {
450451
let obj = obj.unwrap();
451452

452453
assert!(obj.is_some());
453-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
454-
&& obj.is_exact_instance(&class)));
454+
assert!(
455+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
456+
&& obj.is_exact_instance(&class))
457+
);
455458
}
456459

457460
drop(object);
@@ -492,8 +495,10 @@ mod tests {
492495
let obj = unsafe { reference.upgrade_as_unchecked::<PyAny>() };
493496

494497
assert!(obj.is_some());
495-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
496-
&& obj.is_exact_instance(&class)));
498+
assert!(
499+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
500+
&& obj.is_exact_instance(&class))
501+
);
497502
}
498503

499504
drop(object);
@@ -586,6 +591,7 @@ mod tests {
586591
mod pyo3_pyclass {
587592
use super::*;
588593
use crate::{pyclass, Py};
594+
use std::ptr;
589595

590596
#[pyclass(weakref, crate = "crate")]
591597
struct WeakrefablePyClass {}
@@ -609,7 +615,7 @@ mod tests {
609615
let obj = obj.unwrap();
610616

611617
assert!(obj.is_some());
612-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
618+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
613619
}
614620

615621
drop(object);
@@ -647,7 +653,7 @@ mod tests {
647653
let obj = unsafe { reference.upgrade_as_unchecked::<WeakrefablePyClass>() };
648654

649655
assert!(obj.is_some());
650-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
656+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
651657
}
652658

653659
drop(object);

src/types/weakref/proxy.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ mod tests {
264264
use super::*;
265265
use crate::ffi;
266266
use crate::{py_result_ext::PyResultExt, types::PyDict, types::PyType};
267+
use std::ptr;
267268

268269
fn get_type(py: Python<'_>) -> PyResult<Bound<'_, PyType>> {
269270
let globals = PyDict::new(py);
@@ -349,8 +350,10 @@ mod tests {
349350
let obj = obj.unwrap();
350351

351352
assert!(obj.is_some());
352-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
353-
&& obj.is_exact_instance(&class)));
353+
assert!(
354+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
355+
&& obj.is_exact_instance(&class))
356+
);
354357
}
355358

356359
drop(object);
@@ -381,8 +384,10 @@ mod tests {
381384
let obj = unsafe { reference.upgrade_as_unchecked::<PyAny>() };
382385

383386
assert!(obj.is_some());
384-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
385-
&& obj.is_exact_instance(&class)));
387+
assert!(
388+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
389+
&& obj.is_exact_instance(&class))
390+
);
386391
}
387392

388393
drop(object);
@@ -439,6 +444,7 @@ mod tests {
439444
mod pyo3_pyclass {
440445
use super::*;
441446
use crate::{pyclass, Py};
447+
use std::ptr;
442448

443449
#[pyclass(weakref, crate = "crate")]
444450
struct WeakrefablePyClass {}
@@ -520,7 +526,7 @@ mod tests {
520526
let obj = obj.unwrap();
521527

522528
assert!(obj.is_some());
523-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
529+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
524530
}
525531

526532
drop(object);
@@ -548,7 +554,7 @@ mod tests {
548554
let obj = unsafe { reference.upgrade_as_unchecked::<WeakrefablePyClass>() };
549555

550556
assert!(obj.is_some());
551-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
557+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
552558
}
553559

554560
drop(object);
@@ -611,6 +617,7 @@ mod tests {
611617
use super::*;
612618
use crate::ffi;
613619
use crate::{py_result_ext::PyResultExt, types::PyDict, types::PyType};
620+
use std::ptr;
614621

615622
fn get_type(py: Python<'_>) -> PyResult<Bound<'_, PyType>> {
616623
let globals = PyDict::new(py);
@@ -687,8 +694,10 @@ mod tests {
687694
let obj = obj.unwrap();
688695

689696
assert!(obj.is_some());
690-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
691-
&& obj.is_exact_instance(&class)));
697+
assert!(
698+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
699+
&& obj.is_exact_instance(&class))
700+
);
692701
}
693702

694703
drop(object);
@@ -719,8 +728,10 @@ mod tests {
719728
let obj = unsafe { reference.upgrade_as_unchecked::<PyAny>() };
720729

721730
assert!(obj.is_some());
722-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
723-
&& obj.is_exact_instance(&class)));
731+
assert!(
732+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
733+
&& obj.is_exact_instance(&class))
734+
);
724735
}
725736

726737
drop(object);
@@ -778,6 +789,7 @@ mod tests {
778789
mod pyo3_pyclass {
779790
use super::*;
780791
use crate::{pyclass, pymethods, Py};
792+
use std::ptr;
781793

782794
#[pyclass(weakref, crate = "crate")]
783795
struct WeakrefablePyClass {}
@@ -854,7 +866,7 @@ mod tests {
854866
let obj = obj.unwrap();
855867

856868
assert!(obj.is_some());
857-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
869+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
858870
}
859871

860872
drop(object);
@@ -882,7 +894,7 @@ mod tests {
882894
let obj = unsafe { reference.upgrade_as_unchecked::<WeakrefablePyClass>() };
883895

884896
assert!(obj.is_some());
885-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
897+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
886898
}
887899

888900
drop(object);

0 commit comments

Comments
 (0)