Skip to content

Commit 90d5f56

Browse files
authored
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 66034ef commit 90d5f56

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
@@ -20,6 +20,7 @@ mod impls;
2020
use crate::conversion::IntoPyObject;
2121
use err_state::{PyErrState, PyErrStateLazyFnOutput, PyErrStateNormalized};
2222
use std::convert::Infallible;
23+
use std::ptr;
2324

2425
/// Represents a Python exception.
2526
///
@@ -324,7 +325,10 @@ impl PyErr {
324325
pub fn take(py: Python<'_>) -> Option<PyErr> {
325326
let state = PyErrStateNormalized::take(py)?;
326327
let pvalue = state.pvalue.bind(py);
327-
if pvalue.get_type().as_ptr() == PanicException::type_object_raw(py).cast() {
328+
if ptr::eq(
329+
pvalue.get_type().as_ptr(),
330+
PanicException::type_object_raw(py).cast(),
331+
) {
328332
let msg: String = pvalue
329333
.str()
330334
.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
@@ -17,6 +17,7 @@ use std::{
1717
ffi::{CStr, CString},
1818
marker::PhantomData,
1919
os::raw::{c_int, c_void},
20+
ptr,
2021
ptr::NonNull,
2122
sync::Mutex,
2223
thread,
@@ -948,7 +949,7 @@ pub unsafe extern "C" fn alloc_with_freelist<T: PyClassWithFreeList>(
948949
let self_type = T::type_object_raw(py);
949950
// If this type is a variable type or the subtype is not equal to this type, we cannot use the
950951
// freelist
951-
if nitems == 0 && subtype == self_type {
952+
if nitems == 0 && ptr::eq(subtype, self_type) {
952953
let mut free_list = T::get_free_list(py).lock().unwrap();
953954
if let Some(obj) = free_list.pop() {
954955
drop(free_list);
@@ -1141,7 +1142,6 @@ impl<T> PyClassThreadChecker<T> for ThreadCheckerImpl {
11411142
}
11421143

11431144
/// Trait denoting that this class is suitable to be used as a base type for PyClass.
1144-
11451145
#[cfg_attr(
11461146
all(diagnostic_namespace, Py_LIMITED_API),
11471147
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
@@ -14,6 +14,7 @@ use crate::{gil, PyTypeCheck};
1414
use std::marker::PhantomData;
1515
use std::mem::ManuallyDrop;
1616
use std::ops::Deref;
17+
use std::ptr;
1718
use std::ptr::NonNull;
1819

1920
/// Owned or borrowed gil-bound Python smart pointer
@@ -1315,7 +1316,7 @@ impl<T> Py<T> {
13151316
/// This is equivalent to the Python expression `self is other`.
13161317
#[inline]
13171318
pub fn is<U: AsPyPointer>(&self, o: &U) -> bool {
1318-
self.as_ptr() == o.as_ptr()
1319+
ptr::eq(self.as_ptr(), o.as_ptr())
13191320
}
13201321

13211322
/// Gets the reference count of the `ffi::PyObject` pointer.
@@ -1387,7 +1388,7 @@ impl<T> Py<T> {
13871388
///
13881389
/// This is equivalent to the Python expression `self is None`.
13891390
pub fn is_none(&self, _py: Python<'_>) -> bool {
1390-
unsafe { ffi::Py_None() == self.as_ptr() }
1391+
unsafe { ptr::eq(ffi::Py_None(), self.as_ptr()) }
13911392
}
13921393

13931394
/// 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`
@@ -71,7 +72,12 @@ pub unsafe trait PyTypeInfo: Sized {
7172
/// Checks if `object` is an instance of this type.
7273
#[inline]
7374
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool {
74-
unsafe { ffi::Py_TYPE(object.as_ptr()) == Self::type_object_raw(object.py()) }
75+
unsafe {
76+
ptr::eq(
77+
ffi::Py_TYPE(object.as_ptr()),
78+
Self::type_object_raw(object.py()),
79+
)
80+
}
7581
}
7682
}
7783

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
///
@@ -951,7 +952,7 @@ macro_rules! implement_binop {
951952
impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
952953
#[inline]
953954
fn is<T: AsPyPointer>(&self, other: &T) -> bool {
954-
self.as_ptr() == other.as_ptr()
955+
ptr::eq(self.as_ptr(), other.as_ptr())
955956
}
956957

957958
fn hasattr<N>(&self, attr_name: N) -> PyResult<bool>
@@ -1355,7 +1356,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
13551356

13561357
#[inline]
13571358
fn is_none(&self) -> bool {
1358-
unsafe { ffi::Py_None() == self.as_ptr() }
1359+
unsafe { ptr::eq(ffi::Py_None(), self.as_ptr()) }
13591360
}
13601361

13611362
fn is_empty(&self) -> PyResult<bool> {

src/types/boolobject.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
use super::any::PyAnyMethods;
99
use crate::conversion::IntoPyObject;
1010
use std::convert::Infallible;
11+
use std::ptr;
1112

1213
/// Represents a Python `bool`.
1314
///
@@ -51,7 +52,7 @@ pub trait PyBoolMethods<'py>: crate::sealed::Sealed {
5152
impl<'py> PyBoolMethods<'py> for Bound<'py, PyBool> {
5253
#[inline]
5354
fn is_true(&self) -> bool {
54-
self.as_ptr() == unsafe { crate::ffi::Py_True() }
55+
unsafe { ptr::eq(self.as_ptr(), ffi::Py_True()) }
5556
}
5657
}
5758

src/types/sequence.rs

Lines changed: 2 additions & 1 deletion
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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ mod tests {
351351
use super::*;
352352
use crate::ffi;
353353
use crate::{py_result_ext::PyResultExt, types::PyType};
354+
use std::ptr;
354355

355356
fn get_type(py: Python<'_>) -> PyResult<Bound<'_, PyType>> {
356357
py.run(ffi::c_str!("class A:\n pass\n"), None, None)?;
@@ -379,8 +380,10 @@ mod tests {
379380
let obj = obj.unwrap();
380381

381382
assert!(obj.is_some());
382-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
383-
&& obj.is_exact_instance(&class)));
383+
assert!(
384+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
385+
&& obj.is_exact_instance(&class))
386+
);
384387
}
385388

386389
drop(object);
@@ -421,8 +424,10 @@ mod tests {
421424
let obj = unsafe { reference.upgrade_as_unchecked::<PyAny>() };
422425

423426
assert!(obj.is_some());
424-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
425-
&& obj.is_exact_instance(&class)));
427+
assert!(
428+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
429+
&& obj.is_exact_instance(&class))
430+
);
426431
}
427432

428433
drop(object);
@@ -481,6 +486,7 @@ mod tests {
481486
mod pyo3_pyclass {
482487
use super::*;
483488
use crate::{pyclass, Py};
489+
use std::ptr;
484490

485491
#[pyclass(weakref, crate = "crate")]
486492
struct WeakrefablePyClass {}
@@ -504,7 +510,7 @@ mod tests {
504510
let obj = obj.unwrap();
505511

506512
assert!(obj.is_some());
507-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
513+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
508514
}
509515

510516
drop(object);
@@ -542,7 +548,7 @@ mod tests {
542548
let obj = unsafe { reference.upgrade_as_unchecked::<WeakrefablePyClass>() };
543549

544550
assert!(obj.is_some());
545-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
551+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
546552
}
547553

548554
drop(object);

src/types/weakref/proxy.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ mod tests {
243243
use super::*;
244244
use crate::ffi;
245245
use crate::{py_result_ext::PyResultExt, types::PyDict, types::PyType};
246+
use std::ptr;
246247

247248
fn get_type(py: Python<'_>) -> PyResult<Bound<'_, PyType>> {
248249
let globals = PyDict::new(py);
@@ -328,8 +329,10 @@ mod tests {
328329
let obj = obj.unwrap();
329330

330331
assert!(obj.is_some());
331-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
332-
&& obj.is_exact_instance(&class)));
332+
assert!(
333+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
334+
&& obj.is_exact_instance(&class))
335+
);
333336
}
334337

335338
drop(object);
@@ -360,8 +363,10 @@ mod tests {
360363
let obj = unsafe { reference.upgrade_as_unchecked::<PyAny>() };
361364

362365
assert!(obj.is_some());
363-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
364-
&& obj.is_exact_instance(&class)));
366+
assert!(
367+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
368+
&& obj.is_exact_instance(&class))
369+
);
365370
}
366371

367372
drop(object);
@@ -418,6 +423,7 @@ mod tests {
418423
mod pyo3_pyclass {
419424
use super::*;
420425
use crate::{pyclass, Py};
426+
use std::ptr;
421427

422428
#[pyclass(weakref, crate = "crate")]
423429
struct WeakrefablePyClass {}
@@ -499,7 +505,7 @@ mod tests {
499505
let obj = obj.unwrap();
500506

501507
assert!(obj.is_some());
502-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
508+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
503509
}
504510

505511
drop(object);
@@ -527,7 +533,7 @@ mod tests {
527533
let obj = unsafe { reference.upgrade_as_unchecked::<WeakrefablePyClass>() };
528534

529535
assert!(obj.is_some());
530-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
536+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
531537
}
532538

533539
drop(object);
@@ -573,6 +579,7 @@ mod tests {
573579
use super::*;
574580
use crate::ffi;
575581
use crate::{py_result_ext::PyResultExt, types::PyDict, types::PyType};
582+
use std::ptr;
576583

577584
fn get_type(py: Python<'_>) -> PyResult<Bound<'_, PyType>> {
578585
let globals = PyDict::new(py);
@@ -649,8 +656,10 @@ mod tests {
649656
let obj = obj.unwrap();
650657

651658
assert!(obj.is_some());
652-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
653-
&& obj.is_exact_instance(&class)));
659+
assert!(
660+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
661+
&& obj.is_exact_instance(&class))
662+
);
654663
}
655664

656665
drop(object);
@@ -681,8 +690,10 @@ mod tests {
681690
let obj = unsafe { reference.upgrade_as_unchecked::<PyAny>() };
682691

683692
assert!(obj.is_some());
684-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()
685-
&& obj.is_exact_instance(&class)));
693+
assert!(
694+
obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())
695+
&& obj.is_exact_instance(&class))
696+
);
686697
}
687698

688699
drop(object);
@@ -722,6 +733,7 @@ mod tests {
722733
mod pyo3_pyclass {
723734
use super::*;
724735
use crate::{pyclass, pymethods, Py};
736+
use std::ptr;
725737

726738
#[pyclass(weakref, crate = "crate")]
727739
struct WeakrefablePyClass {}
@@ -798,7 +810,7 @@ mod tests {
798810
let obj = obj.unwrap();
799811

800812
assert!(obj.is_some());
801-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
813+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
802814
}
803815

804816
drop(object);
@@ -826,7 +838,7 @@ mod tests {
826838
let obj = unsafe { reference.upgrade_as_unchecked::<WeakrefablePyClass>() };
827839

828840
assert!(obj.is_some());
829-
assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr()));
841+
assert!(obj.map_or(false, |obj| ptr::eq(obj.as_ptr(), object.as_ptr())));
830842
}
831843

832844
drop(object);

0 commit comments

Comments
 (0)