From 9ea684c90198da67070985231ae08cc39fd522e0 Mon Sep 17 00:00:00 2001 From: Pablo Brudnick Date: Mon, 26 May 2025 10:04:09 -0300 Subject: [PATCH 1/2] Add rebar3_hank plugin for detecing oxbow and dead code --- .github/workflows/run_test_case.yaml | 3 +++ Makefile | 3 +++ rebar.config | 13 ++++++++++++- src/hocon_schema_json.erl | 6 +++--- src/hocon_schema_md.erl | 5 ++--- test/hocon_schema_aliases_tests.erl | 2 ++ 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/.github/workflows/run_test_case.yaml b/.github/workflows/run_test_case.yaml index 5cdc4083..7a549114 100644 --- a/.github/workflows/run_test_case.yaml +++ b/.github/workflows/run_test_case.yaml @@ -20,6 +20,9 @@ jobs: run: | make dialyzer rm -f rebar.lock + - name: Detect dead code + run: | + make hank - name: Run tests run: | make eunit diff --git a/Makefile b/Makefile index e8b1ba98..48ee8910 100644 --- a/Makefile +++ b/Makefile @@ -55,3 +55,6 @@ erlfmt: $(REBAR) .PHONY: erlfmt-check erlfmt-check: $(REBAR) $(REBAR) fmt -c + +hank: + $(REBAR) hank diff --git a/rebar.config b/rebar.config index 06c0fb99..64bc82db 100644 --- a/rebar.config +++ b/rebar.config @@ -57,4 +57,15 @@ {escript_comment, "%% Hocon 0.1.0"}. {escript_emu_args, "%%! +sbtu +A1\n"}. {yrl_opts, [verbose, warnings_as_errors]}. -{project_plugins, [erlfmt]}. + +{hank, [ + {ignore, [ + "include/hoconsc.hrl", + "include/hocon_types.hrl", + "include/hocon.hrl", + %% dynamically generated file + "src/hocon_parser.erl" + ]} +]}. + +{project_plugins, [erlfmt, {rebar3_hank, "1.4.1"}]}. diff --git a/src/hocon_schema_json.erl b/src/hocon_schema_json.erl index 4c38f976..c7451a22 100644 --- a/src/hocon_schema_json.erl +++ b/src/hocon_schema_json.erl @@ -55,10 +55,10 @@ gen(Schema, Opts) -> {RootNs, RootFields, Structs} = hocon_schema:find_structs(Schema, Opts), Json = [ - gen_struct(RootNs, RootNs, "Root Config Keys", #{fields => RootFields}, Opts) + gen_struct(RootNs, "Root Config Keys", #{fields => RootFields}, Opts) | lists:map( fun({Ns, Name, Meta}) -> - case gen_struct(RootNs, Ns, Name, Meta, Opts) of + case gen_struct(Ns, Name, Meta, Opts) of #{fields := []} = Meta1 -> error( {struct_with_no_fields, #{ @@ -79,7 +79,7 @@ gen(Schema, Opts) -> ], Json. -gen_struct(_RootNs, Ns, Name, #{fields := Fields} = Meta, Opts) -> +gen_struct(Ns, Name, #{fields := Fields} = Meta, Opts) -> Paths = case Meta of #{paths := Ps} -> lists:sort(maps:keys(Ps)); diff --git a/src/hocon_schema_md.erl b/src/hocon_schema_md.erl index 21fcd990..c77cdf8f 100644 --- a/src/hocon_schema_md.erl +++ b/src/hocon_schema_md.erl @@ -64,7 +64,7 @@ fmt_struct( fmt_paths(Paths), fmt_envs(Paths, Opts), "\n\n**Fields**\n\n", - lists:map(fun(F) -> fmt_field(F, Opts) end, Fields) + lists:map(fun(F) -> fmt_field(F) end, Fields) ]. fmt_paths([]) -> @@ -95,8 +95,7 @@ fmt_field( #{ name := Name, type := Type - } = Field, - _Opts + } = Field ) -> Default = fmt_default(maps:get(default, Field, undefined)), Desc = maps:get(desc, Field, ""), diff --git a/test/hocon_schema_aliases_tests.erl b/test/hocon_schema_aliases_tests.erl index 7ffdb816..56f4c13c 100644 --- a/test/hocon_schema_aliases_tests.erl +++ b/test/hocon_schema_aliases_tests.erl @@ -16,6 +16,8 @@ -module(hocon_schema_aliases_tests). +-hank([{unnecessary_function_arguments, [{incr, 2}]}]). + -include_lib("eunit/include/eunit.hrl"). -include_lib("typerefl/include/types.hrl"). -include_lib("hocon/include/hoconsc.hrl"). From 549a9cab04e887391065c34fc7d624232b4213fd Mon Sep 17 00:00:00 2001 From: Pablo Brudnick Date: Mon, 26 May 2025 10:24:28 -0300 Subject: [PATCH 2/2] Ignore missing dynamically generated file --- rebar.config | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rebar.config b/rebar.config index 64bc82db..dc1138b9 100644 --- a/rebar.config +++ b/rebar.config @@ -64,7 +64,8 @@ "include/hocon_types.hrl", "include/hocon.hrl", %% dynamically generated file - "src/hocon_parser.erl" + "src/hocon_parser.erl", + "src/hocon_scanner.erl" ]} ]}.