From 5093fe4614cab7e7594e4afeaa20162986b23bfd Mon Sep 17 00:00:00 2001 From: Akos Hajdu Date: Tue, 6 Jul 2021 06:05:30 -0700 Subject: [PATCH] [erl-frontend] Add tests for non-exhaistive case expressions Summary: Add some test cases to potentially non-exhaustive case clauses. Reviewed By: rgrig Differential Revision: D29555525 fbshipit-source-id: f710e93e6 --- .../codetoanalyze/erlang/nonmatch/issues.exp | 6 +++- .../erlang/nonmatch/src/nonmatch.erl | 33 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/infer/tests/codetoanalyze/erlang/nonmatch/issues.exp b/infer/tests/codetoanalyze/erlang/nonmatch/issues.exp index 0190973b2..b44bc2b53 100644 --- a/infer/tests/codetoanalyze/erlang/nonmatch/issues.exp +++ b/infer/tests/codetoanalyze/erlang/nonmatch/issues.exp @@ -1,5 +1,8 @@ codetoanalyze/erlang/nonmatch/src/nonmatch.erl, assert_empty/1, 0, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [*** LATENT ***,no pattern match here] codetoanalyze/erlang/nonmatch/src/nonmatch.erl, assert_second_is_nil/1, 0, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [*** LATENT ***,no pattern match here] +codetoanalyze/erlang/nonmatch/src/nonmatch.erl, case_simple/1, 1, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [*** LATENT ***,no pattern match here] +codetoanalyze/erlang/nonmatch/src/nonmatch.erl, case_test_simple3_Bad/0, -14, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `case_simple/1`,no pattern match here] +codetoanalyze/erlang/nonmatch/src/nonmatch.erl, case_test_tail3_Bad/0, -15, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `tail_with_case/1`,no pattern match here] codetoanalyze/erlang/nonmatch/src/nonmatch.erl, fp_list_match_test_secondnil1_Ok/0, -17, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `assert_second_is_nil/1`,no pattern match here] codetoanalyze/erlang/nonmatch/src/nonmatch.erl, fp_list_match_test_secondnil2_Ok/0, -20, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `assert_second_is_nil/1`,no pattern match here] codetoanalyze/erlang/nonmatch/src/nonmatch.erl, fp_match_test_c_Ok/0, 1, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [no pattern match here] @@ -11,6 +14,7 @@ codetoanalyze/erlang/nonmatch/src/nonmatch.erl, list_match_test_tail3_Bad/0, -8, codetoanalyze/erlang/nonmatch/src/nonmatch.erl, match_test_b_Bad/0, 1, CONSTANT_ADDRESS_DEREFERENCE, no_bucket, WARNING, [in call to `two/0`,is the constant 2,assigned,returned,return from call to `two/0`,invalid access occurs here] codetoanalyze/erlang/nonmatch/src/nonmatch.erl, match_test_b_Bad/0, 1, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [no pattern match here] codetoanalyze/erlang/nonmatch/src/nonmatch.erl, match_test_e_Bad/0, -42, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `tail/1`,no pattern match here] -codetoanalyze/erlang/nonmatch/src/nonmatch.erl, match_test_g_Bad/0, 7, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `only_accepts_one/1`,no pattern match here] +codetoanalyze/erlang/nonmatch/src/nonmatch.erl, match_test_g_Bad/0, 32, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `only_accepts_one/1`,no pattern match here] codetoanalyze/erlang/nonmatch/src/nonmatch.erl, only_accepts_one/1, 0, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [*** LATENT ***,no pattern match here] codetoanalyze/erlang/nonmatch/src/nonmatch.erl, tail/1, 0, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [*** LATENT ***,no pattern match here] +codetoanalyze/erlang/nonmatch/src/nonmatch.erl, tail_with_case/1, 1, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [*** LATENT ***,no pattern match here] diff --git a/infer/tests/codetoanalyze/erlang/nonmatch/src/nonmatch.erl b/infer/tests/codetoanalyze/erlang/nonmatch/src/nonmatch.erl index 54ede283c..98f3b82c0 100644 --- a/infer/tests/codetoanalyze/erlang/nonmatch/src/nonmatch.erl +++ b/infer/tests/codetoanalyze/erlang/nonmatch/src/nonmatch.erl @@ -21,7 +21,13 @@ fp_match_test_d_Ok/0, match_test_e_Bad/0, match_test_f_Ok/0, - match_test_g_Bad/0 + match_test_g_Bad/0, + case_test_simple1_Ok/0, + case_test_simple2_Ok/0, + case_test_simple3_Bad/0, + case_test_tail1_Ok/0, + case_test_tail2_Ok/0, + case_test_tail3_Bad/0 ]). tail([_ | Xs]) -> Xs. @@ -79,6 +85,31 @@ match_test_g_Bad() -> X = 2, only_accepts_one(X). +case_simple(X) -> + case X of + 0 -> zero; + 1 -> one + end. + +tail_with_case(X) -> + case X of + [_|T] -> T + end. + +case_test_simple1_Ok() -> + case_simple(0). +case_test_simple2_Ok() -> + case_simple(1). +case_test_simple3_Bad() -> + case_simple(2). + +case_test_tail1_Ok() -> + tail_with_case([1, 2]). +case_test_tail2_Ok() -> + tail_with_case([1]). +case_test_tail3_Bad() -> + tail_with_case([]). + %% internal %% These functions are used to fool the compiler, which would warn if these were inlined.