From df9fe91a138912a725e1a3131b88cc82a19dc6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=B3r=20Mil=C3=A1n?= <71042542+bormilan@users.noreply.github.com> Date: Thu, 31 Oct 2024 21:31:39 +0100 Subject: [PATCH] Closes #311 - fix no_match_in_condition reports a warning even when the condition includes a valid match (#368) --- src/elvis_style.erl | 3 ++- test/examples/pass_no_match_in_condition2.erl | 14 ++++++++++++++ test/style_SUITE.erl | 3 ++- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/examples/pass_no_match_in_condition2.erl diff --git a/src/elvis_style.erl b/src/elvis_style.erl index 456c899..efa6e19 100644 --- a/src/elvis_style.erl +++ b/src/elvis_style.erl @@ -1302,7 +1302,8 @@ no_match_in_condition(Config, Target, RuleConfig) -> CaseNodes). is_match_in_condition(Node) -> - ktn_code:type(Node) == case_expr andalso [] =/= elvis_code:find(fun is_match/1, Node). + (ktn_code:type(Node) == case_expr orelse ktn_code:type(Node) == block) + andalso lists:any(fun is_match/1, ktn_code:content(Node)). is_match(Node) -> ktn_code:type(Node) == match orelse ktn_code:type(Node) == maybe_match. diff --git a/test/examples/pass_no_match_in_condition2.erl b/test/examples/pass_no_match_in_condition2.erl new file mode 100644 index 0000000..af69b3d --- /dev/null +++ b/test/examples/pass_no_match_in_condition2.erl @@ -0,0 +1,14 @@ +-module(pass_no_match_in_condition2). + +-export([valid/1]). + +valid(List) -> + case lists:all(fun ({K, V}) -> + Something = find:something(for, K), + check:something(V, Something) + end, + List) + of + true -> all_true; + false -> found_a_bad_one + end. diff --git a/test/style_SUITE.erl b/test/style_SUITE.erl index 5464966..d6aa612 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -1638,9 +1638,10 @@ verify_no_match_in_condition(Config) -> PassPath = "pass_no_match_in_condition." ++ Ext, [] = elvis_core_apply_rule(Config, elvis_style, no_match_in_condition, #{}, PassPath), + PassPath2 = "pass_no_match_in_condition2." ++ Ext, + [] = elvis_core_apply_rule(Config, elvis_style, no_match_in_condition, #{}, PassPath2), FailPath = "fail_no_match_in_condition." ++ Ext, - R = elvis_core_apply_rule(Config, elvis_style, no_match_in_condition, #{}, FailPath), case Group of beam_files ->