Skip to content

Commit

Permalink
Some Clippy lints (#642)
Browse files Browse the repository at this point in the history
* some initial clippy fixes

* some clippy lints cleaned

* one more

* more lints

* even more

* more lints

* all clippy lints solved or ignored

* test in travis

* revert from_iter changes

* add temporary allow to from_iter lint

* install clippy in travis

* only run clippy on nightly

* cloned rather than copied

* miscreant line

* collapse non-looping loop

* Derive traits for Axis instead of manual impl

The behavior should be identical.

* Remove manual impls of Send and Sync for IxDynImpl

`Send` and `Sync` are automatically implemented, so manual
implementations aren't necessary.

* Implement Hash for IxDynRepr and IxDynImpl

* Derive Hash for Dim instead of manual impl

This avoids any issues associated with deriving `PartialEq` but
manually implementing `Hash`.

* Simplify remove_axis using copy_from_slice

* Implement TrustedIterator for Cloned

* Clarify docs for TrustedIterator

* Reformat match in slice_collapse

* Remove unnecessary .clone() call

* Reformat values of CORDER and FORDER

* explicit folds

* more minor changes

* replace notes with reference to @jturner314 comment

* more small tweaks

* flup re len & empty

* clippy on beta only since sometimes not available on nightly (e.g. today)

* only attempt clippy install on beta

* srlsy?

* bash precedence

* set aside one lint for the moment
  • Loading branch information
max-sixty authored and LukeMathWalker committed Jul 11, 2019
1 parent 715ee64 commit 98bf3b6
Show file tree
Hide file tree
Showing 59 changed files with 326 additions and 201 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ matrix:
- rust: beta
env:
- FEATURES='test docs'
- CHANNEL='beta'
- rust: nightly
env:
- FEATURES='test docs'
- IS_NIGHTLY=1
- CHANNEL='nightly'
env:
global:
- HOST=x86_64-unknown-linux-gnu
Expand All @@ -30,5 +31,5 @@ before_script:
- rustup component add rustfmt
script:
- |
cargo fmt --all -- --check &&
./scripts/all-tests.sh "$FEATURES" "$IS_NIGHTLY"
cargo fmt --all -- --check &&
./scripts/all-tests.sh "$FEATURES" "$CHANNEL"
15 changes: 12 additions & 3 deletions benches/bench1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#![feature(test)]
#![allow(unused_imports)]
#![allow(
clippy::many_single_char_names,
clippy::deref_addrof,
clippy::unreadable_literal,
clippy::many_single_char_names
)]

extern crate ndarray;
extern crate test;
Expand Down Expand Up @@ -304,6 +310,7 @@ fn add_2d_zip_cutout(bench: &mut test::Bencher) {
}

#[bench]
#[allow(clippy::identity_op)]
fn add_2d_cutouts_by_4(bench: &mut test::Bencher) {
let mut a = Array::<i32, _>::zeros((64 * 1, 64 * 1));
let b = Array::<i32, _>::zeros((64 * 1, 64 * 1));
Expand All @@ -316,6 +323,7 @@ fn add_2d_cutouts_by_4(bench: &mut test::Bencher) {
}

#[bench]
#[allow(clippy::identity_op)]
fn add_2d_cutouts_by_16(bench: &mut test::Bencher) {
let mut a = Array::<i32, _>::zeros((64 * 1, 64 * 1));
let b = Array::<i32, _>::zeros((64 * 1, 64 * 1));
Expand All @@ -328,6 +336,7 @@ fn add_2d_cutouts_by_16(bench: &mut test::Bencher) {
}

#[bench]
#[allow(clippy::identity_op)]
fn add_2d_cutouts_by_32(bench: &mut test::Bencher) {
let mut a = Array::<i32, _>::zeros((64 * 1, 64 * 1));
let b = Array::<i32, _>::zeros((64 * 1, 64 * 1));
Expand Down Expand Up @@ -580,7 +589,7 @@ fn iadd_scalar_2d_strided_dyn(bench: &mut test::Bencher) {
fn scaled_add_2d_f32_regular(bench: &mut test::Bencher) {
let mut av = Array::<f32, _>::zeros((ADD2DSZ, ADD2DSZ));
let bv = Array::<f32, _>::zeros((ADD2DSZ, ADD2DSZ));
let scalar = 3.1415926535;
let scalar = std::f32::consts::PI;
bench.iter(|| {
av.scaled_add(scalar, &bv);
});
Expand Down Expand Up @@ -650,7 +659,7 @@ fn bench_row_iter(bench: &mut test::Bencher) {
let a = Array::<f32, _>::zeros((1024, 1024));
let it = a.row(17);
bench.iter(|| {
for elt in it.clone() {
for elt in it {
black_box(elt);
}
})
Expand All @@ -661,7 +670,7 @@ fn bench_col_iter(bench: &mut test::Bencher) {
let a = Array::<f32, _>::zeros((1024, 1024));
let it = a.column(17);
bench.iter(|| {
for elt in it.clone() {
for elt in it {
black_box(elt);
}
})
Expand Down
1 change: 1 addition & 0 deletions benches/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn chunk2x2_sum_uget1(bench: &mut Bencher) {
}

#[bench]
#[allow(clippy::identity_op)]
fn chunk2x2_sum_get2(bench: &mut Bencher) {
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
Expand Down
7 changes: 6 additions & 1 deletion benches/construct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#![feature(test)]

#![allow(
clippy::many_single_char_names,
clippy::deref_addrof,
clippy::unreadable_literal,
clippy::many_single_char_names
)]
extern crate test;
use test::Bencher;

Expand Down
6 changes: 6 additions & 0 deletions benches/gemv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#![feature(test)]
#![allow(
clippy::many_single_char_names,
clippy::deref_addrof,
clippy::unreadable_literal,
clippy::many_single_char_names
)]

extern crate test;
use test::Bencher;
Expand Down
11 changes: 8 additions & 3 deletions benches/higher-order.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#![feature(test)]

#![allow(
clippy::many_single_char_names,
clippy::deref_addrof,
clippy::unreadable_literal,
clippy::many_single_char_names
)]
extern crate test;
use test::black_box;
use test::Bencher;
Expand Down Expand Up @@ -67,13 +72,13 @@ fn map_axis_0(bench: &mut Bencher) {
let a = Array::from_iter(0..MASZ as i32)
.into_shape([MA, MA])
.unwrap();
bench.iter(|| a.map_axis(Axis(0), |lane| black_box(lane)));
bench.iter(|| a.map_axis(Axis(0), black_box));
}

#[bench]
fn map_axis_1(bench: &mut Bencher) {
let a = Array::from_iter(0..MASZ as i32)
.into_shape([MA, MA])
.unwrap();
bench.iter(|| a.map_axis(Axis(1), |lane| black_box(lane)));
bench.iter(|| a.map_axis(Axis(1), black_box));
}
24 changes: 15 additions & 9 deletions benches/iter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#![feature(test)]
#![allow(
clippy::many_single_char_names,
clippy::deref_addrof,
clippy::unreadable_literal,
clippy::many_single_char_names
)]

extern crate rawpointer;
extern crate test;
Expand All @@ -14,15 +20,15 @@ use ndarray::{FoldWhile, Zip};
#[bench]
fn iter_sum_2d_regular(bench: &mut Bencher) {
let a = Array::<i32, _>::zeros((64, 64));
bench.iter(|| a.iter().fold(0, |acc, &x| acc + x));
bench.iter(|| a.iter().sum::<i32>());
}

#[bench]
fn iter_sum_2d_cutout(bench: &mut Bencher) {
let a = Array::<i32, _>::zeros((66, 66));
let av = a.slice(s![1..-1, 1..-1]);
let a = av;
bench.iter(|| a.iter().fold(0, |acc, &x| acc + x));
bench.iter(|| a.iter().sum::<i32>());
}

#[bench]
Expand All @@ -37,37 +43,37 @@ fn iter_all_2d_cutout(bench: &mut Bencher) {
fn iter_sum_2d_transpose(bench: &mut Bencher) {
let a = Array::<i32, _>::zeros((66, 66));
let a = a.t();
bench.iter(|| a.iter().fold(0, |acc, &x| acc + x));
bench.iter(|| a.iter().sum::<i32>());
}

#[bench]
fn iter_filter_sum_2d_u32(bench: &mut Bencher) {
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
let b = a.mapv(|x| (x * 100.) as u32);
bench.iter(|| b.iter().filter(|&&x| x < 75).fold(0, |acc, &x| acc + x));
bench.iter(|| b.iter().filter(|&&x| x < 75).sum::<u32>());
}

#[bench]
fn iter_filter_sum_2d_f32(bench: &mut Bencher) {
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
let b = a * 100.;
bench.iter(|| b.iter().filter(|&&x| x < 75.).fold(0., |acc, &x| acc + x));
bench.iter(|| b.iter().filter(|&&x| x < 75.).sum::<f32>());
}

#[bench]
fn iter_filter_sum_2d_stride_u32(bench: &mut Bencher) {
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
let b = a.mapv(|x| (x * 100.) as u32);
let b = b.slice(s![.., ..;2]);
bench.iter(|| b.iter().filter(|&&x| x < 75).fold(0, |acc, &x| acc + x));
bench.iter(|| b.iter().filter(|&&x| x < 75).sum::<u32>());
}

#[bench]
fn iter_filter_sum_2d_stride_f32(bench: &mut Bencher) {
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
let b = a * 100.;
let b = b.slice(s![.., ..;2]);
bench.iter(|| b.iter().filter(|&&x| x < 75.).fold(0., |acc, &x| acc + x));
bench.iter(|| b.iter().filter(|&&x| x < 75.).sum::<f32>());
}

const ZIPSZ: usize = 10_000;
Expand Down Expand Up @@ -190,7 +196,7 @@ fn vector_sum_3_zip_unchecked_manual(bench: &mut Bencher) {
let mut ap = a.as_ptr();
let mut bp = b.as_ptr();
let mut cp = c.as_mut_ptr();
let cend = cp.offset(c.len() as isize);
let cend = cp.add(c.len());
while cp != cend {
*cp.post_inc() += *ap.post_inc() + *bp.post_inc();
}
Expand Down Expand Up @@ -310,7 +316,7 @@ fn indexed_iter_3d_dyn(bench: &mut Bencher) {
fn iter_sum_1d_strided_fold(bench: &mut Bencher) {
let mut a = Array::<u64, _>::ones(10240);
a.slice_axis_inplace(Axis(0), Slice::new(0, None, 2));
bench.iter(|| a.iter().fold(0, |acc, &x| acc + x));
bench.iter(|| a.iter().sum::<u64>());
}

#[bench]
Expand Down
6 changes: 6 additions & 0 deletions examples/axis_ops.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#![allow(
clippy::many_single_char_names,
clippy::deref_addrof,
clippy::unreadable_literal,
clippy::many_single_char_names
)]
extern crate ndarray;

use ndarray::prelude::*;
Expand Down
6 changes: 6 additions & 0 deletions examples/bounds_check_elim.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#![crate_type = "lib"]
#![allow(
clippy::many_single_char_names,
clippy::deref_addrof,
clippy::unreadable_literal,
clippy::many_single_char_names
)]

// Test cases for bounds check elimination

Expand Down
1 change: 1 addition & 0 deletions examples/convo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ where
for i in 0..n - 2 {
for j in 0..m - 2 {
let mut conv = F::zero();
#[allow(clippy::needless_range_loop)]
for k in 0..3 {
for l in 0..3 {
conv = conv + *a.uget((i + k, j + l)) * kernel[k][l];
Expand Down
10 changes: 8 additions & 2 deletions examples/life.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#![allow(
clippy::many_single_char_names,
clippy::deref_addrof,
clippy::unreadable_literal,
clippy::many_single_char_names
)]
extern crate ndarray;

use ndarray::prelude::*;

const INPUT: &'static [u8] = include_bytes!("life.txt");
const INPUT: &[u8] = include_bytes!("life.txt");
//const INPUT: &'static [u8] = include_bytes!("lifelite.txt");

const N: usize = 100;
Expand Down Expand Up @@ -71,7 +77,7 @@ fn render(a: &Board) {
print!(".");
}
}
println!("");
println!();
}
}

Expand Down
6 changes: 6 additions & 0 deletions examples/zip_many.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#![allow(
clippy::many_single_char_names,
clippy::deref_addrof,
clippy::unreadable_literal,
clippy::many_single_char_names
)]
extern crate ndarray;

use ndarray::prelude::*;
Expand Down
5 changes: 3 additions & 2 deletions scripts/all-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -x
set -e

FEATURES=$1
IS_NIGHTLY=$2
CHANNEL=$2

cargo build --verbose --no-default-features
cargo test --verbose --no-default-features
Expand All @@ -14,4 +14,5 @@ cargo test --verbose --features "$FEATURES"
cargo test --manifest-path=serialization-tests/Cargo.toml --verbose
cargo test --manifest-path=blas-tests/Cargo.toml --verbose
CARGO_TARGET_DIR=target/ cargo test --manifest-path=numeric-tests/Cargo.toml --verbose
([ "$IS_NIGHTLY" != 1 ] || cargo bench --no-run --verbose --features "$FEATURES")
([ "$CHANNEL" != "beta" ] || (rustup component add clippy && cargo clippy))
([ "$CHANNEL" != "nightly" ] || cargo bench --no-run --verbose --features "$FEATURES")
2 changes: 1 addition & 1 deletion src/array_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<S, Di> ArrayVisitor<S, Di> {
}
}

static ARRAY_FIELDS: &'static [&'static str] = &["v", "dim", "data"];
static ARRAY_FIELDS: &[&str] = &["v", "dim", "data"];

/// **Requires crate feature `"serde-1"`**
impl<'de, A, Di, S> Deserialize<'de> for ArrayBase<S, Di>
Expand Down
9 changes: 3 additions & 6 deletions src/arrayformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,11 @@ enum PrintableCell {
// where indexes are being omitted.
fn to_be_printed(length: usize, limit: usize) -> Vec<PrintableCell> {
if length <= 2 * limit {
(0..length)
.map(|x| PrintableCell::ElementIndex(x))
.collect()
(0..length).map(PrintableCell::ElementIndex).collect()
} else {
let mut v: Vec<PrintableCell> =
(0..limit).map(|x| PrintableCell::ElementIndex(x)).collect();
let mut v: Vec<PrintableCell> = (0..limit).map(PrintableCell::ElementIndex).collect();
v.push(PrintableCell::Ellipses);
v.extend((length - limit..length).map(|x| PrintableCell::ElementIndex(x)));
v.extend((length - limit..length).map(PrintableCell::ElementIndex));
v
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub unsafe trait DataMut: Data + RawDataMut {
/// accessed with safe code.
///
/// ***Internal trait, see `Data`.***
#[deprecated(note = "use `Data + RawDataClone` instead", since = "0.13")]
#[deprecated(note = "use `Data + RawDataClone` instead", since = "0.13.0")]
pub trait DataClone: Data + RawDataClone {}

#[allow(deprecated)]
Expand Down Expand Up @@ -241,7 +241,7 @@ unsafe impl<A> Data for OwnedArcRepr<A> {
Self::ensure_unique(&mut self_);
let data = OwnedRepr(Arc::try_unwrap(self_.data.0).ok().unwrap());
ArrayBase {
data: data,
data,
ptr: self_.ptr,
dim: self_.dim,
strides: self_.strides,
Expand Down
5 changes: 4 additions & 1 deletion src/dimension/axes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ where
{
Axes {
dim: d,
strides: strides,
strides,
start: 0,
end: d.ndim(),
}
Expand Down Expand Up @@ -46,6 +46,9 @@ pub struct AxisDescription(pub Axis, pub Ix, pub Ixs);

copy_and_clone!(AxisDescription);

// AxisDescription can't really be empty
// https://github.com/rust-ndarray/ndarray/pull/642#discussion_r296051702
#[allow(clippy::len_without_is_empty)]
impl AxisDescription {
/// Return axis
#[inline(always)]
Expand Down
Loading

0 comments on commit 98bf3b6

Please sign in to comment.