-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
[no-unnecessary-condition] Wrong report "Unnecessary conditional" #2421
[no-unnecessary-condition] Wrong report "Unnecessary conditional" #2421
Comments
Seems unary negation is very broken moving from 3.9.1 -> 3.10.0: export function foo(value: unknown) {
if (!value) throw new Error('bad value'); // always fails with @typescript-eslint/no-unnecessary-condition
} |
probably due to #2382? |
@bradzacher yes, this regression is caused by the mentioned Pull Request. In your code review, you were originally right when you mentioned that the unary expression checker should simply call The problem with Let's consider the following code: function runTest() {
const element = document.querySelector('.classSelector');
if (!element) {
throw new Error('Element not found');
}
return element;
}
runTest();
This is because it incorrectly thinks that since Not only that, this function also does not check for other edge cases that declare function doSomething(): void;
declare function getBoolean(): boolean;
declare function getUnknown(): unknown;
function runTest(): void {
const booleanTyped = getBoolean();
const unknownTyped = getUnknown();
if (!(booleanTyped || unknownTyped)) {
doSomething();
}
}
runTest(); The condition is necessary, because ultimately the result of the unary's arguments might be of type
In other words - If my analysis is correct, I think #2382 should be reverted and reimplemented in a different way (a couple more test cases to check for this regression wouldn't hurt as well). Fixing and extending |
A canary version should be published within the next 15 mins. Give it a go so I can verify the fix worked, and I'll see if we can get an out-of-band hotfix published. |
@bradzacher Confirmed: the issue is gone when I use the canary release. Thank your for the immediate fix! |
Another confirmation here; issue encountered on 3.10.0 and fixed in 3.10.1-alpha.1. |
Reading through the original issue for #2255, I'm not sure I understand what the motivating use cases for that change were. I do agree that |
Repro
The following code wrongly reports Unnecessary conditional, value is always falsy. It's a simplification of several cases where I have if-statements with negations for a function's boolean return value.
Rule definition in .eslintrc.yml:
tsconfig.json:
Expected Result
No message
Actual Result
Message Unnecessary conditional, value is always falsy.
Versions
@typescript-eslint/eslint-plugin
3.9.2-alpha.10
@typescript-eslint/parser
3.9.2-alpha.10
TypeScript
4.0.2
ESLint
7.7.0
node
14.8.0
The text was updated successfully, but these errors were encountered: