Skip to content

Commit 0774334

Browse files
authored
Merge pull request #555 from macisamuele/maci-run-clippy-on-all-the-codebase
Run clippy on all the codebase
2 parents e752bd2 + f15fa21 commit 0774334

File tree

12 files changed

+57
-30
lines changed

12 files changed

+57
-30
lines changed

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ matrix:
1818
python: "3.8-dev"
1919
- name: Minimum nightly
2020
python: "3.7"
21-
# Keep this synced up with build.rs
22-
env: TRAVIS_RUST_VERSION=nightly-2019-07-19
23-
# Tested via anaconda PyPy (since travis's PyPy version is too old)
24-
- name: PyPy3.5 7.0
21+
# Keep this synced up with build.rs and ensure that the nightly version does have clippy available
22+
# https://static.rust-lang.org/dist/YYYY-MM-DD/clippy-nightly-x86_64-unknown-linux-gnu.tar.gz exists
23+
env: TRAVIS_RUST_VERSION=nightly-2019-07-19
24+
- name: PyPy3.5 7.0 # Tested via anaconda PyPy (since travis's PyPy version is too old)
2525
python: "3.7"
2626
env: FEATURES="pypy" PATH="$PATH:/opt/anaconda/envs/pypy3/bin"
2727
allow_failures:

Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
.PHONY: test test_py3 publish
1+
.PHONY: test test_py3 publish clippy lint fmt
2+
3+
# Constants used in clippy target
4+
CLIPPY_LINTS_TO_DENY := warnings
5+
CLIPPY_LINTS_TO_ALLOW := clippy::new_ret_no_self
26

37
test:
48
cargo test
5-
cargo clippy
9+
${MAKE} clippy
610
tox
711
for example in examples/*; do tox -e py -c $$example/tox.ini; done
812

913
test_py3:
1014
tox -e py3
1115
for example in examples/*; do tox -e py3 -c $$example/tox.ini; done
1216

17+
fmt:
18+
cargo fmt --all -- --check
19+
20+
clippy:
21+
@touch src/lib.rs # Touching file to ensure that cargo clippy will re-check the project
22+
cargo clippy --all-features --all-targets -- \
23+
$(addprefix -D ,${CLIPPY_LINTS_TO_DENY}) \
24+
$(addprefix -A ,${CLIPPY_LINTS_TO_ALLOW})
25+
26+
lint: fmt clippy
27+
@true
28+
1329
publish: test
1430
cargo publish --manifest-path pyo3-derive-backend/Cargo.toml
1531
cargo publish --manifest-path pyo3cls/Cargo.toml

benches/bench_dict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use test::Bencher;
99
fn iter_dict(b: &mut Bencher) {
1010
let gil = Python::acquire_gil();
1111
let py = gil.python();
12-
const LEN: usize = 1_000_00;
12+
const LEN: usize = 100_000;
1313
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
1414
let mut sum = 0;
1515
b.iter(|| {

ci/travis/test.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ else
1111
fi
1212

1313
if [ "$TRAVIS_JOB_NAME" = "Minimum nightly" ]; then
14-
cargo fmt --all -- --check
15-
cargo clippy --features "$FEATURES num-complex" -- -D warnings
14+
make lint
1615
fi
1716

1817
for example_dir in examples/*; do

src/buffer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ mod test {
706706
assert_eq!(buffer.to_vec::<u8>(py).unwrap(), b"abcde");
707707
}
708708

709+
#[allow(clippy::float_cmp)] // The test wants to ensure that no precision was lost on the Python round-trip
709710
#[test]
710711
fn test_array_buffer() {
711712
let gil = Python::acquire_gil();
@@ -715,7 +716,7 @@ mod test {
715716
.unwrap()
716717
.call_method("array", ("f", (1.0, 1.5, 2.0, 2.5)), None)
717718
.unwrap();
718-
let buffer = PyBuffer::get(py, array.into()).unwrap();
719+
let buffer = PyBuffer::get(py, array).unwrap();
719720
assert_eq!(buffer.dimensions(), 1);
720721
assert_eq!(buffer.item_count(), 4);
721722
assert_eq!(buffer.format().to_str().unwrap(), "f");

src/types/complex.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ mod complex_conversion {
196196
complex_conversion!(f32);
197197
complex_conversion!(f64);
198198

199+
#[allow(clippy::float_cmp)] // The test wants to ensure that no precision was lost on the Python round-trip
199200
#[test]
200201
fn from_complex() {
201202
let gil = Python::acquire_gil();
@@ -230,11 +231,13 @@ mod test {
230231

231232
#[test]
232233
fn test_from_double() {
234+
use assert_approx_eq::assert_approx_eq;
235+
233236
let gil = Python::acquire_gil();
234237
let py = gil.python();
235238
let complex = PyComplex::from_doubles(py, 3.0, 1.2);
236-
assert_eq!(complex.real(), 3.0);
237-
assert_eq!(complex.imag(), 1.2);
239+
assert_approx_eq!(complex.real(), 3.0);
240+
assert_approx_eq!(complex.imag(), 1.2);
238241
}
239242

240243
#[cfg(not(Py_LIMITED_API))]
@@ -281,8 +284,8 @@ mod test {
281284
let l = PyComplex::from_doubles(py, 3.0, 1.2);
282285
let r = PyComplex::from_doubles(py, 1.0, 2.6);
283286
let res = l / r;
284-
assert_approx_eq!(res.real(), 0.7886597938144329);
285-
assert_approx_eq!(res.imag(), -0.8505154639175257);
287+
assert_approx_eq!(res.real(), 0.788_659_793_814_432_9);
288+
assert_approx_eq!(res.imag(), -0.850_515_463_917_525_7);
286289
}
287290

288291
#[cfg(not(Py_LIMITED_API))]
@@ -302,7 +305,7 @@ mod test {
302305
let gil = Python::acquire_gil();
303306
let py = gil.python();
304307
let val = PyComplex::from_doubles(py, 3.0, 1.2);
305-
assert_approx_eq!(val.abs(), 3.2310988842807022);
308+
assert_approx_eq!(val.abs(), 3.231_098_884_280_702_2);
306309
}
307310

308311
#[cfg(not(Py_LIMITED_API))]
@@ -313,7 +316,7 @@ mod test {
313316
let l = PyComplex::from_doubles(py, 3.0, 1.2);
314317
let r = PyComplex::from_doubles(py, 1.2, 2.6);
315318
let val = l.pow(r);
316-
assert_approx_eq!(val.real(), -1.4193099970166037);
317-
assert_approx_eq!(val.imag(), -0.5412974660335446);
319+
assert_approx_eq!(val.real(), -1.419_309_997_016_603_7);
320+
assert_approx_eq!(val.imag(), -0.541_297_466_033_544_6);
318321
}
319322
}

src/types/floatob.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@ mod test {
9191
($func_name:ident, $t1:ty, $t2:ty) => (
9292
#[test]
9393
fn $func_name() {
94+
use assert_approx_eq::assert_approx_eq;
95+
9496
let gil = Python::acquire_gil();
9597
let py = gil.python();
9698
let val = 123 as $t1;
9799
let obj = val.to_object(py);
98-
assert_eq!(obj.extract::<$t2>(py).unwrap(), val as $t2);
100+
assert_approx_eq!(obj.extract::<$t2>(py).unwrap(), val as $t2);
99101
}
100102
)
101103
);
@@ -106,10 +108,12 @@ mod test {
106108

107109
#[test]
108110
fn test_as_double_macro() {
111+
use assert_approx_eq::assert_approx_eq;
112+
109113
let gil = Python::acquire_gil();
110114
let py = gil.python();
111115
let v = 1.23f64;
112116
let obj = v.to_object(py);
113-
assert_eq!(v, unsafe { PyFloat_AS_DOUBLE(obj.as_ptr()) });
117+
assert_approx_eq!(v, unsafe { PyFloat_AS_DOUBLE(obj.as_ptr()) });
114118
}
115119
}

src/types/tuple.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
154154
fn to_object(&self, py: Python) -> PyObject {
155155
unsafe {
156156
let ptr = ffi::PyTuple_New($length);
157-
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.to_object(py).into_ptr());)+;
157+
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.to_object(py).into_ptr());)+
158158
PyObject::from_owned_ptr_or_panic(py, ptr)
159159
}
160160
}
@@ -163,7 +163,7 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
163163
fn into_object(self, py: Python) -> PyObject {
164164
unsafe {
165165
let ptr = ffi::PyTuple_New($length);
166-
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.into_object(py).into_ptr());)+;
166+
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.into_object(py).into_ptr());)+
167167
PyObject::from_owned_ptr_or_panic(py, ptr)
168168
}
169169
}
@@ -173,7 +173,7 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
173173
fn into_py(self, py: Python) -> Py<PyTuple> {
174174
unsafe {
175175
let ptr = ffi::PyTuple_New($length);
176-
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.into_object(py).into_ptr());)+;
176+
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.into_object(py).into_ptr());)+
177177
Py::from_owned_ptr_or_panic(ptr)
178178
}
179179
}

tests/test_datetime.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use pyo3::ffi::*;
44
use pyo3::prelude::*;
55
use pyo3::types::{IntoPyDict, PyAny};
66

7+
#[allow(clippy::trivially_copy_pass_by_ref)]
78
fn _get_subclasses<'p>(
89
py: &'p Python,
910
py_type: &str,
@@ -101,6 +102,7 @@ fn test_delta_check() {
101102

102103
#[test]
103104
fn test_datetime_utc() {
105+
use assert_approx_eq::assert_approx_eq;
104106
use pyo3::types::PyDateTime;
105107

106108
let gil = Python::acquire_gil();
@@ -119,10 +121,11 @@ fn test_datetime_utc() {
119121
.unwrap()
120122
.extract()
121123
.unwrap();
122-
assert_eq!(offset, 0f32);
124+
assert_approx_eq!(offset, 0f32);
123125
}
124126

125-
static INVALID_DATES: &'static [(i32, u8, u8)] = &[
127+
#[cfg(Py_3_6)]
128+
static INVALID_DATES: &[(i32, u8, u8)] = &[
126129
(-1, 1, 1),
127130
(0, 1, 1),
128131
(10000, 1, 1),
@@ -134,7 +137,8 @@ static INVALID_DATES: &'static [(i32, u8, u8)] = &[
134137
(2018, 1, 32),
135138
];
136139

137-
static INVALID_TIMES: &'static [(u8, u8, u8, u32)] =
140+
#[cfg(Py_3_6)]
141+
static INVALID_TIMES: &[(u8, u8, u8, u32)] =
138142
&[(25, 0, 0, 0), (255, 0, 0, 0), (0, 60, 0, 0), (0, 0, 61, 0)];
139143

140144
#[cfg(Py_3_6)]
@@ -145,7 +149,7 @@ fn test_pydate_out_of_bounds() {
145149
// This test is an XFAIL on Python < 3.6 until bounds checking is implemented
146150
let gil = Python::acquire_gil();
147151
let py = gil.python();
148-
for val in INVALID_DATES.into_iter() {
152+
for val in INVALID_DATES {
149153
let (year, month, day) = val;
150154
let dt = PyDate::new(py, *year, *month, *day);
151155
dt.unwrap_err();

tests/test_dict_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fn iter_dict_nosegv() {
1212
let i: u64 = k.extract().unwrap();
1313
sum += i;
1414
}
15-
assert_eq!(sum, 49999995000000);
15+
assert_eq!(sum, 49_999_995_000_000);
1616
}

tests/test_sequence.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ impl PySequenceProtocol for ByteSequence {
4040
fn __getitem__(&self, idx: isize) -> PyResult<u8> {
4141
self.elements
4242
.get(idx as usize)
43-
.map(|&byte| byte)
44-
.ok_or(IndexError::py_err("list index out of range"))
43+
.copied()
44+
.ok_or_else(|| IndexError::py_err("list index out of range"))
4545
}
4646

4747
fn __setitem__(&mut self, idx: isize, value: u8) -> PyResult<()> {

tests/test_various.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn pytuple_pyclass_iter() {
111111
PyRef::new(py, SimplePyClass {}).unwrap(),
112112
PyRef::new(py, SimplePyClass {}).unwrap(),
113113
]
114-
.into_iter(),
114+
.iter(),
115115
);
116116
py_assert!(py, tup, "type(tup[0]).__name__ == 'SimplePyClass'");
117117
py_assert!(py, tup, "type(tup[0]).__name__ == type(tup[0]).__name__");

0 commit comments

Comments
 (0)