Typescript: noUnusedLocals: Don't count self-call in recursive function

Created on 25 May 2017  路  5Comments  路  Source: microsoft/TypeScript

TypeScript Version: nightly (2.4.0-dev.20170525)

Code

function f(): void { f(); }

Expected behavior:

Function f is unused.

Actual behavior:

No error.

(Ref: palantir/tslint#1937)

Suggestion

All 5 comments

how far do we want to go with this? is a class that is only instantiated in an instance method the same? a var that is only referenced in its own intializer? etc..

It makes sense to disclude any use of a class inside itself, since the self-references won't be used unless the class is. But I don't think I would treat instantiation specially, since it's hard to tell when a reference to a class might instantiate it.

class C {
    static s() {
        new C().m();
    }

    m() {
        new C();
    }
}

maybeThisInstantiatesIt(C);

function maybeThisInstantiatesIt(klass: any) {
    klass.s();
}

Without the maybeThisInstantiatesIt(C) call, I would definitely mark the class as unused.

A variable initialized to itself is an error anyway if you use const or let, so I don't think that's an issue.

On the other hand, the mere presence of a static property may be side-affecting in its initialization, so the following code, which marks D unused today, actually uses the static initialization of s.

function sideAffecting(): number {
    console.log("called");
    return 10;
}

function foo() {
    class D {
        static s: number = sideAffecting();
    }
}
foo();

But perhaps this should be a separate issue.

D itself is unused though. If you write const x = sideAffecting(); we will also give you an unused-variable error because x is unused even if its initializer has side effects.

I agree with @andy-ms . This is an issue on checking recursive functions. We don't need to consider classes.

@aozgaa In your case, D should be unused, and sideAffecting should be used.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dlaberge picture dlaberge  路  3Comments

uber5001 picture uber5001  路  3Comments

remojansen picture remojansen  路  3Comments

jbondc picture jbondc  路  3Comments

siddjain picture siddjain  路  3Comments