question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

List assignment with ternary operator

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Grundlefleckcommented, Feb 26, 2018

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.

0reactions
rohansuricommented, Mar 16, 2018

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found