Skip to content

Commit 2d9c3fb

Browse files
author
José Valim
committed
Print warnings when used ? with characters with escape codes
Closes #2640
1 parent 6f07f4a commit 2d9c3fb

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/elixir/src/elixir_tokenizer.erl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ tokenize([$?,$\\,H|T], Line, Scope, Tokens) ->
204204
tokenize(T, Line, Scope, [{number, Line, Char}|Tokens]);
205205

206206
tokenize([$?,Char|T], Line, Scope, Tokens) ->
207+
case handle_char(Char) of
208+
{Escape, Name} ->
209+
Msg = io_lib:format("found ? followed by codepoint 0x~.16B (~ts), please use ~ts instead",
210+
[Char, Name, Escape]),
211+
elixir_errors:warn(Line, Scope#elixir_tokenizer.file, Msg);
212+
false ->
213+
ok
214+
end,
207215
tokenize(T, Line, Scope, [{number, Line, Char}|Tokens]);
208216

209217
% Heredocs
@@ -470,6 +478,18 @@ until_eol("\n" ++ _) -> [];
470478
until_eol([]) -> [];
471479
until_eol([H|T]) -> [H|until_eol(T)].
472480

481+
handle_char(7) -> {"\\a", "alert"};
482+
handle_char($\b) -> {"\\b", "backspace"};
483+
handle_char($\d) -> {"\\d", "delete"};
484+
handle_char($\e) -> {"\\e", "escape"};
485+
handle_char($\f) -> {"\\f", "form feed"};
486+
handle_char($\n) -> {"\\n", "newline"};
487+
handle_char($\r) -> {"\\r", "carriage return"};
488+
handle_char($\s) -> {"\\s", "space"};
489+
handle_char($\t) -> {"\\t", "tab"};
490+
handle_char($\v) -> {"\\v", "vertical tab"};
491+
handle_char(_) -> false.
492+
473493
escape_char(List) ->
474494
<<Char/utf8>> = elixir_interpolation:unescape_chars(list_to_binary(List)),
475495
Char.

lib/elixir/test/elixir/kernel/warning_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,14 @@ defmodule Kernel.WarningTest do
465465
purge [Sample]
466466
end
467467

468+
test :warning_on_codepoint_escape do
469+
assert capture_err(fn ->
470+
Code.eval_string "? "
471+
end) =~ "nofile:1: warning: found ? followed by codepoint 0x20 (space), please use \\s instead"
472+
after
473+
purge [Sample]
474+
end
475+
468476
test :typedoc_on_typep do
469477
assert capture_err(fn ->
470478
Code.eval_string """

0 commit comments

Comments
 (0)