From c917670c9193d080fba7e8f5167f0f29752154fa Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 20 Feb 2022 11:29:59 +0530 Subject: useless_has_attr: do not parenthesize select expressions in fixes `select` expresisons are more tightly binding than `or` expressions, we do not have to parenthesize them when placed next to an `or`. --- bin/tests/data/useless_has_attr.nix | 1 + bin/tests/snapshots/main__useless_has_attr.snap | 7 +++++++ lib/src/lints/useless_has_attr.rs | 3 ++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bin/tests/data/useless_has_attr.nix b/bin/tests/data/useless_has_attr.nix index f4c9fd5..10966ac 100644 --- a/bin/tests/data/useless_has_attr.nix +++ b/bin/tests/data/useless_has_attr.nix @@ -6,4 +6,5 @@ # complex body (if x ? a then x.a else if b then c else d) + (if x ? a then x.a else b.c) ] diff --git a/bin/tests/snapshots/main__useless_has_attr.snap b/bin/tests/snapshots/main__useless_has_attr.snap index df72eaf..d9b5ac8 100644 --- a/bin/tests/snapshots/main__useless_has_attr.snap +++ b/bin/tests/snapshots/main__useless_has_attr.snap @@ -31,4 +31,11 @@ expression: "&out" · ────────────────────┬──────────────────── · ╰────────────────────── Consider using x.a or (if b then c else d) instead of this if expression ───╯ +[W19] Warning: This `if` expression can be simplified with `or` + ╭─[data/useless_has_attr.nix:9:4] + │ + 9 │ (if x ? a then x.a else b.c) + · ─────────────┬──────────── + · ╰────────────── Consider using x.a or b.c instead of this if expression +───╯ diff --git a/lib/src/lints/useless_has_attr.rs b/lib/src/lints/useless_has_attr.rs index aae560a..2dbdb5b 100644 --- a/lib/src/lints/useless_has_attr.rs +++ b/lib/src/lints/useless_has_attr.rs @@ -63,7 +63,8 @@ impl Rule for UselessHasAttr { | SyntaxKind::NODE_PAREN | SyntaxKind::NODE_STRING | SyntaxKind::NODE_ATTR_SET - | SyntaxKind::NODE_IDENT => default_expr, + | SyntaxKind::NODE_IDENT + | SyntaxKind::NODE_SELECT => default_expr, _ => make::parenthesize(&default_expr).node().clone(), }; let replacement = make::or_default(&set, &attr_path, &default_with_parens).node().clone(); -- cgit v1.2.3