S1764: FP on expressions with side-effects

Description

S1764 raises an issue if the left and right argument of a binary operator are identical expressions. This, however, is an issue for referential transparent expressions only, i.e., expressions whose value does not change and that have no side effects.

The current rule implementation does not take this into account and consequently, reports on the following code example:

public interface HelloWorld { default boolean hello() { Random r = new Random(); return r.nextBoolean() && r.nextBoolean() && r.nextBoolean(); // FP } }

This is a false positive because r.nextBoolean() returns a different value each time and has the side effect of changing the state hidden in r. S1764 should not report if the arguments contain function calls.

Note that even field access is referential transparent only in the case of final fields because a field’s contents could be changed by another thread while the binary expression is executed. We might ignore that case, though, and treat field access as referential transparent in the context of this rule.

Activity

Fixed
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Start date

Components

Fix versions

Due date

Priority

Sentry

Created August 5, 2024 at 12:17 PM
Updated October 16, 2024 at 2:06 PM
Resolved September 24, 2024 at 2:44 PM