List assignment with ternary operator
See original GitHub issueI have found a False negative: I wrote a User class that is immutable but the detector doesn’t think it is. I directly provide a piece of code:
given that this.roles
is a private final field and no setter methods are defined
if I write this code in the constructor, the class is correctly recognized IMMUTABLE.
if (roles == null) {
this.roles = null;
} else {
this.roles = Collections.unmodifiableList(new ArrayList<String>(roles));
}
but, if I write this code instead: this.roles = (roles == null ? null : Collections.unmodifiableList(new ArrayList<String>(roles))); the check fail with this Exception:
Expected:[...].User to be IMMUTABLE
but: [...].User is actually NOT_IMMUTABLE
Reasons:
Field is not a wrapped collection type. You can use this expression: Collections.unmodifiableList(new ArrayList<String>(roles)) [Field: roles at [...].User(User.java:1)]
Allowed reasons:
None.
at org.mutabilitydetector.unittesting.internal.AssertionReporter.assertThat(AssertionReporter.java:48)
at org.mutabilitydetector.unittesting.MutabilityAsserter.assertImmutable(MutabilityAsserter.java:127)
at org.mutabilitydetector.unittesting.MutabilityAssert.assertImmutable(MutabilityAssert.java:672)
at [...]TestUser.theUserIsImmutable(TestUser.java:92)
if I need to provide more info, please let me know.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Python ? (conditional/ternary) operator for assignments
operator. This allows you to make very terse choices between two values based on the truth of a condition, which makes expressions, including...
Read more >How to Do Ternary Operator Assignment in Python - Webucator
Many programming languages, including Python, have a ternary conditional operator, which is most often used for conditional variable assignment.
Read more >Conditional (ternary) operator - JavaScript - MDN Web Docs
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?)
Read more >Ternary Operator in Python - GeeksforGeeks
Ternary operators are also known as conditional expressions are operators that evaluate something based on a condition being true or false.
Read more >How to Use the Python Ternary Operator
Summary. The Python ternary operator is value_if_true if condition else value_if_false . Use the ternary operator to make your code more concise.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for raising. I’ve easily reproduced in a test case.
I can’t give an ETA on a fix right now, but I’ll start taking a look.
Hi @Grundlefleck The case I’d thought of is covered by @sband’s (nice work!) latest PR. It was about taking care when null (or a mutable expression) could come on either side of the ternary operator since then you can’t simply rely on the latest PUT_FIELD right before your FrameNode->LabelNode
So we’re looking good!