error-prone icon indicating copy to clipboard operation
error-prone copied to clipboard

`BigDecimalLiteralDouble ` false negative for the boxed value

Open Emilyaxe opened this issue 7 months ago • 1 comments

Error Prone version

2.49.0 (error_prone_core)

Check name

BigDecimalLiteralDouble

Description

The check reports new BigDecimal(double) and new BigDecimal(float) for primitive arguments, but misses the same constructions when the value is wrapped in Double or Float. Since BigDecimal has no Double/Float constructor, the boxed value is automatically unboxed and the same lossy conversion still occurs. This causes a false negative for the boxed forms.

Reproducer

import java.math.BigDecimal;

class Repro {
  // BEFORE — correctly flagged
  BigDecimal a = new BigDecimal(0.1);
  BigDecimal b = new BigDecimal(0.3f);
  BigDecimal c;
  {
    float f = 0.1f;
    c = new BigDecimal(f);
  }

  // AFTER — not flagged, but semantically identical (auto-unboxing)
  Double boxedD = 0.1;
  BigDecimal a2 = new BigDecimal(boxedD);
  BigDecimal b2 = new BigDecimal((Float) 0.3f);
  Float boxedF = 0.1f;
  BigDecimal c2 = new BigDecimal(boxedF);
}

Actual behavior

Only the BEFORE primitive-literal constructions are flagged; every boxed form — variable-typed, cast, or initialized from a literal — passes without a warning.

Emilyaxe avatar Apr 30 '26 08:04 Emilyaxe

Hi, I would like to work on this issue.

My plan is to add regression coverage for boxed Double/Float values that are auto-unboxed when passed to BigDecimal(double), then update BigDecimalLiteralDouble to detect those cases consistently with primitive floating-point literals.

codingkiddo avatar May 09 '26 08:05 codingkiddo