Skip to content

Commit

Permalink
Add clippy checks for examples and tests. (#850)
Browse files Browse the repository at this point in the history
* Add clippy checks for examples and tests.

* Fix clippy errors in examples and tests.

* Fix dead code warning for wasm.

* Import only the bare minimum of float-cmp.
  • Loading branch information
xStrom authored Apr 16, 2020
1 parent fb0e8b8 commit cb96b75
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 38 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --manifest-path=druid/Cargo.toml --features=svg,image -- -D warnings
args: --manifest-path=druid/Cargo.toml --all-targets --features=svg,image -- -D warnings

- name: cargo clippy (druid-shell)
uses: actions-rs/cargo@v1
with:
command: clippy
args: --manifest-path=druid-shell/Cargo.toml -- -D warnings
args: --manifest-path=druid-shell/Cargo.toml --all-targets -- -D warnings

- name: cargo clippy (druid-derive)
uses: actions-rs/cargo@v1
with:
command: clippy
args: --manifest-path=druid-derive/Cargo.toml -- -D warnings
args: --manifest-path=druid-derive/Cargo.toml --all-targets -- -D warnings

- name: cargo test (druid)
uses: actions-rs/cargo@v1
Expand Down Expand Up @@ -220,7 +220,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --manifest-path=druid-shell/Cargo.toml --features=x11 -- -D warnings
args: --manifest-path=druid-shell/Cargo.toml --all-targets --features=x11 -- -D warnings

- name: cargo test druid-shell
uses: actions-rs/cargo@v1
Expand Down Expand Up @@ -276,7 +276,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all --target wasm32-unknown-unknown -- -D warnings
args: --all --all-targets --target wasm32-unknown-unknown -- -D warnings

- name: cargo test compile druid-shell (wasm32)
uses: actions-rs/cargo@v1
Expand Down
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions druid-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ proc-macro2 = "1.0.9"

[dev-dependencies]
druid = { path = "../druid", version = "0.6.0" }
float-cmp = { version = "0.6.0", default-features = false }
47 changes: 25 additions & 22 deletions druid-derive/tests/with_lens.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
use float_cmp::approx_eq;

use druid::Data;
use druid::Lens;

#[test]
fn derive_lens() {
#[derive(Lens)]
struct Foo {
struct State {
text: String,
#[lens(name = "lens_number")]
number: f64,
}

let mut foo = Foo {
let mut state = State {
text: "1.0".into(),
number: 1.0,
};

let text_lens = Foo::text;
let number_lens = Foo::lens_number; //named lens for number
let text_lens = State::text;
let number_lens = State::lens_number; //named lens for number

text_lens.with(&foo, |data| assert_eq!(data, "1.0"));
number_lens.with(&foo, |data| assert_eq!(*data, 1.0));
text_lens.with(&state, |data| assert_eq!(data, "1.0"));
number_lens.with(&state, |data| approx_eq!(f64, *data, 1.0));

text_lens.with_mut(&mut foo, |data| *data = "2.0".into());
number_lens.with_mut(&mut foo, |data| *data = 2.0);
text_lens.with_mut(&mut state, |data| *data = "2.0".into());
number_lens.with_mut(&mut state, |data| *data = 2.0);

assert_eq!(foo.text, "2.0");
assert_eq!(foo.number, 2.0);
assert_eq!(state.text, "2.0");
approx_eq!(f64, state.number, 2.0);
}

#[test]
fn mix_with_data_lens() {
#[derive(Clone, Lens, Data)]
struct Foo {
struct State {
#[data(ignore)]
text: String,
#[data(same_fn = "same_sign")]
Expand All @@ -40,29 +42,30 @@ fn mix_with_data_lens() {
}

//test lens
let mut foo = Foo {
let mut state = State {
text: "1.0".into(),
number: 1.0,
};
let text_lens = Foo::text;
let number_lens = Foo::lens_number; //named lens for number
let text_lens = State::text;
let number_lens = State::lens_number; //named lens for number

text_lens.with(&foo, |data| assert_eq!(data, "1.0"));
number_lens.with(&foo, |data| assert_eq!(*data, 1.0));
text_lens.with(&state, |data| assert_eq!(data, "1.0"));
number_lens.with(&state, |data| approx_eq!(f64, *data, 1.0));

text_lens.with_mut(&mut foo, |data| *data = "2.0".into());
number_lens.with_mut(&mut foo, |data| *data = 2.0);
text_lens.with_mut(&mut state, |data| *data = "2.0".into());
number_lens.with_mut(&mut state, |data| *data = 2.0);

assert_eq!(foo.text, "2.0");
assert_eq!(foo.number, 2.0);
assert_eq!(state.text, "2.0");
approx_eq!(f64, state.number, 2.0);

//test data
let two = Foo {
let two = State {
text: "666".into(),
number: 200.0,
};
assert!(foo.same(&two))
assert!(state.same(&two))
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn same_sign(one: &f64, two: &f64) -> bool {
one.signum() == two.signum()
}
2 changes: 1 addition & 1 deletion druid-derive/tests/with_same.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn enums() {
let tritwo = Hi::Tri(-1.);
assert!(!trione.same(&tritwo));
}

#[allow(clippy::trivially_copy_pass_by_ref)]
fn same_sign(one: &f64, two: &f64) -> bool {
one.signum() == two.signum()
}
3 changes: 3 additions & 0 deletions druid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ version = "0.4.0"

[target.'cfg(target_arch="wasm32")'.dependencies]
console_log = "0.1.2"

[dev-dependencies]
float-cmp = { version = "0.6.0", default-features = false }
5 changes: 4 additions & 1 deletion druid/examples/ext_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ pub fn main() {
last_color = new_color.clone();

// if this fails we're shutting down
if let Err(_) = event_sink.submit_command(SET_COLOR, new_color, None) {
if event_sink
.submit_command(SET_COLOR, new_color, None)
.is_err()
{
break;
}
thread::sleep(Duration::from_millis(150));
Expand Down
2 changes: 1 addition & 1 deletion druid/examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Widget<u32> for SimpleBox {
fn event(&mut self, _ctx: &mut EventCtx, _event: &Event, _data: &mut u32, _env: &Env) {}

fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, _data: &u32, _env: &Env) {
if matches!(event, LifeCycle::HotChanged(_)) {
if let LifeCycle::HotChanged(_) = event {
ctx.request_paint();
}
}
Expand Down
1 change: 1 addition & 0 deletions druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ impl BaseState {
}

#[cfg(test)]
#[allow(dead_code)]
pub(crate) fn layout_rect(&self) -> Rect {
self.layout_rect.unwrap_or_default()
}
Expand Down
10 changes: 5 additions & 5 deletions druid/src/localization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,18 +411,18 @@ mod tests {
vec![en_ca.clone(), en_us.clone(), en_gb.clone()]
);
assert_eq!(
resmgr.resolve_locales(en_za.clone()),
vec![en_gb.clone(), en_us.clone(), en_ca.clone()]
resmgr.resolve_locales(en_za),
vec![en_gb, en_us.clone(), en_ca]
);
assert_eq!(
resmgr.resolve_locales(fr_ca.clone()),
resmgr.resolve_locales(fr_ca),
vec![fr_fr.clone(), en_us.clone()]
);
assert_eq!(
resmgr.resolve_locales(fr_fr.clone()),
vec![fr_fr.clone(), en_us.clone()]
vec![fr_fr, en_us.clone()]
);
assert_eq!(resmgr.resolve_locales(cn_hk), vec![en_us.clone()]);
assert_eq!(resmgr.resolve_locales(pt_pt), vec![en_us.clone()]);
assert_eq!(resmgr.resolve_locales(pt_pt), vec![en_us]);
}
}
2 changes: 1 addition & 1 deletion druid/src/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl<S, T: Data> Widget<T> for ModularWidget<S, T> {
layout
.as_mut()
.map(|f| f(state, ctx, bc, data, env))
.unwrap_or(Size::new(100., 100.))
.unwrap_or_else(|| Size::new(100., 100.))
}

fn paint(&mut self, ctx: &mut PaintCtx, data: &T, env: &Env) {
Expand Down
5 changes: 4 additions & 1 deletion druid/src/tests/layout_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

//! Tests related to layout.
use float_cmp::approx_eq;

use super::*;

#[test]
Expand All @@ -33,7 +35,8 @@ fn simple_layout() {
harness.send_initial_events();
harness.just_layout();
let state = harness.get_state(id_1);
assert_eq!(
approx_eq!(
f64,
state.layout_rect().x0,
((DEFAULT_SIZE.width - BOX_WIDTH) / 2.) - PADDING
);
Expand Down
1 change: 1 addition & 0 deletions druid/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn propogate_hot() {
button: MouseButton::Left,
}
}
#[allow(clippy::cognitive_complexity)]
Harness::create((), widget, |harness| {
harness.send_initial_events();
harness.just_layout();
Expand Down
2 changes: 1 addition & 1 deletion druid/src/text/editable_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ mod tests {
fn replace() {
let mut a = String::from("hello world");
a.edit(1..9, "era");
assert_eq!("herald", String::from(a));
assert_eq!("herald", a);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions druid/src/widget/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ mod tests {
use super::*;

#[test]
#[allow(clippy::cognitive_complexity)]
fn test_main_axis_alignment_spacing() {
// The following alignment strategy is based on how
// Chrome 80 handles it with CSS flex.
Expand Down

0 comments on commit cb96b75

Please sign in to comment.