From b15a2b2a02dc9af2b47b77eb3aede73ffa85ac66 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 12 Jun 2021 20:52:00 -0400 Subject: [PATCH] fix(eslint-plugin): allow explicit any for no-unsafe-return (#3498) --- .../eslint-plugin/src/rules/no-unsafe-return.ts | 17 ++++++++++------- .../tests/rules/no-unsafe-return.test.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-unsafe-return.ts b/packages/eslint-plugin/src/rules/no-unsafe-return.ts index 4d65ce529af..d460dd04c9b 100644 --- a/packages/eslint-plugin/src/rules/no-unsafe-return.ts +++ b/packages/eslint-plugin/src/rules/no-unsafe-return.ts @@ -91,6 +91,16 @@ export default util.createRule({ functionType = checker.getTypeAtLocation(functionTSNode); } + // If there is an explicit type annotation *and* that type matches the actual + // function return type, we shouldn't complain (it's intentional, even if unsafe) + if (functionTSNode.type) { + for (const signature of functionType.getCallSignatures()) { + if (returnNodeType === signature.getReturnType()) { + return; + } + } + } + if (anyType !== util.AnyType.Safe) { // Allow cases when the declared return type of the function is either unknown or unknown[] // and the function is returning any or any[]. @@ -140,13 +150,6 @@ export default util.createRule({ for (const signature of functionType.getCallSignatures()) { const functionReturnType = signature.getReturnType(); - if (returnNodeType === functionReturnType) { - // don't bother checking if they're the same - // either the function is explicitly declared to return the same type - // or there was no declaration, so the return type is implicit - return; - } - const result = util.isUnsafeAssignment( returnNodeType, functionReturnType, diff --git a/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts b/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts index 0f533917d54..2bbdfdd5ffa 100644 --- a/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts @@ -40,6 +40,18 @@ function foo() { ` function foo() { return []; +} + `, + // explicit any return type is allowed, if you want to be unsafe like that + ` +function foo(): any { + return {} as any; +} + `, + // explicit any array return type is allowed, if you want to be unsafe like that + ` +function foo(): any[] { + return [] as any[]; } `, // explicit any generic return type is allowed, if you want to be unsafe like that