clippy: deny / fix used-underscope-binding lint

This commit is contained in:
David Hewitt 2023-07-14 13:42:35 +01:00
parent 09d79f1a5f
commit 45ff25cd2a
8 changed files with 46 additions and 38 deletions

View File

@ -13,6 +13,7 @@ rustflags = [
"-Dclippy::todo", "-Dclippy::todo",
"-Dclippy::unnecessary_wraps", "-Dclippy::unnecessary_wraps",
"-Dclippy::useless_transmute", "-Dclippy::useless_transmute",
"-Dclippy::used_underscore_binding",
"-Delided_lifetimes_in_paths", "-Delided_lifetimes_in_paths",
"-Dunused_lifetimes", "-Dunused_lifetimes",
"-Drust_2021_prelude_collisions" "-Drust_2021_prelude_collisions"

View File

@ -177,8 +177,8 @@ extern "C" {
#[inline(always)] #[inline(always)]
pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject { pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject {
assert!(!arg.is_null()); assert!(!arg.is_null());
let _args = [std::ptr::null_mut(), arg]; let args_array = [std::ptr::null_mut(), arg];
let args = _args.as_ptr().offset(1); // For PY_VECTORCALL_ARGUMENTS_OFFSET let args = args_array.as_ptr().offset(1); // For PY_VECTORCALL_ARGUMENTS_OFFSET
let tstate = PyThreadState_GET(); let tstate = PyThreadState_GET();
let nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET; let nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
_PyObject_VectorcallTstate(tstate, func, args, nargsf as size_t, std::ptr::null_mut()) _PyObject_VectorcallTstate(tstate, func, args, nargsf as size_t, std::ptr::null_mut())

View File

@ -148,8 +148,8 @@ const STATE_READY_WIDTH: u8 = 1;
#[repr(C)] #[repr(C)]
#[repr(align(4))] #[repr(align(4))]
struct PyASCIIObjectState { struct PyASCIIObjectState {
_bitfield_align: [u8; 0], bitfield_align: [u8; 0],
_bitfield: BitfieldUnit<[u8; 4usize]>, bitfield: BitfieldUnit<[u8; 4usize]>,
} }
// c_uint and u32 are not necessarily the same type on all targets / architectures // c_uint and u32 are not necessarily the same type on all targets / architectures
@ -158,7 +158,7 @@ impl PyASCIIObjectState {
#[inline] #[inline]
unsafe fn interned(&self) -> c_uint { unsafe fn interned(&self) -> c_uint {
std::mem::transmute( std::mem::transmute(
self._bitfield self.bitfield
.get(STATE_INTERNED_INDEX, STATE_INTERNED_WIDTH) as u32, .get(STATE_INTERNED_INDEX, STATE_INTERNED_WIDTH) as u32,
) )
} }
@ -166,57 +166,57 @@ impl PyASCIIObjectState {
#[inline] #[inline]
unsafe fn set_interned(&mut self, val: c_uint) { unsafe fn set_interned(&mut self, val: c_uint) {
let val: u32 = std::mem::transmute(val); let val: u32 = std::mem::transmute(val);
self._bitfield self.bitfield
.set(STATE_INTERNED_INDEX, STATE_INTERNED_WIDTH, val as u64) .set(STATE_INTERNED_INDEX, STATE_INTERNED_WIDTH, val as u64)
} }
#[inline] #[inline]
unsafe fn kind(&self) -> c_uint { unsafe fn kind(&self) -> c_uint {
std::mem::transmute(self._bitfield.get(STATE_KIND_INDEX, STATE_KIND_WIDTH) as u32) std::mem::transmute(self.bitfield.get(STATE_KIND_INDEX, STATE_KIND_WIDTH) as u32)
} }
#[inline] #[inline]
unsafe fn set_kind(&mut self, val: c_uint) { unsafe fn set_kind(&mut self, val: c_uint) {
let val: u32 = std::mem::transmute(val); let val: u32 = std::mem::transmute(val);
self._bitfield self.bitfield
.set(STATE_KIND_INDEX, STATE_KIND_WIDTH, val as u64) .set(STATE_KIND_INDEX, STATE_KIND_WIDTH, val as u64)
} }
#[inline] #[inline]
unsafe fn compact(&self) -> c_uint { unsafe fn compact(&self) -> c_uint {
std::mem::transmute(self._bitfield.get(STATE_COMPACT_INDEX, STATE_COMPACT_WIDTH) as u32) std::mem::transmute(self.bitfield.get(STATE_COMPACT_INDEX, STATE_COMPACT_WIDTH) as u32)
} }
#[inline] #[inline]
unsafe fn set_compact(&mut self, val: c_uint) { unsafe fn set_compact(&mut self, val: c_uint) {
let val: u32 = std::mem::transmute(val); let val: u32 = std::mem::transmute(val);
self._bitfield self.bitfield
.set(STATE_COMPACT_INDEX, STATE_COMPACT_WIDTH, val as u64) .set(STATE_COMPACT_INDEX, STATE_COMPACT_WIDTH, val as u64)
} }
#[inline] #[inline]
unsafe fn ascii(&self) -> c_uint { unsafe fn ascii(&self) -> c_uint {
std::mem::transmute(self._bitfield.get(STATE_ASCII_INDEX, STATE_ASCII_WIDTH) as u32) std::mem::transmute(self.bitfield.get(STATE_ASCII_INDEX, STATE_ASCII_WIDTH) as u32)
} }
#[inline] #[inline]
unsafe fn set_ascii(&mut self, val: c_uint) { unsafe fn set_ascii(&mut self, val: c_uint) {
let val: u32 = std::mem::transmute(val); let val: u32 = std::mem::transmute(val);
self._bitfield self.bitfield
.set(STATE_ASCII_INDEX, STATE_ASCII_WIDTH, val as u64) .set(STATE_ASCII_INDEX, STATE_ASCII_WIDTH, val as u64)
} }
#[cfg(not(Py_3_12))] #[cfg(not(Py_3_12))]
#[inline] #[inline]
unsafe fn ready(&self) -> c_uint { unsafe fn ready(&self) -> c_uint {
std::mem::transmute(self._bitfield.get(STATE_READY_INDEX, STATE_READY_WIDTH) as u32) std::mem::transmute(self.bitfield.get(STATE_READY_INDEX, STATE_READY_WIDTH) as u32)
} }
#[cfg(not(Py_3_12))] #[cfg(not(Py_3_12))]
#[inline] #[inline]
unsafe fn set_ready(&mut self, val: c_uint) { unsafe fn set_ready(&mut self, val: c_uint) {
let val: u32 = std::mem::transmute(val); let val: u32 = std::mem::transmute(val);
self._bitfield self.bitfield
.set(STATE_READY_INDEX, STATE_READY_WIDTH, val as u64) .set(STATE_READY_INDEX, STATE_READY_WIDTH, val as u64)
} }
} }
@ -225,8 +225,8 @@ impl From<u32> for PyASCIIObjectState {
#[inline] #[inline]
fn from(value: u32) -> Self { fn from(value: u32) -> Self {
PyASCIIObjectState { PyASCIIObjectState {
_bitfield_align: [], bitfield_align: [],
_bitfield: BitfieldUnit::new(value.to_ne_bytes()), bitfield: BitfieldUnit::new(value.to_ne_bytes()),
} }
} }
} }
@ -234,7 +234,7 @@ impl From<u32> for PyASCIIObjectState {
impl From<PyASCIIObjectState> for u32 { impl From<PyASCIIObjectState> for u32 {
#[inline] #[inline]
fn from(value: PyASCIIObjectState) -> Self { fn from(value: PyASCIIObjectState) -> Self {
u32::from_ne_bytes(value._bitfield.storage) u32::from_ne_bytes(value.bitfield.storage)
} }
} }

View File

@ -156,7 +156,7 @@ impl ExtractErrorMode {
impl SelfType { impl SelfType {
pub fn receiver(&self, cls: &syn::Type, error_mode: ExtractErrorMode) -> TokenStream { pub fn receiver(&self, cls: &syn::Type, error_mode: ExtractErrorMode) -> TokenStream {
let py = syn::Ident::new("_py", Span::call_site()); let py = syn::Ident::new("_py", Span::call_site());
let _slf = syn::Ident::new("_slf", Span::call_site()); let slf = syn::Ident::new("_slf", Span::call_site());
match self { match self {
SelfType::Receiver { span, mutable } => { SelfType::Receiver { span, mutable } => {
let method = if *mutable { let method = if *mutable {
@ -168,7 +168,7 @@ impl SelfType {
&py, &py,
quote_spanned! { *span => quote_spanned! { *span =>
_pyo3::impl_::extract_argument::#method::<#cls>( _pyo3::impl_::extract_argument::#method::<#cls>(
#py.from_borrowed_ptr::<_pyo3::PyAny>(#_slf), #py.from_borrowed_ptr::<_pyo3::PyAny>(#slf),
&mut { _pyo3::impl_::extract_argument::FunctionArgumentHolder::INIT }, &mut { _pyo3::impl_::extract_argument::FunctionArgumentHolder::INIT },
) )
}, },
@ -178,10 +178,10 @@ impl SelfType {
error_mode.handle_error( error_mode.handle_error(
&py, &py,
quote_spanned! { *span => quote_spanned! { *span =>
#py.from_borrowed_ptr::<_pyo3::PyAny>(#_slf).downcast::<_pyo3::PyCell<#cls>>() #py.from_borrowed_ptr::<_pyo3::PyAny>(#slf).downcast::<_pyo3::PyCell<#cls>>()
.map_err(::std::convert::Into::<_pyo3::PyErr>::into) .map_err(::std::convert::Into::<_pyo3::PyErr>::into)
.and_then( .and_then(
#[allow(clippy::useless_conversion)] // In case _slf is PyCell<Self> #[allow(clippy::useless_conversion)] // In case slf is PyCell<Self>
|cell| ::std::convert::TryFrom::try_from(cell).map_err(::std::convert::Into::into) |cell| ::std::convert::TryFrom::try_from(cell).map_err(::std::convert::Into::into)
) )

View File

@ -38,7 +38,11 @@ impl<'p> PyVisit<'p> {
/// Creates the PyVisit from the arguments to tp_traverse /// Creates the PyVisit from the arguments to tp_traverse
#[doc(hidden)] #[doc(hidden)]
pub unsafe fn from_raw(visit: ffi::visitproc, arg: *mut c_void, _py: Python<'p>) -> Self { pub unsafe fn from_raw(visit: ffi::visitproc, arg: *mut c_void, py: Python<'p>) -> Self {
Self { visit, arg, _py } Self {
visit,
arg,
_py: py,
}
} }
} }

View File

@ -82,14 +82,14 @@ fn tuple_class_with_new() {
#[pyclass] #[pyclass]
#[derive(Debug)] #[derive(Debug)]
struct NewWithOneArg { struct NewWithOneArg {
_data: i32, data: i32,
} }
#[pymethods] #[pymethods]
impl NewWithOneArg { impl NewWithOneArg {
#[new] #[new]
fn new(arg: i32) -> NewWithOneArg { fn new(arg: i32) -> NewWithOneArg {
NewWithOneArg { _data: arg } NewWithOneArg { data: arg }
} }
} }
@ -100,14 +100,14 @@ fn new_with_one_arg() {
let wrp = typeobj.call((42,), None).unwrap(); let wrp = typeobj.call((42,), None).unwrap();
let obj = wrp.downcast::<PyCell<NewWithOneArg>>().unwrap(); let obj = wrp.downcast::<PyCell<NewWithOneArg>>().unwrap();
let obj_ref = obj.borrow(); let obj_ref = obj.borrow();
assert_eq!(obj_ref._data, 42); assert_eq!(obj_ref.data, 42);
}); });
} }
#[pyclass] #[pyclass]
struct NewWithTwoArgs { struct NewWithTwoArgs {
_data1: i32, data1: i32,
_data2: i32, data2: i32,
} }
#[pymethods] #[pymethods]
@ -115,8 +115,8 @@ impl NewWithTwoArgs {
#[new] #[new]
fn new(arg1: i32, arg2: i32) -> Self { fn new(arg1: i32, arg2: i32) -> Self {
NewWithTwoArgs { NewWithTwoArgs {
_data1: arg1, data1: arg1,
_data2: arg2, data2: arg2,
} }
} }
} }
@ -131,8 +131,8 @@ fn new_with_two_args() {
.unwrap(); .unwrap();
let obj = wrp.downcast::<PyCell<NewWithTwoArgs>>().unwrap(); let obj = wrp.downcast::<PyCell<NewWithTwoArgs>>().unwrap();
let obj_ref = obj.borrow(); let obj_ref = obj.borrow();
assert_eq!(obj_ref._data1, 10); assert_eq!(obj_ref.data1, 10);
assert_eq!(obj_ref._data2, 20); assert_eq!(obj_ref.data2, 20);
}); });
} }

View File

@ -29,7 +29,10 @@ fn test_default_interpreter() {
unsafe { ffi::PyConfig_InitPythonConfig(&mut config) }; unsafe { ffi::PyConfig_InitPythonConfig(&mut config) };
// Require manually calling _Py_InitializeMain to exercise more ffi code // Require manually calling _Py_InitializeMain to exercise more ffi code
config._init_main = 0; #[allow(clippy::used_underscore_binding)]
{
config._init_main = 0;
}
#[cfg(Py_3_10)] #[cfg(Py_3_10)]
unsafe { unsafe {

View File

@ -14,14 +14,14 @@ struct EmptyClass;
struct ExampleClass { struct ExampleClass {
#[pyo3(get, set)] #[pyo3(get, set)]
value: i32, value: i32,
_custom_attr: Option<i32>, custom_attr: Option<i32>,
} }
#[pymethods] #[pymethods]
impl ExampleClass { impl ExampleClass {
fn __getattr__(&self, py: Python<'_>, attr: &str) -> PyResult<PyObject> { fn __getattr__(&self, py: Python<'_>, attr: &str) -> PyResult<PyObject> {
if attr == "special_custom_attr" { if attr == "special_custom_attr" {
Ok(self._custom_attr.into_py(py)) Ok(self.custom_attr.into_py(py))
} else { } else {
Err(PyAttributeError::new_err(attr.to_string())) Err(PyAttributeError::new_err(attr.to_string()))
} }
@ -29,7 +29,7 @@ impl ExampleClass {
fn __setattr__(&mut self, attr: &str, value: &PyAny) -> PyResult<()> { fn __setattr__(&mut self, attr: &str, value: &PyAny) -> PyResult<()> {
if attr == "special_custom_attr" { if attr == "special_custom_attr" {
self._custom_attr = Some(value.extract()?); self.custom_attr = Some(value.extract()?);
Ok(()) Ok(())
} else { } else {
Err(PyAttributeError::new_err(attr.to_string())) Err(PyAttributeError::new_err(attr.to_string()))
@ -38,7 +38,7 @@ impl ExampleClass {
fn __delattr__(&mut self, attr: &str) -> PyResult<()> { fn __delattr__(&mut self, attr: &str) -> PyResult<()> {
if attr == "special_custom_attr" { if attr == "special_custom_attr" {
self._custom_attr = None; self.custom_attr = None;
Ok(()) Ok(())
} else { } else {
Err(PyAttributeError::new_err(attr.to_string())) Err(PyAttributeError::new_err(attr.to_string()))
@ -68,7 +68,7 @@ fn make_example(py: Python<'_>) -> &PyCell<ExampleClass> {
py, py,
ExampleClass { ExampleClass {
value: 5, value: 5,
_custom_attr: Some(20), custom_attr: Some(20),
}, },
) )
.unwrap() .unwrap()