Skip to content

Conversation

@halibobo1205
Copy link
Contributor

@halibobo1205 halibobo1205 commented Oct 15, 2025

What does this PR do?
Fix implicit narrowing conversion, pre PR: #6417
Why are these changes required?
x += y is equivalent to x = (T)(x + y), where T is the type of x, is not equivalent to x += (T) y
This PR has been tested by:

  • Unit Tests
  • Manual Testing

Follow up

Extra details

public class Test {

  public static void main(String[] args) {
    long l1 = 36714;
    long l2 = 36714;
    long l3 = 36714;
    double d = (double) 50 / 64400 * 2210208;
    
    l1 = method1(l1, d);
    l2 = method2(l2, d);
    l3 = method3(l3, d);
    
    System.out.println("l1 = " + l1);
    System.out.println("l2 = " + l2);
    System.out.println("l3 = " + l3);
    System.out.println("l2 == l3: " + (l2 == l3));
    System.out.println("l1 != l2: " + (l1 != l2));
    System.out.println("l1 != l3: " + (l1 != l3));
  }
  
  
  public static long method1(long l1, double d) {
    return l1 + (long) (d);
  }
  
  
  public static long method2(long l2, double d) {
    l2 += d;
    return l2;
  }
  
  public static long method3(long l3, double d) {
    return (long) (l3 + d);
  }
}
javac Test.java
javap -c Test.class
 public static long method1(long, double);
    Code:
       0: lload_0
       1: dload_2
       2: d2l
       3: ladd
       4: lreturn

  public static long method2(long, double);
    Code:
       0: lload_0
       1: l2d
       2: dload_2
       3: dadd
       4: d2l
       5: lstore_0
       6: lload_0
       7: lreturn

  public static long method3(long, double);
    Code:
       0: lload_0
       1: l2d
       2: dload_2
       3: dadd
       4: d2l
       5: lreturn
}

long userVote = vote.getValue();
double voteRate = (double) userVote / totalVote;
reward += (long) (voteRate * totalReward);
reward = (long) (reward + voteRate * totalReward);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand it logically equals to previous reward += voteRate * totalReward;, just wonder why you not revert to previous one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix: Code scanning alerts java/implicit-cast-in-compound-assignment

@halibobo1205
Copy link
Contributor Author

#6466

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants