Skip to content
This repository was archived by the owner on Aug 15, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
%% https://github.com/erlang-ls/erlang_ls/issues/1527
%% Only valid for OTP25 or higher
-module(diagnostics_bound_var_in_pattern_maybe).

-if(?OTP_RELEASE<27).
-feature(maybe_expr, enable).
-endif.

-export([foo/0, maybe_expr/0, no_else/0]).

foo() ->
maybe
X ?= bar(),
X == foo
else
e = Error -> Error
% ^- Bound variable in pattern: Error
end.

bar() ->
foo.

maybe_expr() ->
X = 1,
Y = ok,
maybe
X ?= 1
else
Y -> Y
end.

no_else() ->
Y = 1,
maybe
{ok, Y} ?= 2,
X = 4,
Z ?= 8,
X == Z
end.
4 changes: 4 additions & 0 deletions apps/els_lsp/src/els_bound_var_in_pattern_diagnostics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ find_vars_in_tree(Tree, Acc) ->
Pattern = erl_syntax:match_expr_pattern(Tree),
NewAcc = fold_pattern(Pattern, Acc),
find_vars_in_tree(erl_syntax:match_expr_body(Tree), NewAcc);
maybe_match_expr ->
Pattern = erl_syntax:maybe_match_expr_pattern(Tree),
NewAcc = fold_pattern(Pattern, Acc),
find_vars_in_tree(erl_syntax:maybe_match_expr_body(Tree), NewAcc);
clause ->
Patterns = erl_syntax:clause_patterns(Tree),
NewAcc = fold_pattern_list(Patterns, Acc),
Expand Down
2 changes: 2 additions & 0 deletions apps/els_lsp/src/els_erlfmt_ast.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ erlfmt_to_st(Node) ->
%% The special `match` node is encoded as a regular binary operator
{op, Pos, '=', Left, Right} ->
erlfmt_to_st_1({match, Pos, Left, Right});
{op, Pos, '?=', Left, Right} ->
erlfmt_to_st_1({maybe_match, Pos, Left, Right});
%% The special `catch` node is encoded as a regular unary operator
{op, Pos, 'catch', Expr} ->
erlfmt_to_st_1({'catch', Pos, Expr});
Expand Down
35 changes: 35 additions & 0 deletions apps/els_lsp/test/els_diagnostics_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
-export([
atom_typo/1,
bound_var_in_pattern/1,
bound_var_in_pattern_maybe/1,
bound_var_in_pattern_cannot_parse/1,
compiler/1,
compiler_with_behaviour/1,
Expand Down Expand Up @@ -193,6 +194,17 @@ init_per_testcase(TestCase, Config) when
->
mock_refactorerl(),
els_test_utils:init_per_testcase(TestCase, Config);
init_per_testcase(TestCase, Config) when
TestCase =:= bound_var_in_pattern_maybe
->
case ?OTP_RELEASE < 25 of
true ->
{skip, "Maybe expressions are only supported from OTP 25"};
false ->
Config
end,
els_mock_diagnostics:setup(),
els_test_utils:init_per_testcase(TestCase, Config);
init_per_testcase(TestCase, Config) ->
els_mock_diagnostics:setup(),
els_test_utils:init_per_testcase(TestCase, Config).
Expand Down Expand Up @@ -363,6 +375,29 @@ bound_var_in_pattern(_Config) ->
end,
els_test:run_diagnostics_test(Path, Source, Errors, Warnings, Hints).

%% #1527
-spec bound_var_in_pattern_maybe(config()) -> ok.
bound_var_in_pattern_maybe(_Config) ->
Path = src_path("diagnostics_bound_var_in_pattern_maybe.erl"),
Source = <<"BoundVarInPattern">>,
Errors = [],
Warnings = [],
Hints = [
#{
message => <<"Bound variable in pattern: X">>,
range => {{26, 8}, {26, 9}}
},
#{
message => <<"Bound variable in pattern: Y">>,
range => {{28, 8}, {28, 9}}
},
#{
message => <<"Bound variable in pattern: Y">>,
range => {{34, 13}, {34, 14}}
}
],
els_test:run_diagnostics_test(Path, Source, Errors, Warnings, Hints).

-spec bound_var_in_pattern_cannot_parse(config()) -> ok.
bound_var_in_pattern_cannot_parse(_Config) ->
Path = src_path("diagnostics_bound_var_in_pattern_cannot_parse.erl"),
Expand Down
5 changes: 2 additions & 3 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
debug_info,
warnings_as_errors,
warn_export_vars,
warn_unused_import,
warn_missing_spec_all
warn_unused_import
]}.

{deps, [
Expand All @@ -15,7 +14,7 @@
{docsh, "0.7.2"},
{elvis_core, "~> 3.2.2"},
{rebar3_format, "0.8.2"},
{erlfmt, "1.3.0"},
{erlfmt, "1.5.0"},
{ephemeral, "2.0.4"},
{tdiff, "0.1.2"},
{uuid, "2.0.1", {pkg, uuid_erl}},
Expand Down
6 changes: 3 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{<<"docsh">>,{pkg,<<"docsh">>,<<"0.7.2">>},0},
{<<"elvis_core">>,{pkg,<<"elvis_core">>,<<"3.2.2">>},0},
{<<"ephemeral">>,{pkg,<<"ephemeral">>,<<"2.0.4">>},0},
{<<"erlfmt">>,{pkg,<<"erlfmt">>,<<"1.3.0">>},0},
{<<"erlfmt">>,{pkg,<<"erlfmt">>,<<"1.5.0">>},0},
{<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.1">>},2},
{<<"gradualizer">>,
{git,"https://github.com/josefs/Gradualizer.git",
Expand All @@ -28,7 +28,7 @@
{<<"docsh">>, <<"F893D5317A0E14269DD7FE79CF95FB6B9BA23513DA0480EC6E77C73221CAE4F2">>},
{<<"elvis_core">>, <<"D5AE5FB7ACDF9D23A2AA3F6E4610490A06F7E8FB33EE65E09C5EA3A0ECF64A73">>},
{<<"ephemeral">>, <<"B3E57886ADD5D90C82FE3880F5954978222A122CB8BAA123667401BBAAEC51D6">>},
{<<"erlfmt">>, <<"672994B92B1A809C04C46F0B781B447BF9AB7A515F5856A96177BC1962F100A9">>},
{<<"erlfmt">>, <<"5DDECA120A6E8E0A0FAB7D0BB9C2339D841B1C9E51DD135EE583256DEF20DE25">>},
{<<"getopt">>, <<"C73A9FA687B217F2FF79F68A3B637711BB1936E712B521D8CE466B29CBF7808A">>},
{<<"jsx">>, <<"20A170ABD4335FC6DB24D5FAD1E5D677C55DADF83D1B20A8A33B5FE159892A39">>},
{<<"katana_code">>, <<"0C42BDCD7E59995876AED9F678CF62E3D12EF42E0FBB2190556E64BFEBDD15C6">>},
Expand All @@ -44,7 +44,7 @@
{<<"docsh">>, <<"4E7DB461BB07540D2BC3D366B8513F0197712D0495BB85744F367D3815076134">>},
{<<"elvis_core">>, <<"3786F027751CC265E7389BF5AC1329DB547510D80F499B45EFE771BDAF889B36">>},
{<<"ephemeral">>, <<"4B293D80F75F9C4575FF4B9C8E889A56802F40B018BF57E74F19644EFEE6C850">>},
{<<"erlfmt">>, <<"2A84AA1EBA2F4FCD7DD31D5C57E9DE2BC2705DDA18DA4553F27DF7114CFAA052">>},
{<<"erlfmt">>, <<"3933A40CFBE790AD94E5B650B36881DE70456319263C1479B556E9AFDBD80C75">>},
{<<"getopt">>, <<"53E1AB83B9CEB65C9672D3E7A35B8092E9BDC9B3EE80721471A161C10C59959C">>},
{<<"jsx">>, <<"37BECA0435F5CA8A2F45F76A46211E76418FBEF80C36F0361C249FC75059DC6D">>},
{<<"katana_code">>, <<"AE3BBACA187511588F69695A9FF22251CB2CC672FDCCC180289779BDD25175EF">>},
Expand Down
Loading