rustlintrust-clippy

How can i solve the useless conversion clippy warning


I've recently updated my rust version using rustup update and now I'm getting new useless conversion to the same type warnings.

This is the code that is causing the warning:

    pub fn replace_attributes(
        &mut self,
        index: Vec<PyIndex>,
        attributes: PyAttributes,
    ) -> PyResult<()> {
        let attributes: Attributes = attributes.into();

        for index in index {
            let current_attributes = self
                .0
                .attributes_mut(&index)
                .map_err(PyCustomError::from)?;

            current_attributes.clone_from(&attributes);
        }

        Ok(())
    }

The warning is

warning: useless conversion to the same type: `pyo3::PyErr`
   --> .../mod.rs:276:18
    |
276 |     ) -> PyResult<()> {
    |                  ^ help: consider removing
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion

Context:

I have a crate that handles my main logic/structs etc. The warning occurs in a separate crate for the python bindings (using pyo3). This binding crate has wrapper structs for most main structs, e.g. PyAttributes wrapping Attributes and PyCustomError mapping CustomError.

The reason for this specific warning seems to be the conversion from PyCustomError to PyErr. The From<PyCustomError> is implemented for PyErr. From<CustomError> cannot be implemented for PyErr as neither CustomError nor PyErr are defined in this crate.

PyResult<T> is a pyo3 internal type alias for Result<T, PyErr>

My steps so far:

I tried to rewrite the function as this

    pub fn replace_attributes(
        &mut self,
        index: Vec<PyIndex>,
        attributes: PyAttributes,
    ) -> PyResult<()> {
        let attributes: Attributes = attributes.into();

        for index in index {
            let current_attributes = self
                .0
                .attributes_mut(&index)?;

            current_attributes.clone_from(&attributes);
        }

        Ok(())
    }

however this obviously does not work as From<CustomError> is not and cannot be implemented for PyErr.

I just do not understand what this warning wants me to do in this case. Running cargo clippy --fix --lib -p ... also does not do anything.

Is there a simple fix for this problem? Am I just not seeing it?

Edit:

The function is part of a struct

#[pyclass]
#[repr(transparent)]
pub struct PyCustomStruct(CustomStruct);

#[pymethods]
impl PyCustomStruct {
    pub fn replace_attributes(
        &mut self,
        index: Vec<PyIndex>,
        attributes: PyAttributes,
    ) -> PyResult<()> {
        let attributes: Attributes = attributes.into();

        for index in index {
            let current_attributes = self
                .0
                .attributes_mut(&index)
                .map_err(PyCustomError::from)?;

            current_attributes.clone_from(&attributes);
        }

        Ok(())
    }
}

Solution

  • Update your PyO3 to the version 0.23.5. The useless warning was fixed here: https://github.com/PyO3/pyo3/pull/4838