Skip to content

prefer-udim2-shorthand

Replaces new UDim2(...) with UDim2.fromScale(...) or UDim2.fromOffset(...) when the other half is zero.

The rule checks new UDim2(scaleX, offsetX, scaleY, offsetY) calls with exactly four arguments.

  • If offsetX and offsetY both evaluate to 0, it reports UDim2.fromScale(scaleX, scaleY).
  • If scaleX and scaleY both evaluate to 0, it reports UDim2.fromOffset(offsetX, offsetY).

It can evaluate numeric literals, identifiers, unary + and -, and simple arithmetic with +, -, *, /, and %. Mixed scale and offset constructors are left alone.

Incorrect
new UDim2(1, 0, 1, 0);
new UDim2(0, 100, 0, 50);
new UDim2(x + 1, 0, 1 - y, 0);
Correct
UDim2.fromScale(1, 1);
UDim2.fromOffset(100, 50);
UDim2.fromScale(x + 1, 1 - y);
new UDim2(0.5, 10, 0.5, 10);
new UDim2(0, 0, 0, 0);
  • constructors with fewer or more than four arguments
  • constructors where the zero side cannot be evaluated
  • unsupported expressions such as bitwise operators or string literals
  • mixed scale and offset values