-
Notifications
You must be signed in to change notification settings - Fork 12.8k
false negative on uninitialized reference (null pointer exception) in class method #41446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This doesn't really tell us much, especially in the presence of virtual methods (which is going to be very common). Inlining one level instead of zero levels just creates "TypeScript isn't consistent about X" bugs. |
It does not create "TypeScript isn't consistent about X" bugs. It would not create any inconsistency. It would find more bugs. |
@beauxq I noticed a similar issue that has a false negative. How can we handle something like this? interface IB { method(): void }
class A implements IB {
b = new B(this)
method() { this.b.method() } // delegation pattern
}
class B implements IB {
constructor(b: IB) {
b.method() // crashes unless commented out
}
method() { console.log(0) }
}
new A().method()
Here's a Java version of same thing (results in public class Main {
interface IB { void method(); }
static class A implements IB {
final B b = new B(this);
public void method() { this.b.method(); }
}
static class B implements IB {
public B(IB b) { b.method(); }
public void method() { System.out.println(0); }
}
public static void main(String []args){
new A(); // crashes
}
} |
If a class constructor calls another method, references in that method are not checked for initialization.
TypeScript Version: 4.0.3
Search Terms: false negative null undefined used before being assigned
Code
Expected behavior: error:
b
used before being assignedActual behavior: no error
Related Issues:
Note that this problem can be solved (mostly) with this: #30462
That issue talked about a false positive, but this false negative is also a problem that could be solved with the same solution.
It can't be solved completely because of possible complexities in structures.
But I ran into a null pointer exception that would have been caught by TypeScript if it just followed a simple single level of function calls.
The text was updated successfully, but these errors were encountered: