From 2fb2ca9ba8d5dde8af0fa57c69c600ca3e64f3e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20R=2E=20Gu=C3=A9rin?= Date: Mon, 3 Nov 2025 10:39:40 -0500 Subject: [PATCH] Use match variable in mappings to avoid unsed variables --- bitstring.opam | 2 +- dune-project | 4 ++-- ppx/ppx_bitstring.ml | 20 +++++++++++++------- ppx_bitstring.opam | 4 ++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/bitstring.opam b/bitstring.opam index dce7a4c..18e3b56 100644 --- a/bitstring.opam +++ b/bitstring.opam @@ -1,6 +1,6 @@ # This file is generated by dune, edit dune-project instead opam-version: "2.0" -version: "5.0.1" +version: "5.0.2" synopsis: "Bitstrings and bitstring matching for OCaml" description: """ The ocaml-bitstring project adds Erlang-style bitstrings and matching over bitstrings as a syntax extension and library for OCaml. diff --git a/dune-project b/dune-project index 8906faf..f217853 100644 --- a/dune-project +++ b/dune-project @@ -2,7 +2,7 @@ (name bitstring) -(version 5.0.1) +(version 5.0.2) (generate_opam_files true) @@ -34,7 +34,7 @@ ) (depends (ocaml (>= 5.2.0)) - (bitstring (>= 5.0.1)) + (bitstring (>= 5.0.2)) (ocaml (and :with-test (>= 5.2.0))) (ppxlib (>= 0.36.0)) (ounit :with-test))) diff --git a/ppx/ppx_bitstring.ml b/ppx/ppx_bitstring.ml index 058ca99..3717b63 100644 --- a/ppx/ppx_bitstring.ml +++ b/ppx/ppx_bitstring.ml @@ -18,6 +18,12 @@ open Ppxlib open Printf open Ast_builder.Default +(* Exception *) + +let location_exn ~loc msg = + Location.raise_errorf ~loc "%s" msg +;; + (* Type definition *) module Entity = struct @@ -167,13 +173,12 @@ module MatchField = struct | Any of Parsetree.pattern | Tuple of tuple ;; -end -(* Exception *) - -let location_exn ~loc msg = - Location.raise_errorf ~loc "%s" msg -;; + let as_evar v = + match v.pat with + | {ppat_desc = Ppat_var(v); _} -> evar ~loc:v.loc v.txt + | {ppat_loc; _} -> location_exn ~loc:ppat_loc "Pattern is not a variable" +end (* Helper functions *) @@ -691,7 +696,8 @@ let gen_value ~loc fld res beh = | Some b, None -> [%expr let [%p fld.pat] = [%e b] in [%e beh]][@metaloc loc] | None, Some m -> - [%expr let [%p fld.pat] = [%e m] [%e res] in [%e beh]][@metaloc loc] + let exp = MatchField.as_evar fld in + [%expr let [%p fld.pat] = [%e m] [%e exp] in [%e beh]][@metaloc loc] | _, _ -> beh ;; diff --git a/ppx_bitstring.opam b/ppx_bitstring.opam index 0029b79..cf574e5 100644 --- a/ppx_bitstring.opam +++ b/ppx_bitstring.opam @@ -1,6 +1,6 @@ # This file is generated by dune, edit dune-project instead opam-version: "2.0" -version: "5.0.1" +version: "5.0.2" synopsis: "Bitstrings and bitstring matching for OCaml - PPX extension" description: """ The ocaml-bitstring project adds Erlang-style bitstrings and matching over bitstrings as a syntax extension and library for OCaml. @@ -15,7 +15,7 @@ bug-reports: "https://github.com/xguerin/bitstring/issues" depends: [ "dune" {>= "2.7"} "ocaml" {>= "5.2.0"} - "bitstring" {>= "5.0.1"} + "bitstring" {>= "5.0.2"} "ocaml" {with-test & >= "5.2.0"} "ppxlib" {>= "0.36.0"} "ounit" {with-test}