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

Detect collections using double brace initialization #914

Closed
gaul opened this issue Jan 28, 2018 · 3 comments
Closed

Detect collections using double brace initialization #914

gaul opened this issue Jan 28, 2018 · 3 comments

Comments

@gaul
Copy link
Contributor

gaul commented Jan 28, 2018

Some call sites construct collections via double brace initialization:

new HashSet<String>() {{ add("NU"); add("TK"); add("NC"); add("AC"); }};

error-prone should recommend the more succinct and efficient:

new HashSet<String>(Arrays.asList("NU", "TK", "NC", "AC"));

The former creates an anonymous inner class and an implicit this reference which can prolong the outer class lifetime:

https://blog.nishtahir.com/2015/09/27/why-you-shouldnt-use-the-double-brace-initializer/

@JakeWharton
Copy link
Contributor

JakeWharton commented Jan 28, 2018 via email

@JakeWharton
Copy link
Contributor

JakeWharton commented Jan 28, 2018 via email

@jbduncan
Copy link
Contributor

Or even Guava's Immutable* collections?

ronshapiro pushed a commit that referenced this issue Feb 5, 2018
Fixes #914

RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=184371199
@eaftan eaftan closed this as completed in c0db107 Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants