From c69241fbbcef0b748cb3fd48d08c306929b45f72 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 31 Oct 2023 13:02:33 +0100 Subject: [PATCH 1/3] test: Showcase warning detection false positives --- .../bin/mdx-test/expect/warnings/test-case.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/bin/mdx-test/expect/warnings/test-case.md b/test/bin/mdx-test/expect/warnings/test-case.md index 3d5e190ff..ac23368d0 100644 --- a/test/bin/mdx-test/expect/warnings/test-case.md +++ b/test/bin/mdx-test/expect/warnings/test-case.md @@ -29,3 +29,36 @@ Warning 9 [missing-record-field-pattern]: the following labels are not bound in y Either bind these labels explicitly or add '; _' to the pattern. ``` + +Test against some false positives: + +```ocaml +let x = [ "Warning" ] +``` +```mdx-error +val x : string list = ["Warning"] +``` + +```ocaml +module Warning = struct + type t = Warning +end + +let warning = Warning.Warning +``` +```mdx-error +module Warning : sig type t = Warning end +val warning : Warning.t = Warning.Warning +``` + +Intended false positive: + +```ocaml +let x = + if true then + prerr_endline "Warning: Assert failed"; + [ "foo" ] +``` +```mdx-error +Warning: Assert failed +``` From d906c679a5dd066b35a3809ac23fed66f12a2f32 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 31 Oct 2023 13:04:45 +0100 Subject: [PATCH 2/3] More precise warning detection Reduce false-positive while detecting warnings by searching for the word "Warning" at the beginning of a line only. --- lib/test/mdx_test.ml | 4 +++- test/bin/mdx-test/expect/warnings/test-case.md | 7 ------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/test/mdx_test.ml b/lib/test/mdx_test.ml index 08c5f6b01..bbed0cd32 100644 --- a/lib/test/mdx_test.ml +++ b/lib/test/mdx_test.ml @@ -203,9 +203,11 @@ let rec error_padding = function let xs = error_padding xs in x :: xs +let contains_warnings l = + String.is_prefix ~affix:"Warning" l || String.is_infix ~affix:"\nWarning" l + let eval_ocaml ~(block : Block.t) ?syntax ?root c ppf errors = let cmd = block.contents |> remove_padding in - let contains_warnings = String.is_infix ~affix:"Warning" in let error_lines = match eval_test ?root ~block c cmd with | Ok lines -> List.filter contains_warnings lines diff --git a/test/bin/mdx-test/expect/warnings/test-case.md b/test/bin/mdx-test/expect/warnings/test-case.md index ac23368d0..60b1857dc 100644 --- a/test/bin/mdx-test/expect/warnings/test-case.md +++ b/test/bin/mdx-test/expect/warnings/test-case.md @@ -35,9 +35,6 @@ Test against some false positives: ```ocaml let x = [ "Warning" ] ``` -```mdx-error -val x : string list = ["Warning"] -``` ```ocaml module Warning = struct @@ -46,10 +43,6 @@ end let warning = Warning.Warning ``` -```mdx-error -module Warning : sig type t = Warning end -val warning : Warning.t = Warning.Warning -``` Intended false positive: From 8bf25f4645eae00b740b3b252bb045a7550e09c3 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 31 Oct 2023 13:09:51 +0100 Subject: [PATCH 3/3] Update CHANGES --- CHANGES.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index f8952ea16..71ee19fed 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,10 @@ - Handle the error-blocks syntax (#439, @jonludlam, @gpetiot) +#### Fixed + +- Reduce false-positives while detecting warnings (#440, @Julow) + ### 2.3.1 #### Added