Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more lints #2244

Merged
merged 4 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ rustflags = [
"-Dclippy::todo",
"-Dclippy::unnecessary_wraps",
"-Dclippy::useless_transmute",
"-Delided_lifetimes_in_paths",
"-Dunused_lifetimes",
"-Drust_2021_prelude_collisions"
]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
fn string_sum(_py: Python, m: &PyModule) -> PyResult<()> {
fn string_sum(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions benches/bench_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ macro_rules! test_module {
};
}

fn bench_call_0(b: &mut Bencher) {
fn bench_call_0(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let module = test_module!(py, "def foo(): pass");

Expand All @@ -22,7 +22,7 @@ fn bench_call_0(b: &mut Bencher) {
})
}

fn bench_call_method_0(b: &mut Bencher) {
fn bench_call_method_0(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let module = test_module!(
py,
Expand Down
12 changes: 6 additions & 6 deletions benches/bench_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
use std::collections::{BTreeMap, HashMap};

fn iter_dict(b: &mut Bencher) {
fn iter_dict(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
Expand All @@ -18,14 +18,14 @@ fn iter_dict(b: &mut Bencher) {
});
}

fn dict_new(b: &mut Bencher) {
fn dict_new(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
b.iter(|| (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py));
}

fn dict_get_item(b: &mut Bencher) {
fn dict_get_item(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
Expand All @@ -38,15 +38,15 @@ fn dict_get_item(b: &mut Bencher) {
});
}

fn extract_hashmap(b: &mut Bencher) {
fn extract_hashmap(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| HashMap::<u64, u64>::extract(dict));
}

fn extract_btreemap(b: &mut Bencher) {
fn extract_btreemap(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
Expand All @@ -55,7 +55,7 @@ fn extract_btreemap(b: &mut Bencher) {
}

#[cfg(feature = "hashbrown")]
fn extract_hashbrown_map(b: &mut Bencher) {
fn extract_hashbrown_map(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
Expand Down
4 changes: 2 additions & 2 deletions benches/bench_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};

use pyo3::{exceptions::PyValueError, prelude::*};

fn err_new_restore_and_fetch(b: &mut Bencher) {
fn err_new_restore_and_fetch(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
b.iter(|| {
PyValueError::new_err("some exception message").restore(py);
Expand All @@ -11,7 +11,7 @@ fn err_new_restore_and_fetch(b: &mut Bencher) {
})
}

fn err_new_without_gil(b: &mut Bencher) {
fn err_new_without_gil(b: &mut Bencher<'_>) {
b.iter(|| PyValueError::new_err("some exception message"))
}

Expand Down
2 changes: 1 addition & 1 deletion benches/bench_frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum ManyTypes {
String(String),
}

fn enum_from_pyobject(b: &mut Bencher) {
fn enum_from_pyobject(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let obj = PyString::new(py, "hello world");
b.iter(|| {
Expand Down
6 changes: 3 additions & 3 deletions benches/bench_gil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ use criterion::{criterion_group, criterion_main, BatchSize, Bencher, Criterion};

use pyo3::{prelude::*, GILPool};

fn bench_clean_gilpool_new(b: &mut Bencher) {
fn bench_clean_gilpool_new(b: &mut Bencher<'_>) {
Python::with_gil(|_py| {
b.iter(|| {
let _ = unsafe { GILPool::new() };
});
});
}

fn bench_clean_acquire_gil(b: &mut Bencher) {
fn bench_clean_acquire_gil(b: &mut Bencher<'_>) {
// Acquiring first GIL will also create a "clean" GILPool, so this measures the Python overhead.
b.iter(|| {
let _ = Python::acquire_gil();
});
}

fn bench_dirty_acquire_gil(b: &mut Bencher) {
fn bench_dirty_acquire_gil(b: &mut Bencher<'_>) {
let obj = Python::with_gil(|py| py.None());
b.iter_batched(
|| {
Expand Down
8 changes: 4 additions & 4 deletions benches/bench_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use pyo3::prelude::*;
use pyo3::types::PyList;

fn iter_list(b: &mut Bencher) {
fn iter_list(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
Expand All @@ -17,14 +17,14 @@ fn iter_list(b: &mut Bencher) {
});
}

fn list_new(b: &mut Bencher) {
fn list_new(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
b.iter(|| PyList::new(py, 0..LEN));
}

fn list_get_item(b: &mut Bencher) {
fn list_get_item(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
Expand All @@ -38,7 +38,7 @@ fn list_get_item(b: &mut Bencher) {
}

#[cfg(not(Py_LIMITED_API))]
fn list_get_item_unchecked(b: &mut Bencher) {
fn list_get_item_unchecked(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
Expand Down
2 changes: 1 addition & 1 deletion benches/bench_pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl MyClass {
}
}

pub fn first_time_init(b: &mut criterion::Bencher) {
pub fn first_time_init(b: &mut criterion::Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
b.iter(|| {
Expand Down
2 changes: 1 addition & 1 deletion benches/bench_pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};

use pyo3::prelude::*;

fn drop_many_objects(b: &mut Bencher) {
fn drop_many_objects(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
b.iter(|| {
Expand Down
8 changes: 4 additions & 4 deletions benches/bench_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pyo3::prelude::*;
use pyo3::types::PySet;
use std::collections::{BTreeSet, HashSet};

fn iter_set(b: &mut Bencher) {
fn iter_set(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
Expand All @@ -18,15 +18,15 @@ fn iter_set(b: &mut Bencher) {
});
}

fn extract_hashset(b: &mut Bencher) {
fn extract_hashset(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| HashSet::<u64>::extract(set));
}

fn extract_btreeset(b: &mut Bencher) {
fn extract_btreeset(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
Expand All @@ -35,7 +35,7 @@ fn extract_btreeset(b: &mut Bencher) {
}

#[cfg(feature = "hashbrown")]
fn extract_hashbrown_set(b: &mut Bencher) {
fn extract_hashbrown_set(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
Expand Down
8 changes: 4 additions & 4 deletions benches/bench_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use pyo3::prelude::*;
use pyo3::types::PyTuple;

fn iter_tuple(b: &mut Bencher) {
fn iter_tuple(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
Expand All @@ -17,14 +17,14 @@ fn iter_tuple(b: &mut Bencher) {
});
}

fn tuple_new(b: &mut Bencher) {
fn tuple_new(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
b.iter(|| PyTuple::new(py, 0..LEN));
}

fn tuple_get_item(b: &mut Bencher) {
fn tuple_get_item(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
Expand All @@ -38,7 +38,7 @@ fn tuple_get_item(b: &mut Bencher) {
}

#[cfg(not(Py_LIMITED_API))]
fn tuple_get_item_unchecked(b: &mut Bencher) {
fn tuple_get_item_unchecked(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
Expand Down
4 changes: 2 additions & 2 deletions examples/decorator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl PyCounter {
#[args(args = "*", kwargs = "**")]
fn __call__(
&mut self,
py: Python,
py: Python<'_>,
args: &PyTuple,
kwargs: Option<&PyDict>,
) -> PyResult<Py<PyAny>> {
Expand All @@ -48,7 +48,7 @@ impl PyCounter {
}

#[pymodule]
pub fn decorator(_py: Python, module: &PyModule) -> PyResult<()> {
pub fn decorator(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
module.add_class::<PyCounter>()?;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/maturin-starter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl ExampleClass {

/// An example module implemented in Rust using PyO3.
#[pymodule]
fn maturin_starter(py: Python, m: &PyModule) -> PyResult<()> {
fn maturin_starter(py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<ExampleClass>()?;
m.add_wrapped(wrap_pymodule!(submodule))?;

Expand Down
2 changes: 1 addition & 1 deletion examples/maturin-starter/src/submodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl SubmoduleClass {
}

#[pymodule]
pub fn submodule(_py: Python, m: &PyModule) -> PyResult<()> {
pub fn submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<SubmoduleClass>()?;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/setuptools-rust-starter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl ExampleClass {

/// An example module implemented in Rust using PyO3.
#[pymodule]
fn _setuptools_rust_starter(py: Python, m: &PyModule) -> PyResult<()> {
fn _setuptools_rust_starter(py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<ExampleClass>()?;
m.add_wrapped(wrap_pymodule!(submodule))?;

Expand Down
2 changes: 1 addition & 1 deletion examples/setuptools-rust-starter/src/submodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl SubmoduleClass {
}

#[pymodule]
pub fn submodule(_py: Python, m: &PyModule) -> PyResult<()> {
pub fn submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<SubmoduleClass>()?;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/word-count/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn search_sequential(contents: &str, needle: &str) -> usize {
}

#[pyfunction]
fn search_sequential_allow_threads(py: Python, contents: &str, needle: &str) -> usize {
fn search_sequential_allow_threads(py: Python<'_>, contents: &str, needle: &str) -> usize {
py.allow_threads(|| search_sequential(contents, needle))
}

Expand Down
16 changes: 8 additions & 8 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ The next step is to create the module initializer and add our class to it
# struct Number(i32);
#
#[pymodule]
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<Number>()?;
Ok(())
}
Expand Down Expand Up @@ -267,7 +267,7 @@ impl SubClass {
(SubClass { val2: 15 }, BaseClass::new())
}

fn method2(self_: PyRef<Self>) -> PyResult<usize> {
fn method2(self_: PyRef<'_, Self>) -> PyResult<usize> {
let super_ = self_.as_ref(); // Get &BaseClass
super_.method().map(|x| x * self_.val2)
}
Expand All @@ -286,9 +286,9 @@ impl SubSubClass {
.add_subclass(SubSubClass{val3: 20})
}

fn method3(self_: PyRef<Self>) -> PyResult<usize> {
fn method3(self_: PyRef<'_, Self>) -> PyResult<usize> {
let v = self_.val3;
let super_ = self_.into_super(); // Get PyRef<SubClass>
let super_ = self_.into_super(); // Get PyRef<'_, SubClass>
SubClass::method2(super_).map(|x| x * v)
}
}
Expand Down Expand Up @@ -323,7 +323,7 @@ impl DictWithCounter {
fn new() -> Self {
Self::default()
}
fn set(mut self_: PyRefMut<Self>, key: String, value: &PyAny) -> PyResult<()> {
fn set(mut self_: PyRefMut<'_, Self>, key: String, value: &PyAny) -> PyResult<()> {
self_.counter.entry(key.clone()).or_insert(0);
let py = self_.py();
let dict: &PyDict = unsafe { py.from_borrowed_ptr_or_err(self_.as_ptr())? };
Expand Down Expand Up @@ -518,7 +518,7 @@ gets injected by the method wrapper, e.g.
# }
#[pymethods]
impl MyClass {
fn method2(&self, py: Python) -> PyResult<i32> {
fn method2(&self, py: Python<'_>) -> PyResult<i32> {
Ok(10)
}
}
Expand Down Expand Up @@ -953,7 +953,7 @@ unsafe impl pyo3::PyTypeInfo for MyClass {
const MODULE: Option<&'static str> = None;

#[inline]
fn type_object_raw(py: pyo3::Python) -> *mut pyo3::ffi::PyTypeObject {
fn type_object_raw(py: pyo3::Python<'_>) -> *mut pyo3::ffi::PyTypeObject {
use pyo3::type_object::LazyStaticType;
static TYPE_OBJECT: LazyStaticType = LazyStaticType::new();
TYPE_OBJECT.get_or_init::<Self>(py)
Expand All @@ -967,7 +967,7 @@ impl pyo3::pyclass::PyClass for MyClass {
}

impl pyo3::IntoPy<PyObject> for MyClass {
fn into_py(self, py: pyo3::Python) -> pyo3::PyObject {
fn into_py(self, py: pyo3::Python<'_>) -> pyo3::PyObject {
pyo3::IntoPy::into_py(pyo3::Py::new(py, self).unwrap(), py)
}
}
Expand Down
Loading