fallow icon indicating copy to clipboard operation
fallow copied to clipboard

Member called on a local assigned from a method call is reported unused

Open Smrtnyk opened this issue 3 weeks ago • 1 comments

What happened?

Follow-up to #4311. local.rotate(angle).translate(...) is now credited, but the same call through a local is not. Both of these still report Point.translate:

  • const rotated = angle ? local.rotate(angle) : local; (our real code)
  • const rotated = local.rotate(angle);

The receiver kind does not matter: a parameter typed Point and a const local = new Point(0, 0) local both fail. A type annotation on the local (const rotated: Point = ...) makes it pass.

Reproduction

mkdir repro30 && cd repro30
echo '{ "name": "repro", "private": true, "type": "module", "main": "src/index.ts" }' > package.json
mkdir node_modules src
cat > src/point.ts <<'EOF'
export class Point {
    constructor(
        readonly x: number,
        readonly y: number,
    ) {}

    rotate(angle: number): Point {
        return new Point(this.x * Math.cos(angle), this.y * Math.sin(angle));
    }

    translate(dx: number, dy: number): Point {
        return new Point(this.x + dx, this.y + dy);
    }
}
EOF
cat > src/index.ts <<'EOF'
import type { Point } from "./point.js";

export function toWorld(local: Point, center: Point, angle: number): Point {
    const rotated = angle ? local.rotate(angle) : local;
    return rotated.translate(center.x, center.y);
}
EOF
fallow dead-code
  1 entry point detected (1 package.json)

── Unused Code ─────────────────────────────────────

● Unused class members (1)
  src/point.ts
    :11 Point.translate
  Class methods or properties never referenced outside their class
  https://fallow.tools/docs/explanations/dead-code/#unused-class-members

✗ 1 class member (0.01s)

Replacing the ternary with const rotated = local.rotate(angle); gives the same output.

Expected behavior

Point.translate is credited through the return type of rotate, and through the ternary whose branches are both Point.

Fallow version

3.42.0

Operating system

Linux

Configuration

None.

Smrtnyk avatar Nov 12 '26 07:11 Smrtnyk

Released in v3.43.1 (npm install [email protected]).

fallow dead-code now credits a member on a local that holds a method call result. const rotated = local.rotate(angle); rotated.translate(1, 1); credits Point.translate, the same as the chained call.

Thanks for the report, @Smrtnyk.

BartWaardenburg avatar Nov 15 '26 07:11 BartWaardenburg