- Notifications
You must be signed in to change notification settings - Fork13.2k
Flag non-nullable functions inif statements as errors (tree walk version)#33178
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| // try to verify results of module resolution | ||
| for(const{oldFile:oldSourceFile,newFile:newSourceFile}ofmodifiedSourceFiles){ | ||
| constnewSourceFilePath=getNormalizedAbsolutePath(newSourceFile.originalFileName,currentDirectory); | ||
| if(resolveModuleNamesWorker){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Looks likeresolveModuleNamesWorker is always assigned above, so this was a dead check
| exportfunctioncompose<T>(...args:((t:T)=>T)[]):(t:T)=>T; | ||
| exportfunctioncompose<T>(a:(t:T)=>T,b:(t:T)=>T,c:(t:T)=>T,d:(t:T)=>T,e:(t:T)=>T):(t:T)=>T{ | ||
| if(e){ | ||
| if(!!e){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is a pretty good example of what TS would now consider suspicious-enough code to error on. Makinge optional would also have fixed this.
orta commentedSep 11, 2019
@typescript-bot test this |
typescript-bot commentedSep 11, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
typescript-bot commentedSep 11, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
typescript-bot commentedSep 11, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
typescript-bot commentedSep 11, 2019
The user suite test run you requested has finished andfailed. I've opened aPR with the baseline diff from master. |
RyanCavanaugh commentedSep 11, 2019
The RWC diff is unrelated. The User Test Suite diff is a false positive, but not a bad one:https://github.com/npm/cli/blob/latest/test/tap/check-permissions.js#L27 |
RyanCavanaugh commentedSep 11, 2019
@typescript-bot perf test this |
typescript-bot commentedSep 11, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@RyanCavanaugh, I've started to run the perf test suite on this PR at3c9e338. You can monitor the buildhere. It should now contribute to this PR's status checks. Update:The results are in! |
typescript-bot commentedSep 11, 2019
@RyanCavanaugh Here they are:Comparison Report - master..33178
System
Hosts
Scenarios
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
orta commentedSep 23, 2019
Hrm, none of these builds have accessible logs. I bet it's likely that community projects will need changes like: - if (e) {+ if (!!e) { Will re-run and take a look. @typescript-bot test this |
typescript-bot commentedSep 23, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
typescript-bot commentedSep 23, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
typescript-bot commentedSep 23, 2019
The user suite test run you requested has finished andfailed. I've opened aPR with the baseline diff from master. |
orta commentedSep 23, 2019
…nonNullableCallSignaturesTreeWalk🤖 User test baselines have changed for nonNullableCallSignaturesTreeWalk
orta commentedSep 25, 2019
OK, perf looks acceptable to me. The idea is a good one, and I feel good about the implementation - thanks@jwbay! |
user753 commentedOct 2, 2019
@jwbay |
RyanCavanaugh commentedOct 2, 2019
@user753 a few major problems appeared when we tried that approach. Array access is optimistically assumed to be in-bounds, so code like this gets incorrectly flagged as an error. The same thing happens for constarr=[rec,rec,rec];constel=arr[someIndex];if(el){// do something} Separately, many definition files were written prior to the introduction of Ultimately the very narrow check implemented here was the only one that didn't generate a huge amount of false positives. |
user753 commentedOct 3, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@RyanCavanaugh Thanks. |
RyanCavanaugh commentedOct 3, 2019
Normally if that does happen, a call to |
RyanCavanaugh commentedOct 3, 2019
@jwbay this might be my favorite check this release.https://github.com/microsoft/vscode/blob/ecba37c6ef3aab85b7a9b15afb3610bbe4eaac8c/extensions/css-language-features/server/src/cssServerMain.ts#L78 |
twavv commentedOct 6, 2019
It would be great if this was extended to functions that return promises as well. In the announcement for 3.7 (which I'mthrilled about!!!), you include this code example. functiondoAdminThing(user:User){if(user.isAdministrator){// ~~~~~~~~~~~~~~~~~~~~// error! This condition will always return true since the function is always defined.// Did you mean to call it instead?t It would be incredible if this was also extended to raise an error when the function returns a promise and it's not awaited before using it in a condition (I've personally been bitten by this exact bug before). classUser{// ...asyncisAdministrator(){/* ... */}}functiondoAdminThing(user:User){if(user.isAdministrator()){// ~~~~~~~~~~~~~~~~~~~~~~// error! This condition will always return true since a promise is always truthy.// Did you mean to await it instead? I tried searching for issues related to this but couldn't find any (it's possible that I just missed it because there are so many issues and I'm not sure what keywords to search). I've definitely been bitten by a bug where I did something like this: asyncfunctionpasswordMatches(email:string,password:string){/* return true if the password matches the result in the database */}if(passwordMatches("foo@bar.gov","passw0rd")){/* issue auth token */} |
omidkrad commentedOct 24, 2019
Regarding Uncalled Function Checks, I think it'd be pretty safe to also error on this: functionprocess(){console.log('Processing...');}process; |
bgub commentedMay 30, 2020
BTW this is breaking all of my polyfills, like checks for |
vinothbabu commentedMay 31, 2020
The problem with this is, it's not allowing any of the valid ways to pass. As@nebrelbug pointed above.
We had to adapt to this, which passes
|
Uh oh!
There was an error while loading.Please reload this page.
This is an alternate version of#32802 -- see that one for context.
This version attempts to prevent false positives by doing a syntax walk to see if the checked function is used, as opposed to relying on a heuristic where the function returns a boolean. The first commit is the same for both PR; the second adds filtering that differs.
Opened as a separate PR by request (cc@RyanCavanaugh)
Notably, this change flagged a couple things in the compiler itself. Commented below for both.