From c7874ec26d6499a1196ce23432beb33764490dec Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 20 Feb 2022 10:55:20 +0530 Subject: new lint: useless_has_attr --- bin/tests/data/useless_has_attr.nix | 9 +++++++ bin/tests/main.rs | 3 ++- bin/tests/snapshots/main__useless_has_attr.snap | 34 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 bin/tests/data/useless_has_attr.nix create mode 100644 bin/tests/snapshots/main__useless_has_attr.snap (limited to 'bin/tests') diff --git a/bin/tests/data/useless_has_attr.nix b/bin/tests/data/useless_has_attr.nix new file mode 100644 index 0000000..f4c9fd5 --- /dev/null +++ b/bin/tests/data/useless_has_attr.nix @@ -0,0 +1,9 @@ +[ + # trivial + (if x ? a then x.a else default) + (if x.a ? b then x.a.b else default) + (if x ? a.b then x.a.b else default) + + # complex body + (if x ? a then x.a else if b then c else d) +] diff --git a/bin/tests/main.rs b/bin/tests/main.rs index 3403f65..3425cfa 100644 --- a/bin/tests/main.rs +++ b/bin/tests/main.rs @@ -64,5 +64,6 @@ test_lint! { faster_groupby => session_info!("2.5"), faster_zipattrswith => session_info!("2.6"), deprecated_to_path => session_info!("2.4"), - bool_simplification + bool_simplification, + useless_has_attr } diff --git a/bin/tests/snapshots/main__useless_has_attr.snap b/bin/tests/snapshots/main__useless_has_attr.snap new file mode 100644 index 0000000..df72eaf --- /dev/null +++ b/bin/tests/snapshots/main__useless_has_attr.snap @@ -0,0 +1,34 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W19] Warning: This `if` expression can be simplified with `or` + ╭─[data/useless_has_attr.nix:3:4] + │ + 3 │ (if x ? a then x.a else default) + · ───────────────┬────────────── + · ╰──────────────── Consider using x.a or default instead of this if expression +───╯ +[W19] Warning: This `if` expression can be simplified with `or` + ╭─[data/useless_has_attr.nix:4:4] + │ + 4 │ (if x.a ? b then x.a.b else default) + · ─────────────────┬──────────────── + · ╰────────────────── Consider using x.a.b or default instead of this if expression +───╯ +[W19] Warning: This `if` expression can be simplified with `or` + ╭─[data/useless_has_attr.nix:5:4] + │ + 5 │ (if x ? a.b then x.a.b else default) + · ─────────────────┬──────────────── + · ╰────────────────── Consider using x.a.b or default instead of this if expression +───╯ +[W19] Warning: This `if` expression can be simplified with `or` + ╭─[data/useless_has_attr.nix:8:4] + │ + 8 │ (if x ? a then x.a else if b then c else d) + · ────────────────────┬──────────────────── + · ╰────────────────────── Consider using x.a or (if b then c else d) instead of this if expression +───╯ + -- cgit v1.2.3