Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule: Avoid duplicate keys in Maps #282

Open
elbrujohalcon opened this issue Feb 28, 2023 · 3 comments
Open

New rule: Avoid duplicate keys in Maps #282

elbrujohalcon opened this issue Feb 28, 2023 · 3 comments
Assignees
Labels
Milestone

Comments

@elbrujohalcon
Copy link
Member

elbrujohalcon commented Feb 28, 2023

Avoid Duplicate Keys in Maps

Brief Description

The idea is to emit a warning in situations like the following ones (notice the duplication of keys everywhere):

Map = #{key1 => value1, key2 => value2, key1 => value3}.
OtherMap = Map#{key2 := new_value2, key3 => new_value3, key2 := new_value4, key3 => new_value5},
#{key1 := V1, key2 := V2, key1 := V3} = OtherMap.

Should be on by default?

YES

Reasoning

These are perfectly valid Erlang expressions but are often the result of a typo or some confusion from the developers and they can easily be simplified to produce exactly the same output.

Examples

-module bad.

-export [my_fun/0].

my_fun() ->
    Map = #{key1 => value1, key2 => value2, key1 => value3}.
    OtherMap = Map#{key2 := new_value2, key3 => new_value3, key2 := new_value4, key3 => new_value5},
    #{key1 := V1, key2 := V2, key1 := V3} = OtherMap,
    {V1, V2, V3}.
-module good.

-export [my_fun/0].

my_fun() ->
    Map = #{key2 => value2, key1 => value3}.
    OtherMap = Map#{key2 := new_value4, key3 => new_value5},
    #{key1 := V1 = V3, key2 := V2} = OtherMap,
    {V1, V2, V3}.

Origin (#281)

Inspired by the duplicate_key rule from https://coffeelint.github.io/

@elbrujohalcon elbrujohalcon added this to the 3.0.0 milestone Feb 28, 2023
@elbrujohalcon
Copy link
Member Author

elbrujohalcon commented Feb 28, 2023 via email

@paulo-ferraz-oliveira
Copy link
Collaborator

paulo-ferraz-oliveira commented Feb 28, 2023

Ah, you got it by email and replied 😄 My comment was "I'm sure there was something along the lines of this rule. Maybe I saw it in the formatter".

I was talking about (found it!) this: #156

@paulo-ferraz-oliveira
Copy link
Collaborator

(which is not the same, that one deals with assignment to different variables, from the same key)

@paulo-ferraz-oliveira paulo-ferraz-oliveira changed the title Avoid duplicate keys in Maps New rule: Avoid duplicate keys in Maps Mar 1, 2023
@slyeargin slyeargin self-assigned this Mar 1, 2023
@elbrujohalcon elbrujohalcon modified the milestones: 3.0.0, 3.1.0 Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants