From 22ec7cbec11aa691aaa5efaffdf21316892e4966 Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Mon, 11 Nov 2024 14:40:30 +0100 Subject: [PATCH] Fix false positive given arity mismatch in `inconsistent-args` (#1252) While a mismatched arg is technically still a violation, the arity mismatch should probably be dealt with first, so let's have the compiler handle that. Fixes #1250 Signed-off-by: Anders Eknert --- .../bugs/inconsistent-args/inconsistent_args.rego | 9 +++++++++ .../inconsistent-args/inconsistent_args_test.rego | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/bundle/regal/rules/bugs/inconsistent-args/inconsistent_args.rego b/bundle/regal/rules/bugs/inconsistent-args/inconsistent_args.rego index 5102a3dc..9c9f1796 100644 --- a/bundle/regal/rules/bugs/inconsistent-args/inconsistent_args.rego +++ b/bundle/regal/rules/bugs/inconsistent-args/inconsistent_args.rego @@ -24,6 +24,9 @@ report contains violation if { some name, args_list in function_args_by_name + # leave that to the compiler + not _arity_mismatch(args_list) + # "Partition" the args by their position by_position := [s | some i, _ in args_list[0] @@ -39,6 +42,12 @@ report contains violation if { violation := result.fail(rego.metadata.chain(), result.ranged_location_between(args[0], regal.last(args))) } +_arity_mismatch(args_list) if { + len := count(args_list[0]) + some arr in args_list + count(arr) != len +} + _inconsistent_args(position) if { named_vars := {arg.value | some arg in position diff --git a/bundle/regal/rules/bugs/inconsistent-args/inconsistent_args_test.rego b/bundle/regal/rules/bugs/inconsistent-args/inconsistent_args_test.rego index 999d4f59..00f05635 100644 --- a/bundle/regal/rules/bugs/inconsistent-args/inconsistent_args_test.rego +++ b/bundle/regal/rules/bugs/inconsistent-args/inconsistent_args_test.rego @@ -83,6 +83,19 @@ test_success_using_pattern_matching if { r == set() } +# this is a compiler error, so let's not flag it here +# see https://github.com/StyraInc/regal/issues/1250 +test_success_incorrect_arity if { + module := ast.with_rego_v1(` + foo(a, b) if a == b + foo(a, b, c) if a > b > c + foo(b, a) if a == b + `) + r := rule.report with input as module + + r == set() +} + expected := { "category": "bugs", "description": "Inconsistently named function arguments",