Skip to content

Commit 47225b5

Browse files
committed
pmd fix: avoid assignment in operand
1 parent ded7b8e commit 47225b5

8 files changed

Lines changed: 39 additions & 11 deletions

File tree

commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/JenkinsSmallFast32.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public JenkinsSmallFast32(Integer seed) {
5353
*/
5454
private void setSeedInternal(int seed) {
5555
a = 0xf1ea5eed;
56-
b = c = d = seed;
56+
b = seed;
57+
c = seed;
58+
d = seed;
5759
for (int i = 0; i < 20; i++) {
5860
next();
5961
}

commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/MiddleSquareWeylSequence.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ protected void setStateInternal(byte[] state) {
125125
@Override
126126
public int next() {
127127
x *= x;
128-
x += w += s;
128+
w += s;
129+
x += w;
129130
x = (x >>> 32) | (x << 32);
130131
return (int) x;
131132
}
@@ -135,11 +136,13 @@ public int next() {
135136
public long nextLong() {
136137
// Avoid round trip from long to int to long by performing two iterations inline
137138
x *= x;
138-
x += w += s;
139+
w += s;
140+
x += w;
139141
final long i1 = x & 0xffffffff00000000L;
140142
x = (x >>> 32) | (x << 32);
141143
x *= x;
142-
x += w += s;
144+
w += s;
145+
x += w;
143146
final long i2 = x >>> 32;
144147
x = i2 | x << 32;
145148
return i1 | i2;

commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/MultiplyWithCarry256.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public int next() {
113113
index &= 0xff;
114114
final long t = A * (state[index] & 0xffffffffL) + carry;
115115
carry = (int) (t >> 32);
116-
return state[index++] = (int) t;
116+
state[index++] = (int) t;
117+
return (int) t;
117118
}
118119
}

commons-rng-core/src/main/java/org/apache/commons/rng/core/source64/JenkinsSmallFast64.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public JenkinsSmallFast64(Long seed) {
5252
*/
5353
private void setSeedInternal(long seed) {
5454
a = 0xf1ea5eedL;
55-
b = c = d = seed;
55+
b = seed;
56+
c = seed;
57+
d = seed;
5658
for (int i = 0; i < 20; i++) {
5759
next();
5860
}

commons-rng-core/src/main/java/org/apache/commons/rng/core/source64/SplitMix64.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public SplitMix64(Long seed) {
5656
/** {@inheritDoc} */
5757
@Override
5858
public long next() {
59-
long z = state += 0x9e3779b97f4a7c15L;
59+
long z = state + 0x9e3779b97f4a7c15L;
60+
state = z;
6061
z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
6162
z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
6263
return z ^ (z >>> 31);

commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaTsangWangDiscreteSampler.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ private static final class MarsagliaTsangWangBase64Int8DiscreteSampler
189189
t2 = t1 + (n2 << 18);
190190
t3 = t2 + (n3 << 12);
191191
t4 = t3 + (n4 << 6);
192-
n1 = n2 = n3 = n4 = n5 = 0;
192+
n1 = 0;
193+
n2 = 0;
194+
n3 = 0;
195+
n4 = 0;
196+
n5 = 0;
193197

194198
// Fill tables
195199
for (int i = 0; i < prob.length; i++) {
@@ -331,7 +335,11 @@ private static final class MarsagliaTsangWangBase64Int16DiscreteSampler
331335
t2 = t1 + (n2 << 18);
332336
t3 = t2 + (n3 << 12);
333337
t4 = t3 + (n4 << 6);
334-
n1 = n2 = n3 = n4 = n5 = 0;
338+
n1 = 0;
339+
n2 = 0;
340+
n3 = 0;
341+
n4 = 0;
342+
n5 = 0;
335343

336344
// Fill tables
337345
for (int i = 0; i < prob.length; i++) {
@@ -470,7 +478,11 @@ private static final class MarsagliaTsangWangBase64Int32DiscreteSampler
470478
t2 = t1 + (n2 << 18);
471479
t3 = t2 + (n3 << 12);
472480
t4 = t3 + (n4 << 6);
473-
n1 = n2 = n3 = n4 = n5 = 0;
481+
n1 = 0;
482+
n2 = 0;
483+
n3 = 0;
484+
n4 = 0;
485+
n5 = 0;
474486

475487
// Fill tables
476488
for (int i = 0; i < prob.length; i++) {

commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/SeedFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ static void ensureNonZero(byte[] seed, int from, int to, UniformRandomProvider s
374374
final int end = from + len;
375375
int i = from;
376376
while (i < end) {
377-
long v = MixFunctions.stafford13(x += MixFunctions.GOLDEN_RATIO_64);
377+
x += MixFunctions.GOLDEN_RATIO_64;
378+
long v = MixFunctions.stafford13(x);
378379
for (int j = 0; j < 8; j++) {
379380
seed[i++] = (byte) v;
380381
v >>>= 8;

src/conf/pmd/pmd-ruleset.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@
290290
<rule ref="category/java/errorprone.xml/AssignmentInOperand">
291291
<properties>
292292
<property name="allowIncrementDecrement" value="true" />
293+
<!-- Reassignment of the constructor seed is allowed to extend the seed length. -->
294+
<property name="violationSuppressXPath"
295+
value="./ancestor-or-self::ClassDeclaration[@SimpleName='AbstractL64X128' or
296+
@SimpleName='L128X1024Mix' or @SimpleName='L128X128Mix' or
297+
@SimpleName='L128X256Mix' or @SimpleName='L64X1024Mix' or
298+
@SimpleName='L64X256Mix']"/>
293299
</properties>
294300
</rule>
295301

0 commit comments

Comments
 (0)