Skip to main content

W0051 - Binary string can be written using sigil syntax

warning

This conversion can currently produce incorrect syntax and corrupt binaries. See #187 and #188 for details.

WeakWarning

-module(main).
fn() -> <<"hello">>.
%% ^^^^^^^^^^^ 💡 weak: Binary string can be written using sigil syntax.

Explanation

OTP 27 brought in string sigils.

This diagnostic provides a way to convert existing binary string literals into sigil form.

The sigil syntax can be opinionated, so this diagnostic is disabled by default. It can be enabled by adding the following to the .elp_lint.toml file for your project:

[linters.binary_string_to_sigil]
enabled = true

We currently do not endorse or discourage either syntax, leaving the final decision to the user. The following section describes some of the pros and cons of sigils.

Pros of sigils

Shorter syntax

The sigil syntax is shorter - 3 chars of “syntax” for the default sigil vs 6 for binary syntax.

Sigils prevent Unicode footguns

Strings in binary expression are by default assumed to be a sequence of byte-size characters. This means that unicode characters are silently truncated to just a single byte, unless the /utf8 flag is explicitly used. The sigil syntax works as expected with unicode.

~> erl +pc unicode # This is to print non-ASCII characters in the Erlang shell
1> {<<"żółć">>, <<"żółć"/utf8>>, ~"żółć"}.
{<<124,243,66,7>>,<<"żółć"/utf8>>,<<"żółć"/utf8>>}

Cons of sigils

Less powerful

Binary syntax can be used as a pattern, in concatenations, comprehensions or other features which are not available for the simpler sigil syntax. This can create situations where both syntaxes are used, adding a sense of confusion.

References