Added small performance optimization in checker to avoid generating a diagnostic message in cases where it won't be used.

This commit is contained in:
Eric Traut 2024-08-10 12:42:17 -06:00
Родитель 913d18d960
Коммит 2b82d07f69
1 изменённых файлов: 9 добавлений и 6 удалений

Просмотреть файл

@ -4942,21 +4942,24 @@ export class Checker extends ParseTreeWalker {
);
}
} else if (!FunctionType.isAbstractMethod(functionType)) {
// If the function consists entirely of "...", assume that it's
// an abstract method or a protocol method and don't require that
// the return type matches. This check can also be skipped for an overload.
const isEmptySuite =
ParseTreeUtils.isSuiteEmpty(node.d.suite) || FunctionType.isOverloaded(functionType);
// Make sure that the function doesn't implicitly return None if the declared
// type doesn't allow it. Skip this check for abstract methods.
const diagAddendum = new DiagnosticAddendum();
const diagAddendum = isEmptySuite ? undefined : new DiagnosticAddendum();
// If the declared type isn't compatible with 'None', flag an error.
if (!this._evaluator.assignType(declaredReturnType, this._evaluator.getNoneType(), diagAddendum)) {
// If the function consists entirely of "...", assume that it's
// an abstract method or a protocol method and don't require that
// the return type matches. This check can also be skipped for an overload.
if (!ParseTreeUtils.isSuiteEmpty(node.d.suite) && !FunctionType.isOverloaded(functionType)) {
if (!isEmptySuite) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportReturnType,
LocMessage.returnMissing().format({
returnType: this._evaluator.printType(declaredReturnType),
}) + diagAddendum.getString(),
}) + diagAddendum?.getString(),
returnAnnotation
);
}