-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: The "update entry" error is un-intuitive, especially in presence of macros. Circumventing the warning via a `begin/end` block is also not intuitive, so let's make the information discoverable by developers. If we find a better way to circumvent the warning, we can update this page. Reviewed By: alanz Differential Revision: D58005331 fbshipit-source-id: 838cbb2160cf04b338684c6ceceb6eb8cd3d63b4
- Loading branch information
1 parent
8402121
commit d82f86f
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
sidebar_position: 318 | ||
--- | ||
|
||
# L1318 - Expression Updates a Literal | ||
|
||
## Error | ||
|
||
```erlang | ||
-define(DEFAULT, #{a => 1}). | ||
|
||
updated(Value) -> | ||
?DEFAULT#{a => Value}. | ||
%% ^^^^^^^^ warning: expression updates a literal | ||
``` | ||
|
||
## Explanation | ||
|
||
The warning occurs when a map or a record is updated using the following syntaxes: | ||
|
||
```erlang | ||
> #{a => b}#{c => d} | ||
#{c => d,a => b} | ||
``` | ||
|
||
```erlang | ||
> rd(my_record, {a, b}). %% rd/2 allows you to define an Erlang record from a shell | ||
> #my_record{a = 1}#my_record{a = 2}. | ||
#my_record{a = 2,b = undefined} | ||
``` | ||
|
||
While this is valid Erlang syntax, this behaviour is usually not intentional and the result of a missing comma in a list of elements. Consider, for example: | ||
|
||
```erlang | ||
my_list() -> | ||
[ #{a => 1} %% Missing comma here! | ||
#{a => 2} | ||
]. | ||
``` | ||
|
||
Which results in `[#{a => 2}]`. | ||
|
||
To fix the issue, just add the missing comma. If the update is intentional, a common (but ugly) workaround to silent the linter is to wrap the first map/record in a `begin/end` block, | ||
which will avoid any additional runtime cost. As an example, you could rewrite the following: | ||
|
||
```erlang | ||
-define(DEFAULT, #{a => 1}). | ||
|
||
updated(Value) -> | ||
?DEFAULT#{a => Value}. | ||
``` | ||
|
||
Into: | ||
|
||
```erlang | ||
-define(DEFAULT, #{a => 1}). | ||
|
||
updated(Value) -> | ||
begin ?DEFAULT end#{a => Value}. | ||
``` |